From 662b9e924d8e83959ee44e2ef4b1ac7541378b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Thu, 17 Feb 2022 20:57:45 +0800 Subject: [PATCH] fix: using ts-node in app should check tscompiler and deps (#170) * fix: local ts-node * fix: local ts node * fix: ci * fix: ci * fix: ci * test: add more test cases * fix: remove useless code * fix: ci * chore: update comment * chore: use cpy --- .gitignore | 2 - lib/cmd/test.js | 5 +- lib/command.js | 16 +++- package.json | 1 + .../config/config.default.ts | 3 + .../example-ts-custom-compiler-2/package.json | 3 + .../node_modules/egg/index.js | 8 -- .../node_modules/egg/package.json | 3 - .../example-ts-custom-compiler/package.json | 7 +- .../test/index.test.ts | 6 ++ test/ts.test.js | 82 ++++++++++++++++++- 11 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 test/fixtures/example-ts-custom-compiler-2/config/config.default.ts create mode 100644 test/fixtures/example-ts-custom-compiler-2/package.json delete mode 100644 test/fixtures/example-ts-custom-compiler/node_modules/egg/index.js delete mode 100644 test/fixtures/example-ts-custom-compiler/node_modules/egg/package.json create mode 100644 test/fixtures/example-ts-custom-compiler/test/index.test.ts diff --git a/.gitignore b/.gitignore index 38d66be0..efc6a569 100644 --- a/.gitignore +++ b/.gitignore @@ -17,8 +17,6 @@ test/fixtures/ts/node_modules/aliyun-egg/ test/fixtures/example-ts-ets/typings/ !test/fixtures/example-ts-ets/node_modules/ !test/fixtures/example-ts-simple/node_modules/ -!test/fixtures/example-ts-custom-compiler/node_modules/ - **/run/*.json .tmp diff --git a/lib/cmd/test.js b/lib/cmd/test.js index ed70b479..704709c7 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -116,9 +116,8 @@ class TestCommand extends Command { // for power-assert if (testArgv.typescript && testArgv.espower) { - const tscompilerPath = require.resolve(testArgv.tscompiler); - requireArr.splice(requireArr.indexOf(tscompilerPath), 1); - requireArr.push(tscompilerPath, require.resolve('../espower-typescript')); + requireArr.splice(requireArr.indexOf(testArgv.tscompiler), 1); + requireArr.push(testArgv.tscompiler, require.resolve('../espower-typescript')); } testArgv.require = requireArr; diff --git a/lib/command.js b/lib/command.js index c521c40a..93606a09 100644 --- a/lib/command.js +++ b/lib/command.js @@ -76,8 +76,17 @@ class Command extends BaseCommand { } // read `egg.tscompiler` from package.json if not pass argv - if (argv.tscompiler === undefined) { - argv.tscompiler = eggInfo.tscompiler || 'ts-node/register'; + // try to load from `cwd` while tscompipler has value or app has ts-node deps + if (argv.tscompiler === undefined && !eggInfo.tscompiler) { + const useAppTsNode = pkgInfo && ( + (pkgInfo.dependencies && pkgInfo.dependencies['ts-node']) || + (pkgInfo.devDependencies && pkgInfo.devDependencies['ts-node']) + ); + + argv.tscompiler = require.resolve('ts-node/register', useAppTsNode ? { paths: [ cwd ] } : undefined); + } else { + argv.tscompiler = argv.tscompiler || eggInfo.tscompiler; + argv.tscompiler = require.resolve(argv.tscompiler, { paths: [ cwd ] }); } // read `egg.require` from package.json @@ -87,8 +96,7 @@ class Command extends BaseCommand { // load ts-node if (argv.typescript) { - // try to load from `cwd` first - execArgvObj.require.push(require.resolve(argv.tscompiler, { paths: [ cwd ] })); + execArgvObj.require.push(argv.tscompiler); // tell egg loader to load ts file env.EGG_TYPESCRIPT = 'true'; diff --git a/package.json b/package.json index 51a85aa9..d63614bd 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "babel-preset-airbnb": "^1.0.1", "babel-register": "^6.4.3", "coffee": "^5.4.0", + "cpy": "^7.0.0", "cross-env": "^3.1.3", "egg": "^2.29.4", "egg-mock": "^4.1.0", diff --git a/test/fixtures/example-ts-custom-compiler-2/config/config.default.ts b/test/fixtures/example-ts-custom-compiler-2/config/config.default.ts new file mode 100644 index 00000000..feefe62a --- /dev/null +++ b/test/fixtures/example-ts-custom-compiler-2/config/config.default.ts @@ -0,0 +1,3 @@ +'use strict'; + +export const key = '12345'; diff --git a/test/fixtures/example-ts-custom-compiler-2/package.json b/test/fixtures/example-ts-custom-compiler-2/package.json new file mode 100644 index 00000000..d84d5232 --- /dev/null +++ b/test/fixtures/example-ts-custom-compiler-2/package.json @@ -0,0 +1,3 @@ +{ + "name": "example-ts-custom-compiler-2" +} diff --git a/test/fixtures/example-ts-custom-compiler/node_modules/egg/index.js b/test/fixtures/example-ts-custom-compiler/node_modules/egg/index.js deleted file mode 100644 index 35a2064f..00000000 --- a/test/fixtures/example-ts-custom-compiler/node_modules/egg/index.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict'; - -module.exports = require('../../../../../node_modules/egg'); - -setTimeout(() => { - console.log('exit by master test end'); - process.exit(0); -}, 10000); diff --git a/test/fixtures/example-ts-custom-compiler/node_modules/egg/package.json b/test/fixtures/example-ts-custom-compiler/node_modules/egg/package.json deleted file mode 100644 index 6697ad3f..00000000 --- a/test/fixtures/example-ts-custom-compiler/node_modules/egg/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name": "egg" -} diff --git a/test/fixtures/example-ts-custom-compiler/package.json b/test/fixtures/example-ts-custom-compiler/package.json index 54197843..a995fff0 100644 --- a/test/fixtures/example-ts-custom-compiler/package.json +++ b/test/fixtures/example-ts-custom-compiler/package.json @@ -1,3 +1,6 @@ { - "name": "example-ts-custom-compiler" -} \ No newline at end of file + "name": "example-ts-custom-compiler", + "dependencies": { + "ts-node": "^8.10.2" + } +} diff --git a/test/fixtures/example-ts-custom-compiler/test/index.test.ts b/test/fixtures/example-ts-custom-compiler/test/index.test.ts new file mode 100644 index 00000000..27f53268 --- /dev/null +++ b/test/fixtures/example-ts-custom-compiler/test/index.test.ts @@ -0,0 +1,6 @@ +describe('test', () => { + it('should ok', () => { + console.info(process.argv); + console.info(process.execArgv); + }); +}); \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index bca0a115..5008b0c2 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -4,6 +4,7 @@ const path = require('path'); const coffee = require('coffee'); const mm = require('mm'); const fs = require('fs'); +const cpy = require('cpy'); const rimraf = require('mz-modules/rimraf'); const exec = require('mz/child_process').exec; const os = require('os'); @@ -209,22 +210,73 @@ describe('test/ts.test.js', () => { const cwd = path.join(__dirname, './fixtures/example-ts-custom-compiler'); // install custom ts-node - await exec('npx cnpm install ts-node@8.10.2', { cwd: path.join(__dirname, './fixtures') }); + await rimraf(path.join(cwd, 'node_modules')); + await exec('npx cnpm install', { cwd }); + + // copy egg to node_modules + await cpy( + path.join(__dirname, './fixtures/example-ts-cluster/node_modules/egg'), + path.join(cwd, './node_modules/egg') + ); const { stderr, code } = await coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd, env: { DEBUG: 'egg-bin' } }) - .debug() + // .debug() + .end(); + assert(/ts-node@8\.10\.2/.test(stderr)); + assert.equal(code, 0); + }); + + it('should load custom ts compiler with tscompiler args', async () => { + const cwd = path.join(__dirname, './fixtures/example-ts-custom-compiler-2'); + + // install custom ts-node + await rimraf(path.join(cwd, 'node_modules')); + await exec('npx cnpm install ts-node@8.10.2 --no-save', { cwd }); + + // copy egg to node_modules + await cpy( + path.join(__dirname, './fixtures/example-ts-cluster/node_modules/egg'), + path.join(cwd, './node_modules/egg') + ); + + const { stderr, code } = await coffee.fork(eggBin, [ + 'dev', '--ts', '--tscompiler=ts-node/register', + ], { cwd, env: { DEBUG: 'egg-bin' } }) + // .debug() .end(); assert(/ts-node@8\.10\.2/.test(stderr)); assert.equal(code, 0); }); + it('should not load custom ts compiler without tscompiler args', async () => { + const cwd = path.join(__dirname, './fixtures/example-ts-custom-compiler-2'); + + // install custom ts-node + await rimraf(path.join(cwd, 'node_modules')); + await exec('npx cnpm install ts-node@8.10.2 --no-save', { cwd }); + + // copy egg to node_modules + await cpy( + path.join(__dirname, './fixtures/example-ts-cluster/node_modules/egg'), + path.join(cwd, './node_modules/egg') + ); + + const { stderr, code } = await coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd, env: { DEBUG: 'egg-bin' } }) + // .debug() + .end(); + assert(!/ts-node@8\.10\.2/.test(stderr)); + assert(/ts-node@7\.\d+\.\d+/.test(stderr)); + assert.equal(code, 0); + }); + it('should start app with other tscompiler without error', () => { return coffee.fork(eggBin, [ 'dev', '--ts', '--tscompiler=esbuild-register' ], { cwd: path.join(__dirname, './fixtures/example-ts'), }) // .debug() .expect('stdout', /agent.options.typescript = true/) - .expect('stdout', /agent.options.tscompiler = esbuild-register/) + .expect('stdout', /agent.options.tscompiler =/) + .expect('stdout', /esbuild-register/) .expect('stdout', /started/) .expect('code', 0) .end(); @@ -236,7 +288,8 @@ describe('test/ts.test.js', () => { }) // .debug() .expect('stdout', /agent.options.typescript = true/) - .expect('stdout', /agent.options.tscompiler = esbuild-register/) + .expect('stdout', /agent.options.tscompiler =/) + .expect('stdout', /esbuild-register/) .expect('stdout', /started/) .expect('code', 0) .end(); @@ -251,6 +304,27 @@ describe('test/ts.test.js', () => { .end(); }); + it('should test with custom ts compiler without error', async () => { + const cwd = path.join(__dirname, './fixtures/example-ts-custom-compiler'); + + // install custom ts-node + await rimraf(path.join(cwd, 'node_modules')); + await exec('npx cnpm install', { cwd }); + + // copy egg to node_modules + await cpy( + path.join(__dirname, './fixtures/example-ts-cluster/node_modules/egg'), + path.join(cwd, './node_modules/egg') + ); + + const { stdout, code } = await coffee.fork(eggBin, [ 'test', '--ts' ], { cwd, env: { DEBUG: 'egg-bin' } }) + // .debug() + .end(); + assert(/ts-node@8\.10\.2/.test(stdout)); + assert(!/ts-node@7\.\d+\.\d+/.test(stdout)); + assert.equal(code, 0); + }); + it('should cov app', () => { return coffee.fork(eggBin, [ 'cov' ], { cwd }) // .debug()