From 7e044740d4104bbb8593bffeee46a48ca2688766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 17 May 2024 16:01:17 -0400 Subject: [PATCH] refactor: read cli options from .opts() --- .../babel-cli/src/babel-external-helpers.ts | 3 +- packages/babel-cli/src/babel/options.ts | 28 +++++++++---------- packages/babel-node/src/_babel-node.ts | 19 +++++++------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/packages/babel-cli/src/babel-external-helpers.ts b/packages/babel-cli/src/babel-external-helpers.ts index 5b6fb3bdcc2b8..6f6d3ef403384 100755 --- a/packages/babel-cli/src/babel-external-helpers.ts +++ b/packages/babel-cli/src/babel-external-helpers.ts @@ -27,5 +27,6 @@ commander.option( commander.usage("[options]"); commander.parse(process.argv); +const opts = commander.opts(); -console.log(buildExternalHelpers(commander.whitelist, commander.outputType)); +console.log(buildExternalHelpers(opts.whitelist, opts.outputType)); diff --git a/packages/babel-cli/src/babel/options.ts b/packages/babel-cli/src/babel/options.ts index 1ca3c752e9e9e..4ace8ca6c6c2c 100644 --- a/packages/babel-cli/src/babel/options.ts +++ b/packages/babel-cli/src/babel/options.ts @@ -211,6 +211,8 @@ export default function parseArgv(args: Array): CmdOptions | null { // commander.parse(args); + const opts = commander.opts(); + const errors: string[] = []; let filenames = commander.args.reduce(function (globbed: string[], input) { @@ -234,20 +236,20 @@ export default function parseArgv(args: Array): CmdOptions | null { } }); - if (commander.outDir && !filenames.length) { + if (opts.outDir && !filenames.length) { errors.push("--out-dir requires filenames"); } - if (commander.outFile && commander.outDir) { + if (opts.outFile && opts.outDir) { errors.push("--out-file and --out-dir cannot be used together"); } - if (commander.relative && !commander.outDir) { + if (opts.relative && !opts.outDir) { errors.push("--relative requires --out-dir usage"); } - if (commander.watch) { - if (!commander.outFile && !commander.outDir) { + if (opts.watch) { + if (!opts.outFile && !opts.outDir) { errors.push("--watch requires --out-file or --out-dir"); } @@ -256,29 +258,29 @@ export default function parseArgv(args: Array): CmdOptions | null { } } - if (commander.skipInitialBuild && !commander.watch) { + if (opts.skipInitialBuild && !opts.watch) { errors.push("--skip-initial-build requires --watch"); } - if (commander.deleteDirOnStart && !commander.outDir) { + if (opts.deleteDirOnStart && !opts.outDir) { errors.push("--delete-dir-on-start requires --out-dir"); } - if (commander.verbose && commander.quiet) { + if (opts.verbose && opts.quiet) { errors.push("--verbose and --quiet cannot be used together"); } if ( - !commander.outDir && + !opts.outDir && filenames.length === 0 && - typeof commander.filename !== "string" && - commander.babelrc !== false + typeof opts.filename !== "string" && + opts.babelrc !== false ) { errors.push( "stdin compilation requires either -f/--filename [filename] or --no-babelrc", ); } - if (commander.keepFileExtension && commander.outFileExtension) { + if (opts.keepFileExtension && opts.outFileExtension) { errors.push( "--out-file-extension cannot be used with --keep-file-extension", ); @@ -292,8 +294,6 @@ export default function parseArgv(args: Array): CmdOptions | null { return null; } - const opts = commander.opts(); - const babelOptions: InputOptions = { presets: opts.presets, plugins: opts.plugins, diff --git a/packages/babel-node/src/_babel-node.ts b/packages/babel-node/src/_babel-node.ts index 340eb8c5befe7..abe581db20dd1 100644 --- a/packages/babel-node/src/_babel-node.ts +++ b/packages/babel-node/src/_babel-node.ts @@ -73,24 +73,25 @@ program.option("-b, --presets [string]", "", collect); program.version(PACKAGE_JSON.version); program.usage("[options] [ -e script | script.js ] [arguments]"); program.parse(process.argv); +const opts = program.opts(); const babelOptions = { caller: { name: "@babel/node", }, - extensions: program.extensions, - ignore: program.ignore, - only: program.only, - plugins: program.plugins, - presets: program.presets, - configFile: program.configFile, - envName: program.envName, - rootMode: program.rootMode, + extensions: opts.extensions, + ignore: opts.ignore, + only: opts.only, + plugins: opts.plugins, + presets: opts.presets, + configFile: opts.configFile, + envName: opts.envName, + rootMode: opts.rootMode, // Commander will default the "--no-" arguments to true, but we want to // leave them undefined so that @babel/core can handle the // default-assignment logic on its own. - babelrc: program.babelrc === true ? undefined : program.babelrc, + babelrc: opts.babelrc === true ? undefined : opts.babelrc, }; for (const key of Object.keys(babelOptions) as Array<