diff --git a/CHANGELOG.md b/CHANGELOG.md index 180adff..02d59ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ ## Changelog +* 2.8.3 + - fixed issue for loading TypeScript config file + * 2.8.2 - - fixed loading .ts config file (`npm i amaro`) + - fixed loading .ts config file * 2.8.1 - fixed onData issue (#141) diff --git a/lib/cli.js b/lib/cli.js index 17bc473..74fb691 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -7,7 +7,6 @@ const https = require('https'); const net = require('net'); const os = require('os'); const { pathToFileURL } = require('url'); -const { register } = require('module'); const EC = require('eight-colors'); const KSR = require('koa-static-resolver'); const Koa = require('koa'); @@ -210,18 +209,41 @@ const checkRegisterFeature = () => { return true; }; -const initTSPreload = () => { +const loadEnv = (cliOptions) => { + if (!cliOptions.env) { + return; + } + const envFile = cliOptions.env === true ? '.env' : cliOptions.env; + const loadEnvFile = process.loadEnvFile; + if (typeof loadEnvFile === 'function') { + loadEnvFile(envFile); + } +}; + + +const initNodeOptions = async (cliOptions) => { + + loadEnv(cliOptions); const supportRegister = checkRegisterFeature(); if (!supportRegister) { return; } - register('./hooks.js', pathToFileURL(__filename)); + // for loading mr.config.ts + const modulePath = cliOptions.import || cliOptions.require; + if (!modulePath) { + return; + } + + const res = await import(modulePath); + if (res && typeof res.register === 'function') { + await res.register(); + } }; -// eslint-disable-next-line complexity + const resolveConfigOptions = async (configPath) => { // json format const ext = path.extname(configPath); @@ -229,10 +251,6 @@ const resolveConfigOptions = async (configPath) => { return JSON.parse(Util.readFileSync(configPath)); } - if (ext === '.ts') { - initTSPreload(); - } - let configOptions; let err; try { @@ -256,6 +274,8 @@ const resolveConfigOptions = async (configPath) => { const mergeReports = async (str, cliOptions) => { + await initNodeOptions(cliOptions); + const customConfig = cliOptions.config; const configPath = findUpConfig(customConfig); @@ -321,6 +341,11 @@ program.command('merge-reports') .option('-n, --name ', 'report name for title') .option('-o, --outputFile ', 'output file') + .option('--import ', 'preload module at startup') + .option('--require ', 'preload module at startup') + + .option('--env [path]', 'env file (default: ".env")') + .action((str, options) => { mergeReports(str, options); }); diff --git a/lib/hooks.js b/lib/hooks.js deleted file mode 100644 index 8e21a37..0000000 --- a/lib/hooks.js +++ /dev/null @@ -1,37 +0,0 @@ - -const EC = require('eight-colors'); - -const extensionsRegex = /\.ts$|\.mts$|\.cts$/; - -async function load(url, context, nextLoad) { - - if (extensionsRegex.test(url) && !url.includes('/node_modules/')) { - - const { transformSync } = await import('amaro').catch((e) => { - // console.log(e.message || e); - EC.logRed('The "amaro" module is required for loading ".ts" file, please run "npm i amaro"'); - }); - - // Use format 'module' so it returns the source as-is, without stripping the types. - // Format 'commonjs' would not return the source for historical reasons. - const { source } = await nextLoad(url, { - ... context, - format: 'module' - }); - if (source === null) { - throw new Error('Source code cannot be null or undefined'); - } - const { code } = transformSync(source.toString(), { - mode: 'strip-only' - }); - return { - format: 'module', - source: code - }; - } - return nextLoad(url, context); -} - -module.exports = { - load -}; diff --git a/package.json b/package.json index 513780b..95dacdb 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,6 @@ "devDependencies": { "@babel/code-frame": "^7.24.7", "@playwright/test": "^1.47.2", - "amaro": "^0.1.8", "ansi-to-html": "^0.7.2", "async-tick": "^1.0.0", "autolinker": "^4.0.0", @@ -84,4 +83,4 @@ "vine-ui": "^3.1.16", "ws": "^8.18.0" } -} +} \ No newline at end of file