Skip to content

Commit

Permalink
fix: using ts-node in app should check tscompiler and deps (#170)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
whxaxes committed Feb 17, 2022
1 parent cdc525f commit 662b9e9
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 26 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 12 additions & 4 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export const key = '12345';
3 changes: 3 additions & 0 deletions test/fixtures/example-ts-custom-compiler-2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "example-ts-custom-compiler-2"
}

This file was deleted.

This file was deleted.

7 changes: 5 additions & 2 deletions test/fixtures/example-ts-custom-compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"name": "example-ts-custom-compiler"
}
"name": "example-ts-custom-compiler",
"dependencies": {
"ts-node": "^8.10.2"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/example-ts-custom-compiler/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe('test', () => {
it('should ok', () => {
console.info(process.argv);
console.info(process.execArgv);
});
});
82 changes: 78 additions & 4 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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()
Expand Down

0 comments on commit 662b9e9

Please sign in to comment.