Skip to content

Commit

Permalink
feat: support egg.typescript (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and popomore committed Mar 30, 2018
1 parent 8a05e19 commit a7f0ca8
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 31 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
},
}
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();
});
});
});

0 comments on commit a7f0ca8

Please sign in to comment.