Skip to content

Commit

Permalink
fix: should only read pkg if argv.typescript not pass (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored Apr 5, 2018
1 parent 4b798d9 commit 1e93020
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
6 changes: 4 additions & 2 deletions lib/cmd/cov.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class CovCommand extends Command {

* run(context) {
const { cwd, argv, execArgv, env } = context;

if (argv.prerequire) {
env.EGG_BIN_PREREQUIRE = 'true';
}
Expand Down Expand Up @@ -73,7 +72,10 @@ class CovCommand extends Command {
const opt = {
cwd,
execArgv,
env: Object.assign({ NODE_ENV: 'test' }, env),
env: Object.assign({
NODE_ENV: 'test',
EGG_TYPESCRIPT: context.argv.typescript,
}, env),
};

// save coverage-xxxx.json to $PWD/coverage
Expand Down
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}`);
};
1 change: 0 additions & 1 deletion test/fixtures/example-ts-pkg/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { Application, Context } from 'egg';
import { default as mock, MockOption, BaseMockApplication } from 'egg-mock';
import * as path from 'path';

describe('test/index.test.ts', () => {
let app: BaseMockApplication<Application, Context>;
Expand Down
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 1e93020

Please sign in to comment.