Skip to content

Commit

Permalink
fixed loading .ts config file (npm i amaro)
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Sep 21, 2024
1 parent 385e6f4 commit be9fb5d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

* 2.8.2
- fixed loading .ts config file (`npm i amaro`)

* 2.8.1
- fixed onData issue (#141)

Expand Down
36 changes: 36 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -190,13 +191,48 @@ const findUpConfig = (customConfigFile) => {
// default config not found
};

const checkRegisterFeature = () => {
const nv = process.versions.node;

// "module.register" added in Node.js: v20.6.0
// if (Util.cmpVersion(nv, '20.6.0') >= 0) {
// return true;
// }
// but also added in: v18.19.0
const requiredNV = '18.19.0';
if (Util.cmpVersion(nv, requiredNV) < 0) {
Util.logInfo(`The current Node.js version "${nv}" does NOT support "module.register", it requires "${requiredNV}" or higher.`);
return false;
}

// could be < 20.6.0 but just ignore it, please using latest minor version

return true;
};

const initTSPreload = () => {

const supportRegister = checkRegisterFeature();
if (!supportRegister) {
return;
}

register('./hooks.js', pathToFileURL(__filename));

};

// 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 Down
37 changes: 37 additions & 0 deletions lib/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

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
};
15 changes: 15 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,21 @@ const Util = {
return option;
},

cmpVersion: (v1, v2) => {
const [strMajor1, strMinor1, strPatch1] = `${v1}`.split('.');
const [strMajor2, strMinor2, strPatch2] = `${v2}`.split('.');
const strList = [strMajor1, strMinor1, strPatch1, strMajor2, strMinor2, strPatch2];
const list = strList.map((str) => Util.toNum(parseInt(str)));
const [major1, minor1, patch1, major2, minor2, patch2] = list;
if (major1 === major2) {
if (minor1 === minor2) {
return patch1 - patch2;
}
return minor1 - minor2;
}
return major1 - major2;
},

// ==========================================================================================

loggingLevels: {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"dependencies": {
"console-grid": "^2.2.2",
"eight-colors": "^1.3.0",
"eight-colors": "^1.3.1",
"koa": "^2.15.3",
"koa-static-resolver": "^1.0.6",
"lz-utils": "^2.1.0",
Expand All @@ -52,14 +52,15 @@
},
"devDependencies": {
"@babel/code-frame": "^7.24.7",
"@playwright/test": "^1.47.1",
"@playwright/test": "^1.47.2",
"amaro": "^0.1.8",
"ansi-to-html": "^0.7.2",
"async-tick": "^1.0.0",
"autolinker": "^4.0.0",
"axios": "^1.7.7",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"eslint": "^9.10.0",
"eslint": "^9.11.0",
"eslint-config-plus": "^2.0.2",
"eslint-plugin-html": "^8.1.1",
"eslint-plugin-vue": "^9.28.0",
Expand Down

0 comments on commit be9fb5d

Please sign in to comment.