Skip to content

Commit

Permalink
feat: support typescript (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored Mar 28, 2018
1 parent c48860c commit 75b5cd6
Show file tree
Hide file tree
Showing 32 changed files with 367 additions and 51 deletions.
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.

### 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({
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);
});
});
19 changes: 19 additions & 0 deletions test/fixtures/example-ts/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
},
}
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

0 comments on commit 75b5cd6

Please sign in to comment.