Skip to content

Commit

Permalink
feat(contented): Programmatic API for Contented (#539)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:

As per title.
  • Loading branch information
fuxingloh authored Aug 4, 2023
1 parent da6833a commit 4d0fbc9
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/contented/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Config as ProcessorConfig } from '@contentedjs/contented-processor';

import { BuildCommand } from './commands/BuildCommand.js';
import { GenerateCommand } from './commands/GenerateCommand.js';
import { WatchCommand } from './commands/WatchCommand.js';
import { WriteCommand } from './commands/WriteCommand.js';

export * from '@contentedjs/contented-processor';

export interface PreviewConfig {
Expand All @@ -14,3 +19,33 @@ export interface ContentedConfig {
preview?: PreviewConfig;
processor: ProcessorConfig;
}

/**
* Programmatic API for Contented
* To be used together in a website project like next.config.js.
*/
/* eslint-disable import/no-default-export */
export default {
/**
* import contented from '@contentedjs/contented';
* await contented.build()
*/
async build({ watch = process.env.NODE_ENV === 'development' } = {}): Promise<void> {
if (watch) {
await new BuildCommand().execute();
} else {
await new WatchCommand().execute();
}
},
/**
* import contented from '@contentedjs/contented';
* await contented.preview()
*/
async preview({ watch = process.env.NODE_ENV === 'development' } = {}): Promise<void> {
if (watch) {
await new WriteCommand().execute();
} else {
await new GenerateCommand().execute();
}
},
};

0 comments on commit 4d0fbc9

Please sign in to comment.