diff --git a/README.md b/README.md index 545f8700..a3933d06 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/command.js b/lib/command.js index a52b8758..5e46f2e6 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,6 +1,7 @@ 'use strict'; const path = require('path'); +const fs = require('fs'); const BaseCommand = require('common-bin'); class Command extends BaseCommand { @@ -24,7 +25,7 @@ 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; @@ -32,6 +33,16 @@ class Command extends BaseCommand { // 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 || []; diff --git a/test/fixtures/example-ts-pkg/app.ts b/test/fixtures/example-ts-pkg/app.ts new file mode 100644 index 00000000..a063672b --- /dev/null +++ b/test/fixtures/example-ts-pkg/app.ts @@ -0,0 +1,7 @@ +'use strict'; + +import { Application } from 'egg'; + +export default (app: Application) => { + console.log(`hi, egg, ${app.config.keys}`); +}; diff --git a/test/fixtures/example-ts-pkg/app/router.ts b/test/fixtures/example-ts-pkg/app/router.ts new file mode 100644 index 00000000..46c83b7d --- /dev/null +++ b/test/fixtures/example-ts-pkg/app/router.ts @@ -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`; + }); +}; \ No newline at end of file diff --git a/test/fixtures/example-ts-pkg/config/config.default.ts b/test/fixtures/example-ts-pkg/config/config.default.ts new file mode 100644 index 00000000..446264a9 --- /dev/null +++ b/test/fixtures/example-ts-pkg/config/config.default.ts @@ -0,0 +1,7 @@ +'use strict'; + +export default () => { + const config: any = {}; + config.keys = '123456'; + return config; +}; diff --git a/test/fixtures/example-ts-pkg/node_modules/egg/index.js b/test/fixtures/example-ts-pkg/node_modules/egg/index.js new file mode 100644 index 00000000..d1757bf0 --- /dev/null +++ b/test/fixtures/example-ts-pkg/node_modules/egg/index.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = require('../../../../../node_modules/egg'); + +setTimeout(() => { + console.log('exit by master test end') + process.exit(0); +}, 6000); diff --git a/test/fixtures/example-ts-pkg/node_modules/egg/package.json b/test/fixtures/example-ts-pkg/node_modules/egg/package.json new file mode 100644 index 00000000..6697ad3f --- /dev/null +++ b/test/fixtures/example-ts-pkg/node_modules/egg/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg" +} diff --git a/test/fixtures/example-ts-pkg/package.json b/test/fixtures/example-ts-pkg/package.json new file mode 100644 index 00000000..9db0d2e2 --- /dev/null +++ b/test/fixtures/example-ts-pkg/package.json @@ -0,0 +1,6 @@ +{ + "name": "example-ts-pkg", + "egg": { + "typescript": true + } +} \ No newline at end of file diff --git a/test/fixtures/example-ts-pkg/test/index.test.ts b/test/fixtures/example-ts-pkg/test/index.test.ts new file mode 100644 index 00000000..25c83639 --- /dev/null +++ b/test/fixtures/example-ts-pkg/test/index.test.ts @@ -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; + 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); + }); +}); diff --git a/test/fixtures/example-ts-pkg/tsconfig.json b/test/fixtures/example-ts-pkg/tsconfig.json new file mode 100644 index 00000000..4f10abf7 --- /dev/null +++ b/test/fixtures/example-ts-pkg/tsconfig.json @@ -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 + }, +} \ No newline at end of file diff --git a/test/ts.test.js b/test/ts.test.js index eea43a0b..cf77db6e 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -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(); + }); }); });