Skip to content

Commit

Permalink
refactor: read cli options from .opts()
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed May 17, 2024
1 parent a6ca1cb commit 7e04474
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
3 changes: 2 additions & 1 deletion packages/babel-cli/src/babel-external-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
28 changes: 14 additions & 14 deletions packages/babel-cli/src/babel/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
//
commander.parse(args);

const opts = commander.opts();

const errors: string[] = [];

let filenames = commander.args.reduce(function (globbed: string[], input) {
Expand All @@ -234,20 +236,20 @@ export default function parseArgv(args: Array<string>): 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");
}

Expand All @@ -256,29 +258,29 @@ export default function parseArgv(args: Array<string>): 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",
);
Expand All @@ -292,8 +294,6 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
return null;
}

const opts = commander.opts();

const babelOptions: InputOptions = {
presets: opts.presets,
plugins: opts.plugins,
Expand Down
19 changes: 10 additions & 9 deletions packages/babel-node/src/_babel-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down

0 comments on commit 7e04474

Please sign in to comment.