@lynx-js/rspeedy
Version:
A webpack/rspack-based frontend toolchain for Lynx
47 lines (46 loc) • 3.2 kB
JavaScript
import node_path from "node:path";
import { version_version } from "./src_cli_main_ts-node_child_process-node_events-node_fs-node_path-node_process.js";
function applyCommonOptions(command) {
command.option('-c --config <config>', 'specify the configuration file, can be a relative or absolute path').option('--env-mode <mode>', 'specify the env mode to load the .env.[mode] file').option('--no-env', 'disable loading `.env` files"').option('-m --mode <mode>', 'specify the build mode, can be `development`, `production` or `none`').option('-r --root <root>', 'set the project root directory (absolute path or relative to cwd)');
}
function resolveRoot(cwd, root) {
if (!root) return cwd;
return node_path.isAbsolute(root) ? root : node_path.resolve(cwd, root);
}
function apply(program) {
const cwd = process.cwd();
program.name('rspeedy').usage('<command> [options]').version(version_version).showHelpAfterError(true).showSuggestionAfterError(true).exitOverride();
const buildCommand = program.command('build');
buildCommand.description('Build the project in production mode').option('--environment <name...>', 'specify the name of environment to build').option('--watch', 'Enable watch mode to automatically rebuild on file changes').action(async (buildOptions)=>{
const actualRoot = resolveRoot(cwd, buildOptions.root);
const { build } = await import("./1~build.js");
return await build.call(buildCommand, actualRoot, buildOptions);
});
const devCommand = program.command('dev');
devCommand.description('Run the dev server and watch for source file changes while serving.').option('--base <base>', 'specify the base path of the server').option('--environment <name...>', 'specify the name of environment to build').action(async (devOptions)=>{
const actualRoot = resolveRoot(cwd, devOptions.root);
const { dev } = await import("./1~dev.js");
return await dev.call(devCommand, actualRoot, devOptions);
});
const inspectCommand = program.command('inspect');
inspectCommand.description('View the Rsbuild config and Rspack config of the project.').option('--output <output>', 'specify inspect content output path').option('--verbose', 'show full function definitions in output').action(async (inspectOptions)=>{
const actualRoot = resolveRoot(cwd, inspectOptions.root);
const { inspect } = await import("./1~inspect.js");
return await inspect.call(inspectCommand, actualRoot, inspectOptions);
});
const previewCommand = program.command('preview');
previewCommand.description('Preview the production build outputs locally.').option('--base <base>', 'specify the base path of the server').action(async (previewOptions)=>{
const actualRoot = resolveRoot(cwd, previewOptions.root);
const { preview } = await import("./1~preview.js");
return await preview.call(previewCommand, actualRoot, previewOptions);
});
const commonCommands = [
devCommand,
buildCommand,
inspectCommand,
previewCommand
];
commonCommands.forEach((command)=>applyCommonOptions(command));
return program;
}
export { apply };