Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should only read pkg if argv.typescript not pass #97

Merged
merged 2 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥不是 false

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • yargs 的 bool 默认是 false,除非配置 undefined
  • ci 的时候,是构建 js 后,再跑 cov 的,此时 typescript = false
  • 按之前的逻辑就会去读取 pkg,拿到 typescript = true,在 egg-core 里面就挂掉了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argv.typescript 为 true / false 的时候, 即显式设置时,都不能读取 pkg,所以只能支持 undefined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配了 pkg 了为啥还要传参

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是 cov 那个问题,那天群里面说,线上真实运行的是编译后的 js,所以 ci 的测试,也要针对构建后的 js 进行测试,所以这时候的 cov 要传递 --no-ts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

编译后的和 extension 其实是一样的

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所以我们 cov 和 ci 都是走 ts-node ?

},
};
}
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