Skip to content

Commit

Permalink
fix: should only read pkg if argv.typescript not pass
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Apr 4, 2018
1 parent 4b798d9 commit e0e7fee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
}
Expand All @@ -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;
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/example-ts-pkg/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';


module.exports = agent => {
console.log(`agent.options.typescript = ${agent.options.typescript}`);
};
9 changes: 9 additions & 0 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit e0e7fee

Please sign in to comment.