From 3dbae3f1b84180ea2308c9d891cb86c49d2cf165 Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Wed, 14 Feb 2024 10:33:08 +0800 Subject: [PATCH] fix(contented): programmatic execution (#795) #### What this PR does / why we need it: As per title. --- packages/contented/src/cli.ts | 3 +-- .../contented/src/commands/WatchCommand.ts | 20 ------------------- packages/contented/src/index.ts | 20 +++++++++---------- 3 files changed, 10 insertions(+), 33 deletions(-) delete mode 100644 packages/contented/src/commands/WatchCommand.ts diff --git a/packages/contented/src/cli.ts b/packages/contented/src/cli.ts index 0e21dd9b..613d2c12 100644 --- a/packages/contented/src/cli.ts +++ b/packages/contented/src/cli.ts @@ -3,8 +3,7 @@ import { runExit } from 'clipanion'; import { BuildCommand } from './commands/BuildCommand.js'; import { GenerateCommand } from './commands/GenerateCommand.js'; -import { WatchCommand } from './commands/WatchCommand.js'; import { WriteCommand } from './commands/WriteCommand.js'; // eslint-disable-next-line @typescript-eslint/no-floating-promises -runExit([BuildCommand, GenerateCommand, WatchCommand, WriteCommand]); +runExit([BuildCommand, GenerateCommand, WriteCommand]); diff --git a/packages/contented/src/commands/WatchCommand.ts b/packages/contented/src/commands/WatchCommand.ts deleted file mode 100644 index efed4ba9..00000000 --- a/packages/contented/src/commands/WatchCommand.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { ContentedProcessor } from '@contentedjs/contented-processor'; - -import { BaseCommand } from './BaseCommand.js'; -import { ContentedWatcher } from './contented/ContentedWatcher.js'; - -/** - * `contented watch` files and automatically rebuild when changed into output directory `.contented` - * @deprecated use `contented build --watch` instead - */ -export class WatchCommand extends BaseCommand { - static paths = [[`watch`]]; - - async execute() { - const config = await this.loadConfig(); - const processor = new ContentedProcessor(config.processor); - - const watcher = new ContentedWatcher(this.context, processor); - await watcher.watch(); - } -} diff --git a/packages/contented/src/index.ts b/packages/contented/src/index.ts index b5737fab..a3184faa 100644 --- a/packages/contented/src/index.ts +++ b/packages/contented/src/index.ts @@ -1,10 +1,11 @@ import * as process from 'node:process'; import { Config as ProcessorConfig } from '@contentedjs/contented-processor'; +import { Cli } from 'clipanion'; +import { BaseCommand } from './commands/BaseCommand'; 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'; @@ -33,21 +34,18 @@ export default { * await contented.build() */ async build({ watch = process.env.NODE_ENV === 'development' } = {}): Promise { - if (watch) { - await new WatchCommand().execute(); - } else { - await new BuildCommand().execute(); - } + const build = new BuildCommand(); + build.context = Cli.defaultContext; + build.watch = watch; + await build.execute(); }, /** * import contented from '@contentedjs/contented'; * await contented.preview() */ async preview({ watch = process.env.NODE_ENV === 'development' } = {}): Promise { - if (watch) { - await new WriteCommand().execute(); - } else { - await new GenerateCommand().execute(); - } + const command: BaseCommand = watch ? new WriteCommand() : new GenerateCommand(); + command.context = Cli.defaultContext; + await command.execute(); }, };