Skip to content

Commit

Permalink
feat: add typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Mar 26, 2018
1 parent 3bf9b72 commit dd0a889
Show file tree
Hide file tree
Showing 21 changed files with 216 additions and 13 deletions.
1 change: 1 addition & 0 deletions .autod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'co-mocha',
'intelli-espower-loader',
'power-assert',
'espower-typescript',
'ypkgfiles',
'eslint-plugin-eggache',
'eslint',
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
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
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 options = {
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('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
28 changes: 28 additions & 0 deletions lib/ts-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

require('ts-node').register({
typeCheck: true,
compilerOptions: {
target: 'es2017',
module: 'commonjs',
strict: true,
moduleResolution: 'node',
noImplicitAny: false,
experimentalDecorators: true,
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,
},
});
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@
},
"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",
"eslint": "^4.18.1",
"eslint": "^4.19.1",
"eslint-plugin-eggache": "^1.0.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",
Expand All @@ -37,7 +40,7 @@
"cross-env": "^3.1.3",
"egg": "^1.8.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 Down
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' };
7 changes: 7 additions & 0 deletions test/fixtures/ts/test/a.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

describe('a.test.js', () => {
it('should success', () => {
throw 'should not load js files';
});
});
3 changes: 3 additions & 0 deletions test/fixtures/ts/test/sub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

export default { name: 'egg from ts' };
17 changes: 17 additions & 0 deletions test/fixtures/ts/test/typescript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const assert = require('assert');

describe('typescript.test.ts', () => {
it('should success', () => {
const obj = require('./sub');
console.log('###', obj.default.name);
assert(obj.default.name === 'egg from ts');
});

it('should fail', () => {
const obj = require('./sub');
console.log('###', obj.default.name);
assert(obj.default.name === 'wrong assert ts');
});
});
2 changes: 1 addition & 1 deletion test/lib/cmd/cov.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('test/lib/cmd/cov.test.js', () => {
it('should warn when require intelli-espower-loader', done => {
mm(process.env, 'TESTS', 'test/power-assert-fail.js');
coffee.fork(eggBin, [ 'cov', '-r', 'intelli-espower-loader' ], { cwd })
// .debug()
.debug()
.expect('stderr', /manually require `intelli-espower-loader`/)
.expect('stdout', /1\) should fail/)
.expect('stdout', /1 failing/)
Expand Down
10 changes: 10 additions & 0 deletions test/lib/cmd/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,14 @@ describe('test/lib/cmd/dev.test.js', () => {
.expect('code', 0)
.end(done);
});

it('should support --require', () => {
const script = path.join(__dirname, '../../fixtures/require-script');
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'dev', '--require', script ], { cwd })
// .debug()
.expect('stdout', /hey, you require me by --require/)
.expect('code', 0)
.end();
});
});
34 changes: 34 additions & 0 deletions test/ts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const path = require('path');
const coffee = require('coffee');
const mm = require('mm');

describe('test/ts.test.js', () => {
const eggBin = require.resolve('../bin/egg-bin');
const cwd = path.join(__dirname, './fixtures/ts');

afterEach(mm.restore);

it('should support ts', () => {
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'dev', '--typescript' ], { cwd })
.debug()
.expect('stdout', /### egg from ts/)
.expect('stdout', /options.typescript=true/)
.expect('stdout', /egg started/)
.expect('code', 0)
.end();
});

it('should support ts test', () => {
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd })
.debug()
.notExpect('stdout', /false == true/)
.notExpect('stdout', /should not load js files/)
.expect('stdout', /--- \[string\] 'wrong assert ts'/)
.expect('code', 1)
.end();
});
});

0 comments on commit dd0a889

Please sign in to comment.