import { createReportCliCommands } from '@midscene/core'; import type { BaseMidsceneTools } from '@midscene/shared/agent-tools/base-tools'; import { runToolsCLI } from '@midscene/shared/cli'; import { version } from '../package.json'; import { matchYamlFiles, parseProcessArgs } from './cli-utils'; import { type ConfigFactoryOptions, createConfig, createFilesConfig, } from './config-factory'; import { pickYamlTargetConfig } from './config-options'; import { loadDotenvConfig } from './dotenv-loader'; import { runFrameworkTestConfig } from './framework'; import { runModelCommand } from './model-command'; import { assertLegacyBatchConfigPath, assertLegacyWorkflowSelection, } from './workflow-format'; Promise.resolve( (async () => { const rawArgs = process.argv.slice(2); const [firstArg] = rawArgs; if (firstArg === 'report-tool') { await runToolsCLI( { initTools: async () => undefined, destroy: async () => undefined, getToolDefinitions: () => [], } as unknown as BaseMidsceneTools, 'midscene', { argv: rawArgs, version, extraCommands: createReportCliCommands(), }, ); return; } if (firstArg === 'model') { const exitCode = await runModelCommand(rawArgs); process.exit(exitCode); return; } const { options, path, files: cmdFiles } = await parseProcessArgs(); const welcome = `\nWelcome to @midscene/cli v${version}\n`; console.log(welcome); if (options.url) { console.error( 'the cli mode is no longer supported, please use yaml file instead. See https://www.midscenejs.com/automate-with-scripts-in-yaml for more information. Sorry for the inconvenience.', ); process.exit(1); } const configFile = options.config as string | undefined; if (!configFile && !path && !(cmdFiles && cmdFiles.length > 0)) { console.error('No script path, files, or config provided'); process.exit(1); } // Extract new configuration options const configOptions = { ...pickYamlTargetConfig(options), concurrent: options.concurrent, continueOnError: options['continue-on-error'], retry: options.retry, summary: options.summary, shareBrowserContext: options['share-browser-context'], reusePage: options['reuse-page'], headed: options.headed, keepWindow: options['keep-window'], dotenvOverride: options['dotenv-override'], dotenvDebug: options['dotenv-debug'], files: cmdFiles, setup: options.setup as string | undefined, } satisfies ConfigFactoryOptions; let config; if (configFile) { assertLegacyBatchConfigPath(configFile); config = await createConfig(configFile, configOptions); console.log(` Config file: ${configFile}`); } else if (cmdFiles && cmdFiles.length > 0) { console.log(' Executing YAML files from --files argument...'); config = await createFilesConfig(cmdFiles, configOptions); } else if (path) { const files = await matchYamlFiles(path); if (files.length === 0) { console.error(`No yaml files found in ${path}`); process.exit(1); } console.log(' Executing YAML files...'); config = await createFilesConfig(files, configOptions); } if (!config) { console.error('Could not create a valid configuration.'); process.exit(1); } assertLegacyWorkflowSelection([ ...(config.setup ? [config.setup] : []), ...config.files, ]); loadDotenvConfig({ dotenvDebug: config.dotenvDebug, dotenvOverride: config.dotenvOverride, log: console.log, }); const exitCode = await runFrameworkTestConfig(config); if (config.keepWindow) { // hang the process to keep the browser window open setInterval(() => { console.log('browser is still running, use ctrl+c to stop it'); }, 5000); } else { process.exit(exitCode); } })().catch((e) => { console.error(e); process.exit(1); }), );