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 typescript #89

Merged
merged 20 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from 17 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: 2 additions & 0 deletions .autod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ module.exports = {
'co-mocha',
'intelli-espower-loader',
'power-assert',
'espower-typescript',
'ypkgfiles',
],
devdep: [
'egg',
'autod',
'eslint-config-egg',
'egg-ci',
Expand Down
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
node_modules/
coverage/
!test/fixtures/custom-framework-app/node_modules/
test/fixtures/custom-framework-app/node_modules/

test/fixtures/demo-app/node_modules/aliyun-egg/
!test/fixtures/demo-app/node_modules/aliyun-egg/node_modules/

test/fixtures/ts/node_modules/aliyun-egg/
!test/fixtures/ts/node_modules/aliyun-egg/node_modules/

!test/fixtures/test-files-glob/**
!test/fixtures/test-files-stack/node_modules/
!test/fixtures/example/node_modules/


**/run/*.json
.tmp
.vscode
Expand Down
12 changes: 11 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ language: node_js
node_js:
- '6'
- '8'
env:
- EGG_VERSION=1
- EGG_VERSION=2
matrix:
exclude:
- node_js: '6'
env: EGG_VERSION=2
install:
- npm i npminstall && npminstall
- npm i npminstall
- sed -i.bak '/"egg":/d' package.json
- npminstall -d
script:
- eval "npminstall -d egg@$EGG_VERSION"
- npm run ci
after_script:
- npminstall codecov && codecov
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ $ 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`.
- `--require` will add to `execArgv`, support multiple.
Copy link
Member

Choose a reason for hiding this comment

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

这些 flag 能否支持一下在 package.json 中配置,这样就不需要在每个 scripts 中都去加 --ts 了

Copy link
Member

Choose a reason for hiding this comment

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

其实还期望能否有 --require.once 这种 flag,就是能够在跑 egg 应用前跑一段 js,--require 是每起一个 child_process 也都会运行一次,如果我想在跑 egg 前,跑生成 d.ts 的程序(并且 watch 文件更改重新生成 d.ts),其实是不需要每个子进程都跑一次这个的,不然就起了一堆的 watcher 了

Copy link
Member Author

Choose a reason for hiding this comment

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

  • --require 是加到 execArgv 的,这个在 fork 里面就会默认继承的,所以肯定会跑一次。
  • package.json 那个可以做,下个 PR 看看

Copy link
Member

Choose a reason for hiding this comment

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

是啊,--require 是全部会继承的,就是说能否提供个能力,只运行一次的 require?比如在运行 egg 之前跑一次 tsc,或者跑一次 babel parse,这样就不需要在 scripts 中写 tsc && egg-bin dev 或者 tsc && egg-bin test 了,再加上支持配置在 package.json 中的话,那 scripts 中就只需要 egg-bin devegg-bin test

Copy link
Member Author

Choose a reason for hiding this comment

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

这个 PR 做完后,就不需要 tsc 了啊,不是有 ts-node 了么。

你这个需求感觉更类似于 npm scripts 本身的 beforeTest / beforeStart 的 hook

Copy link
Member

Choose a reason for hiding this comment

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

我知道,只是举个 tsc 的例子,对,就是类似于 beforeTest,但是 npm scripts 没有通用的 before,所以想 egg-bin 能否提供

Copy link
Member Author

Choose a reason for hiding this comment

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

这个不在这个 PR 的范畴内,等这个完后,考虑如何集成你的 ets 的时候再考虑。

Copy link
Member

Choose a reason for hiding this comment

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

OK

Copy link
Member

Choose a reason for hiding this comment

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

tsc 要的,发布构建需要。

Copy link
Member Author

Choose a reason for hiding this comment

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

tsc 是构建发布那个流程的,这块是开发期辅助,不冲突。
构建那个不会存在他说的 require


### debug

Expand Down Expand Up @@ -130,6 +132,7 @@ You can pass any mocha argv.
- `--grep` only run tests matching <pattern>
- `--timeout` milliseconds, default to 30000
- `--full-trace` display the full stack trace, default to false.
- `--typescript` / `--ts` enable typescript support, default to `false`.
- see more at https://mochajs.org/#usage

#### environment
Expand Down
1 change: 0 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '8'

install:
Expand Down
17 changes: 17 additions & 0 deletions lib/cmd/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,30 @@ class DevCommand extends Command {
description: 'specify framework that can be absolute path or npm package',
type: 'string',
},
require: {
description: 'will add to execArgv --require',
type: 'array',
alias: 'r',
},
};
}

get description() {
return 'Start server at local dev mode';
}

get context() {
const context = super.context;
const { argv, execArgvObj } = context;
execArgvObj.require = execArgvObj.require || [];
// add require to execArgv
if (argv.require) {
execArgvObj.require.push(...argv.require);
argv.require = undefined;
}
return context;
}

* run(context) {
const devArgs = yield this.formatArgs(context);
const env = {
Expand Down
9 changes: 8 additions & 1 deletion lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ class TestCommand extends Command {
requireArr.push(require.resolve('intelli-espower-loader'));
}

// for power-assert
if (testArgv.typescript) {
requireArr.push(require.resolve('espower-typescript/guess'));
}

testArgv.require = requireArr;

// collect test files
let files = testArgv._.slice();
if (!files.length) {
files = [ process.env.TESTS || 'test/**/*.test.js' ];
files = [ process.env.TESTS || `test/**/*.test.${testArgv.typescript ? 'ts' : 'js'}` ];
}

