Skip to content

Commit

Permalink
feat: support egg.typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Mar 28, 2018
1 parent 7952f6c commit 7cf8774
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 6 deletions.
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`.
- `--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
},
}
37 changes: 33 additions & 4 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('test/ts.test.js', () => {
cwd = path.join(__dirname, './fixtures/ts');
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'dev', '--typescript' ], { cwd })
.debug()
// .debug()
.expect('stdout', /### egg from ts/)
.expect('stdout', /options.typescript=true/)
.expect('stdout', /started/)
Expand All @@ -26,7 +26,7 @@ describe('test/ts.test.js', () => {
cwd = path.join(__dirname, './fixtures/ts');
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'test', '--typescript' ], { cwd })
.debug()
// .debug()
.notExpect('stdout', /false == true/)
.notExpect('stdout', /should not load js files/)
.expect('stdout', /--- \[string\] 'wrong assert ts'/)
Expand All @@ -41,7 +41,7 @@ describe('test/ts.test.js', () => {
}
cwd = path.join(__dirname, './fixtures/example-ts');
return coffee.fork(eggBin, [ 'dev', '--ts' ], { cwd })
.debug()
// .debug()
.expect('stdout', /hi, egg, 12345/)
.expect('stdout', /started/)
.expect('code', 0)
Expand All @@ -55,10 +55,39 @@ describe('test/ts.test.js', () => {
}
cwd = path.join(__dirname, './fixtures/example-ts');
return coffee.fork(eggBin, [ 'test', '--ts' ], { cwd })
.debug()
// .debug()
.expect('stdout', /hi, egg, 123456/)
.expect('stdout', /should work/)
.expect('code', 0)
.end();
});

describe('egg.typescript = true', () => {
cwd = path.join(__dirname, './fixtures/example-ts-pkg');

it('should start app', () => {
if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') {
console.log('skip egg@1');
return;
}
return coffee.fork(eggBin, [ 'dev' ], { 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;
}
return coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
.expect('stdout', /hi, egg, 123456/)
.expect('code', 0)
.end();
});
});
});

0 comments on commit 7cf8774

Please sign in to comment.