Skip to content

Commit

Permalink
fixed issue for loading TypeScript config file
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Sep 22, 2024
1 parent f3ffc50 commit 73cef7b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
41 changes: 33 additions & 8 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -210,29 +209,48 @@ 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);
if (ext === '.json' || configPath.slice(-2) === 'rc') {
return JSON.parse(Util.readFileSync(configPath));
}

if (ext === '.ts') {
initTSPreload();
}

let configOptions;
let err;
try {
Expand All @@ -256,6 +274,8 @@ const resolveConfigOptions = async (configPath) => {

const mergeReports = async (str, cliOptions) => {

await initNodeOptions(cliOptions);

const customConfig = cliOptions.config;
const configPath = findUpConfig(customConfig);

Expand Down Expand Up @@ -321,6 +341,11 @@ program.command('merge-reports')
.option('-n, --name <name>', 'report name for title')
.option('-o, --outputFile <path>', 'output file')

.option('--import <module>', 'preload module at startup')
.option('--require <module>', 'preload module at startup')

.option('--env [path]', 'env file (default: ".env")')

.action((str, options) => {
mergeReports(str, options);
});
Expand Down
37 changes: 0 additions & 37 deletions lib/hooks.js

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -84,4 +83,4 @@
"vine-ui": "^3.1.16",
"ws": "^8.18.0"
}
}
}

0 comments on commit 73cef7b

Please sign in to comment.