// expand glob and skip node_modules and fixtures
files = globby.sync(files.concat('!test/**/{fixtures, node_modules}/**/*.test.js'));
files.sort();
Expand All @@ -108,6 +114,7 @@ class TestCommand extends Command {
testArgv.r = undefined;
testArgv.t = undefined;
testArgv.g = undefined;
testArgv.typescript = undefined;

return this.helper.unparseArgv(testArgv);
}
Expand Down
22 changes: 20 additions & 2 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

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

class Command extends BaseCommand {
Expand All @@ -9,16 +10,33 @@ class Command extends BaseCommand {
execArgv: true,
removeAlias: true,
};

// common-bin setter, don't care about override at sub class
// https://github.com/node-modules/common-bin/blob/master/lib/command.js#L158
this.options = {
typescript: {
description: 'whether enable typescript support, will load `ts-node/register` etc',
type: 'boolean',
alias: 'ts',
},
};
}

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

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

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

// execArgv
if (argv.typescript) {
execArgvObj.require = execArgvObj.require || [];
execArgvObj.require.push(path.join(__dirname, './ts-helper.js'));
}

return context;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/ts-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

require('ts-node').register({
Copy link
Member

Choose a reason for hiding this comment

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

这些配置和 tsconfig 啥关系

Copy link
Member Author

Choose a reason for hiding this comment

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

这里提供默认值,应用层 tsconfig.json 可以覆盖

Copy link
Member

Choose a reason for hiding this comment

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

那最好只读 tsconfig.json,这个默认配置和 ts-node 的有差异?

Copy link
Member Author

Choose a reason for hiding this comment

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

这里的配置是针对 Node 应用的,ts-node 会考虑到前端应用,所以默认值不一样。

Copy link
Member Author

@atian25 atian25 Mar 27, 2018

Choose a reason for hiding this comment

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

最新的已经去掉默认值了。开发者自己配置

typeCheck: true,
});
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,36 @@
},
"dependencies": {
"autod": "^3.0.1",
"chalk": "^2.3.1",
"co-mocha": "^1.2.1",
"common-bin": "^2.7.1",
"chalk": "^2.3.2",
"co-mocha": "^1.2.2",
"common-bin": "^2.7.2",
"debug": "^3.1.0",
"detect-port": "^1.2.2",
"egg-utils": "^2.3.0",
"espower-typescript": "^8.1.3",
"globby": "^8.0.1",
"inspector-proxy": "^1.2.1",
"intelli-espower-loader": "^1.0.1",
"mocha": "^5.0.1",
"mocha": "^5.0.5",
"mz-modules": "^2.1.0",
"nyc": "^11.4.1",
"nyc": "^11.6.0",
"power-assert": "^1.4.4",
"semver": "^5.5.0",
"test-exclude": "^4.2.0",
"test-exclude": "^4.2.1",
"ts-node": "^5.0.1",
"ypkgfiles": "^1.5.0"
},
"devDependencies": {
"@types/mocha": "^5.0.0",
"autod": "^3.0.1",
"babel": "^6.3.26",
"babel-preset-airbnb": "^1.0.1",
"babel-register": "^6.4.3",
"coffee": "^4.1.0",
"cross-env": "^3.1.3",
"egg": "^1.8.0",
"egg": "^2.5.0",
"egg-ci": "^1.8.0",
"egg-mock": "^3.14.0",
"egg-mock": "^3.15.1",
"enzyme": "^2.0.0",
"eslint": "^4.12.1",
"eslint-config-egg": "^7.0.0",
Expand All @@ -45,7 +48,8 @@
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
"react-dom": "^0.14.7",
"semver": "^5.4.1"
"semver": "^5.4.1",
"typescript": "^2.7.2"
},
"repository": {
"type": "git",
Expand All @@ -63,7 +67,7 @@
"test-local": "node bin/egg-bin.js test -t 3600000",
"cov": "nyc -r lcov -r text-summary npm run test-local",
"ci-test-only": "TESTS=test/lib/cmd/cov.test.js npm run test-local",
"ci": "npm run lint && npm run pkgfiles -- --check && npm run autod -- --check && npm run ci-test-only && npm run cov",
"ci": "npm run lint && npm run pkgfiles -- --check && npm run ci-test-only && npm run cov",
"autod": "node bin/egg-bin.js autod"
},
"engines": {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/example-ts/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/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/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/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/node_modules/egg/package.json

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/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "example-ts"
}
21 changes: 21 additions & 0 deletions test/fixtures/example-ts/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);
});
});
25 changes: 25 additions & 0 deletions test/fixtures/example-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"strict": true,
"moduleResolution": "node",
"noImplicitAny": false,
"experimentalDecorators": true,
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

Choose a reason for hiding this comment

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

已经精简了,将跟默认值一样的值去掉了

"emitDecoratorMetadata": true,
"charset": "utf8",
"allowJs": false,
"pretty": true,
"noEmitOnError": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"strictPropertyInitialization": false,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"inlineSourceMap": true,
"importHelpers": true
},
}
3 changes: 3 additions & 0 deletions test/fixtures/require-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log('hey, you require me by --require');
6 changes: 6 additions & 0 deletions test/fixtures/ts/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';


module.exports = app => {
app.logger.info('###', require('./test.ts').default.name);
};
3 changes: 3 additions & 0 deletions test/fixtures/ts/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.key = '12345';
20 changes: 20 additions & 0 deletions test/fixtures/ts/node_modules/aliyun-egg/index.js

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/ts/node_modules/aliyun-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/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ts",
"egg": {
"framework": "aliyun-egg"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/ts/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export default { name: 'egg from ts' };
Loading