diff --git a/lib/command.js b/lib/command.js index 63f2ed1f..544e32b7 100644 --- a/lib/command.js +++ b/lib/command.js @@ -19,6 +19,7 @@ class Command extends BaseCommand { description: 'whether enable typescript support, will load `ts-node/register` etc', type: 'boolean', alias: 'ts', + default: undefined, }, }; } @@ -33,14 +34,16 @@ class Command extends BaseCommand { // remove unuse args argv.$0 = undefined; - // read `egg.typescript` from package.json - let baseDir = argv._[0] || argv.baseDir || cwd; - if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir); - const pkgFile = path.join(baseDir, 'package.json'); - if (fs.existsSync(pkgFile)) { - const pkgInfo = require(pkgFile); - if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) { - argv.typescript = true; + // read `egg.typescript` from package.json if not pass argv + if (argv.typescript === undefined) { + let baseDir = argv._[0] || argv.baseDir || cwd; + if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir); + const pkgFile = path.join(baseDir, 'package.json'); + if (fs.existsSync(pkgFile)) { + const pkgInfo = require(pkgFile); + if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript === true) { + argv.typescript = true; + } } } diff --git a/test/fixtures/example-ts-pkg/agent.js b/test/fixtures/example-ts-pkg/agent.js new file mode 100644 index 00000000..2fbe626b --- /dev/null +++ b/test/fixtures/example-ts-pkg/agent.js @@ -0,0 +1,6 @@ +'use strict'; + + +module.exports = agent => { + console.log(`agent.options.typescript = ${agent.options.typescript}`); +}; diff --git a/test/ts.test.js b/test/ts.test.js index 0938b4a3..bff49e3d 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -90,6 +90,15 @@ describe('test/ts.test.js', () => { .end(); }); + it('should fail start app with --no-ts', () => { + return coffee.fork(eggBin, [ 'dev', '--no-ts' ], { cwd }) + // .debug() + .expect('stdout', /agent.options.typescript = false/) + .expect('stdout', /started/) + .expect('code', 0) + .end(); + }); + it('should start app with relative path', () => { return coffee.fork(eggBin, [ 'dev', './example-ts-pkg' ], { cwd: path.dirname(cwd) }) // .debug()