☕️ Classic, reliable, trusted test framework for Node.js and the browser
chore: convert `lib/cli/cli.mjs` to ESM (#5909)
Đỗ Trọng Hải6e91a8915e55
diff --git a/bin/mocha.js b/bin/mocha.jsindex a1b1a74d..f0e486b1 100755--- a/bin/mocha.js+++ b/bin/mocha.js@@ -4,7 +4,7 @@/*** This wrapper executable checks for known node flags and appends them when found,- * before invoking the "real" executable (`lib/cli/cli.js`)+ * before invoking the "real" executable (`lib/cli/cli.mjs`)** @module bin/mocha* @private@@ -81,7 +81,7 @@ if (mochaArgs._) {if (mochaArgs["node-option"] || Object.keys(nodeArgs).length || hasInspect) {const { spawn } = require("node:child_process");- const mochaPath = require.resolve("../lib/cli/cli.js");+ const mochaPath = require.resolve("../lib/cli/cli.mjs");const nodeArgv =(mochaArgs["node-option"] &&@@ -146,5 +146,5 @@ if (mochaArgs["node-option"] || Object.keys(nodeArgs).length || hasInspect) {});} else {debug("running Mocha in-process");- require("../lib/cli/cli").main([], mochaArgs);+ require("../lib/cli/cli.mjs").main([], mochaArgs);}diff --git a/lib/cli/cli.js b/lib/cli/cli.mjssimilarity index 63%rename from lib/cli/cli.jsrename to lib/cli/cli.mjsindex dae1f9ef..37e9e609 100755--- a/lib/cli/cli.js+++ b/lib/cli/cli.mjs@@ -1,5 +1,3 @@-"use strict";-/*** Contains CLI entry point and public API for programmatic usage in Node.js.* - Option parsing is handled by {@link https://npm.im/yargs yargs}.@@ -8,25 +6,20 @@* @module lib/cli*/-const debug = require("debug")("mocha:cli:cli");-const yargs = require("yargs");-const path = require("node:path");-const {- loadRc,- loadPkgRc,- loadOptions,- YARGS_PARSER_CONFIG,-} = require("./options.mjs");-const { lookupFiles } = require("./lookup-files.mjs");-const commands = require("./commands.mjs");-const pc = require("picocolors");-const {- repository,- homepage,- version,- discord,-} = require("../../package.json");-const { cwd, logSymbols } = require("../utils");+import debugModule from "debug";+import yargs from "yargs";+import { fileURLToPath } from "node:url";++import { loadOptions, YARGS_PARSER_CONFIG } from "./options.mjs";+import { run, init } from "./commands.mjs";+import pc from "picocolors";+import { logSymbols } from "../utils.js";++import packageJson from "../../package.json" with { type: "json" };++const __filename = fileURLToPath(import.meta.url);++const debug = debugModule("mocha:cli:cli");/*** - Accepts an `Array` of arguments@@ -37,12 +30,8 @@ const { cwd, logSymbols } = require("../utils");* @param {string[]} argv - Array of arguments to parse, or by default the lovely `process.argv.slice(2)`* @param {object} [mochaArgs] - Object of already parsed Mocha arguments (by bin/mocha)*/-exports.main = (argv = process.argv.slice(2), mochaArgs) => {+export function main(argv = process.argv.slice(2), mochaArgs) {debug("entered main with raw args", argv);- // ensure we can require() from current working directory- if (typeof module.paths !== "undefined") {- module.paths.push(cwd(), path.resolve("node_modules"));- }try {Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit?@@ -50,12 +39,12 @@ exports.main = (argv = process.argv.slice(2), mochaArgs) => {debug("unable to set Error.stackTraceLimit = Infinity", err);}- var args = mochaArgs || loadOptions(argv);+ const args = mochaArgs ?? loadOptions(argv);yargs().scriptName("mocha")- .command(commands.run)- .command(commands.init)+ .command(run)+ .command(init).updateStrings({"Positionals:": "Positional Arguments","Options:": "Other Options",@@ -74,27 +63,22 @@ exports.main = (argv = process.argv.slice(2), mochaArgs) => {}).help("help", "Show usage information & exit").alias("help", "h")- .version("version", "Show version number & exit", version)+ .version("version", "Show version number & exit", packageJson.version).alias("version", "V").wrap(process.stdout.columns ? Math.min(process.stdout.columns, 80) : 80).epilog(`${pc.reset("Mocha Resources")}- Chat: ${pc.magenta(discord)}- GitHub: ${pc.blue(repository.url)}- Docs: ${pc.yellow(homepage)}+ Chat: ${pc.magenta(packageJson.discord)}+ GitHub: ${pc.blue(packageJson.repository.url)}+ Docs: ${pc.yellow(packageJson.homepage)}`,).parserConfiguration(YARGS_PARSER_CONFIG).config(args).parse(args._);-};--exports.lookupFiles = lookupFiles;-exports.loadOptions = loadOptions;-exports.loadPkgRc = loadPkgRc;-exports.loadRc = loadRc;+}// allow direct execution-if (require.main === module) {- exports.main();+if (__filename === process.argv[1]) {+ main();}diff --git a/lib/cli/index.js b/lib/cli/index.jsindex 95fcfe5a..71038aa6 100644--- a/lib/cli/index.js+++ b/lib/cli/index.js@@ -1,3 +1,3 @@"use strict";-module.exports = require("./cli");+module.exports = require("./cli.mjs");