/* eslint-disable no-console */ import output from '@/output' import { getInvokePrepTasks, isInvokeCommandAllowTarget, validateProjectCanExecuteInvokeCommand, getInvokeBackendTasks, getInvokeFrontEndTasks, } from '@/scripts' import { EOutputPrefix, setPrefixLogger, setOrginLogger, buildLoggerWithPrefix, EOutputLevel, } from '@/scripts/modular-output' import { createUpgradeMessage } from '@/scripts/shared' import { wrapperActionWithDevAppID } from '@/scripts/shared/dev-app-id' import type { InvokeCLIOptionsSchema } from '@ones-open/cli-utils' import { execaNode } from 'execa' import Listr from 'listr' enum EMode { BACKEND = 'backend', FRONTEND = 'frontend', ALL = 'all', } async function invoke(target: string, options: InvokeCLIOptionsSchema) { try { if (!isInvokeCommandAllowTarget(target)) { const errorMessage = `Target invalid, please execute 'npx op help invoke' to see the help information` throw new Error(errorMessage) } const isProjectFilesValidated = await validateProjectCanExecuteInvokeCommand() if (!isProjectFilesValidated) { const errorMessage = `Project file validation failed.` + `\nPlease check the project file or run 'npx op init' to reinitialize the project` throw new Error(errorMessage) } const { mode } = options const invokePrepTasks = await getInvokePrepTasks(target, options) const runFrontend = mode !== EMode.BACKEND const runBackend = mode !== EMode.FRONTEND const invokeTasks = [...invokePrepTasks] if (runBackend) { invokeTasks.push(...getInvokeBackendTasks(target)) } if (runFrontend) { invokeTasks.push(...getInvokeFrontEndTasks()) } const packupTasks = new Listr(invokeTasks) const taskRes = await packupTasks.run() setPrefixLogger(EOutputPrefix.frontend) if (runFrontend) { // add module-prefix for webpack-server output.info('Now starting front-end development server...') taskRes.frontEndDevServer.start() } if (runBackend) { output.info( `Local debug parameters initialized with target lifecycle: '${target}'\nNow starting node-host...`, ) const { nodeHostParameter: { nodeHostPath, nodeHostParams }, } = taskRes wrapperActionWithDevAppID(async () => { const nodeHostChildProcess = execaNode(nodeHostPath, nodeHostParams, { nodeOptions: ['--inspect'], }) nodeHostChildProcess.stdout?.pipe( buildLoggerWithPrefix(EOutputLevel.info, EOutputPrefix.backend), ) nodeHostChildProcess.stderr?.pipe( buildLoggerWithPrefix(EOutputLevel.error, EOutputPrefix.backend), ) // The node-host tar the `plugin.yaml` file, and send it to the backend. // This is a sync process, so use the `setTimeout` to reset the appID. await new Promise((res) => { setTimeout(() => { res(undefined) }, 3000) }) }, false) } const upgradeMessage = await createUpgradeMessage() if (upgradeMessage) { setPrefixLogger(EOutputPrefix.info) output.warn(upgradeMessage) setPrefixLogger(EOutputPrefix.frontend) } } catch (error) { setOrginLogger() output.error(error) } } export { invoke }