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 7975de7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 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
37 changes: 19 additions & 18 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 Expand Up @@ -145,18 +146,18 @@ const _eval = function (code: string, filename: string) {

code = babel.transformSync(code, {
filename: filename,
presets: program.presets,
plugins: (program.plugins || []).concat([replPlugin]),
presets: opts.presets,
plugins: (opts.plugins || []).concat([replPlugin]),
}).code;

return vm.runInThisContext(code, {
filename: filename,
});
};

if (program.eval || program.print) {
let code = program.eval;
if (!code || code === true) code = program.print;
if (opts.eval || opts.print) {
let code = opts.eval;
if (!code || code === true) code = opts.print;

global.__filename = "[eval]";
global.__dirname = process.cwd();
Expand All @@ -171,7 +172,7 @@ if (program.eval || program.print) {
global.require = module.require.bind(module);

const result = _eval(code, global.__filename);
if (program.print) {
if (opts.print) {
const output = typeof result === "string" ? result : inspect(result);
process.stdout.write(output + "\n");
}
Expand All @@ -196,7 +197,7 @@ if (program.eval || program.print) {
return;
}
const optionName = parsedOption.attributeName();
const parsedArg = program[optionName];
const parsedArg = opts[optionName];
if (optionName === "require" || (parsedArg && parsedArg !== true)) {
ignoreNext = true;
}
Expand Down Expand Up @@ -228,9 +229,9 @@ if (program.eval || program.print) {

// We have to handle require ourselves, as we want to require it in the context of babel-register
function requireArgs() {
if (program.require) {
if (opts.require) {
require(
require.resolve(program.require, {
require.resolve(opts.require, {
paths: [process.cwd()],
}),
);
Expand Down

0 comments on commit 7975de7

Please sign in to comment.