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

feat: support egg.typescript #92

Merged
merged 3 commits into from
Mar 30, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ $ egg-bin dev
- `--port` server port, default to `7001`.
- `--cluster` worker process number, skip this argvs will start only `1` worker, provide this without value will start `cpu` count worker.
- `--sticky` start a sticky cluster server, default to `false`.
- `--typescript` / `--ts` enable typescript support, default to `false`.
- `--typescript` / `--ts` enable typescript support, default to `false`. Also support read from `package.json`'s `egg.typescript`.
Copy link
Member

Choose a reason for hiding this comment

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

我记得有个通用的配置

Copy link
Member Author

Choose a reason for hiding this comment

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

哪里?

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 那个?

Copy link
Member Author

Choose a reason for hiding this comment

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

https://github.com/yargs/yargs/blob/master/docs/api.md#pkgconfkey-cwd

这个感觉不太好用,我们现在还没想好 egg.xx 里面有哪些是 bin 都会去读的。
egg.framework 在 test 里面就不需要,test 里面的 --require 在 dev 不需要。

this.yargs.pkgConf('egg'); 只能用在 dev 上,但 typescript 这个在 test 那边得自己读一次。

Copy link
Member

Choose a reason for hiding this comment

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

可能我搞错了,我记得原来 test/cov 一些环境变量配置可以配在 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.

mocha 和 nyc 的那个是它们自己支持的。

Copy link
Member Author

@atian25 atian25 Mar 29, 2018

Choose a reason for hiding this comment

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

后面我们可以支持读取 pkg.egg 来作为 dev / debug / test / cov 里面的 startCluster 启动参数。
不过不能用它这个方法,要自己做。

- `--require` will add to `execArgv`, support multiple.

### debug
Expand Down
13 changes: 12 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const fs = require('fs');
const BaseCommand = require('common-bin');

class Command extends BaseCommand {
Expand All @@ -24,14 +25,24 @@ class Command extends BaseCommand {

get context() {
const context = super.context;
const { argv, debugPort, execArgvObj } = context;
const { argv, debugPort, execArgvObj, cwd } = context;

// compatible
if (debugPort) context.debug = debugPort;

// remove unuse args
argv.$0 = undefined;

// read `egg.typescript` from package.json
const baseDir = argv._[0] || argv.baseDir || cwd;
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;
}
}

// execArgv
if (argv.typescript) {
execArgvObj.require = execArgvObj.require || [];
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/example-ts-pkg/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

import { Application } from 'egg';

export default (app: Application) => {
console.log(`hi, egg, ${app.config.keys}`);
};
9 changes: 9 additions & 0 deletions test/fixtures/example-ts-pkg/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

import { Application, Context } from 'egg';

export default (app: Application) => {
app.router.get('/', function* (this: Context) {
this.body = `hi, egg`;
});
};
7 changes: 7 additions & 0 deletions test/fixtures/example-ts-pkg/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

export default () => {
const config: any = {};
config.keys = '123456';
return config;
};
8 changes: 8 additions & 0 deletions test/fixtures/example-ts-pkg/node_modules/egg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/example-ts-pkg/node_modules/egg/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/fixtures/example-ts-pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "example-ts-pkg",
"egg": {
"typescript": true
}
}
21 changes: 21 additions & 0 deletions test/fixtures/example-ts-pkg/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

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>;
before(() => {
app = mock.app({ typescript: true } as MockOption);
return app.ready();
});
after(() => app.close());
it('should work', async () => {
await app
.httpRequest()
.get('/')
.expect('hi, egg')
.expect(200);
});
});
19 changes: 19 additions & 0 deletions test/fixtures/example-ts-pkg/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"strict": true,
"noImplicitAny": false,
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"pretty": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"inlineSourceMap": true,
"importHelpers": true
},
}
91 changes: 62 additions & 29 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,78 @@ describe('test/ts.test.js', () => {
.end();
});

it('should start app', () => {
describe('real application', () => {
if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') {
console.log('skip egg@1');
return;
}
cwd = path.join(__dirname, './fixtures/example-ts');
return coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd })
// .debug()
.expect('stdout', /hi, egg, 12345/)
.expect('stdout', /started/)
.expect('code', 0)
.end();
});

it('should test app', () => {
if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') {
console.log('skip egg@1');
return;
}
cwd = path.join(__dirname, './fixtures/example-ts');
return coffee.fork(eggBin, [ 'test', '--ts' ], { cwd })
// .debug()
.expect('stdout', /hi, egg, 123456/)
.expect('stdout', /should work/)
.expect('code', 0)
.end();
before(() => {
cwd = path.join(__dirname, './fixtures/example-ts');
});

it('should start app', () => {
return coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd })
// .debug()
.expect('stdout', /hi, egg, 12345/)
.expect('stdout', /started/)
.expect('code', 0)
.end();
});

it('should test app', () => {
return coffee.fork(eggBin, [ 'test', '--ts' ], { cwd })
// .debug()
.expect('stdout', /hi, egg, 123456/)
.expect('stdout', /should work/)
.expect('code', 0)
.end();
});

it('should cov app', () => {
return coffee.fork(eggBin, [ 'cov', '--ts' ], { cwd })
.debug()
.expect('stdout', /hi, egg, 123456/)
.expect('stdout', process.env.NYC_ROOT_ID ? /Coverage summary/ : /Statements.*100%/)
.expect('code', 0)
.end();
});
});

it('should cov app', () => {
describe('egg.typescript = true', () => {
if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') {
console.log('skip egg@1');
return;
}
cwd = path.join(__dirname, './fixtures/example-ts');
return coffee.fork(eggBin, [ 'cov', '--ts' ], { cwd })
.debug()
.expect('stdout', /hi, egg, 123456/)
.expect('stdout', process.env.NYC_ROOT_ID ? /Coverage summary/ : /Statements.*100%/)
.expect('code', 0)
.end();

before(() => {
cwd = path.join(__dirname, './fixtures/example-ts-pkg');
});

it('should start app', () => {
return coffee.fork(eggBin, [ 'dev' ], { cwd })
// .debug()
.expect('stdout', /hi, egg, 12345/)
.expect('stdout', /started/)
.expect('code', 0)
.end();
});

it('should test app', () => {
return coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
.expect('stdout', /hi, egg, 123456/)
.expect('code', 0)
.end();
});

it('should cov app', () => {
return coffee.fork(eggBin, [ 'cov' ], { cwd })
.debug()
.expect('stdout', /hi, egg, 123456/)
.expect('stdout', process.env.NYC_ROOT_ID ? /Coverage summary/ : /Statements.*100%/)
.expect('code', 0)
.end();
});
});
});