From 263cfd12c111de4b31cfc28d886bc1f8ece2082d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=96=E7=8C=A9?= Date: Fri, 15 Feb 2019 09:46:33 +0800 Subject: [PATCH] feat: intergration with egg-ts-helper (#123) --- .gitignore | 2 + README.md | 1 + lib/command.js | 23 +++++++- package.json | 1 + test/fixtures/example-ts-ets/app.ts | 17 ++++++ .../example-ts-ets/app/controller/home.ts | 17 ++++++ .../example-ts-ets/app/custom/test.ts | 5 ++ .../example-ts-ets/app/extend/application.ts | 5 ++ .../example-ts-ets/app/extend/context.ts | 5 ++ .../example-ts-ets/app/extend/helper.ts | 5 ++ .../example-ts-ets/app/extend/request.ts | 5 ++ .../example-ts-ets/app/extend/response.ts | 5 ++ .../example-ts-ets/app/middleware/access.ts | 5 ++ .../fixtures/example-ts-ets/app/model/User.ts | 7 +++ test/fixtures/example-ts-ets/app/router.ts | 8 +++ .../fixtures/example-ts-ets/app/service/db.ts | 7 +++ .../example-ts-ets/config/config.default.ts | 18 ++++++ .../example-ts-ets/config/config.local.ts | 14 +++++ .../example-ts-ets/config/config.prod.ts | 7 +++ test/fixtures/example-ts-ets/config/plugin.ts | 6 ++ .../example-ts-ets/node_modules/egg/index.js | 8 +++ .../node_modules/egg/package.json | 3 + test/fixtures/example-ts-ets/package.json | 7 +++ test/fixtures/example-ts-ets/tsconfig.json | 21 +++++++ test/fixtures/example-ts-ets/tshelper.js | 19 ++++++ test/ts.test.js | 59 ++++++++++++++++++- 26 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/example-ts-ets/app.ts create mode 100644 test/fixtures/example-ts-ets/app/controller/home.ts create mode 100644 test/fixtures/example-ts-ets/app/custom/test.ts create mode 100644 test/fixtures/example-ts-ets/app/extend/application.ts create mode 100644 test/fixtures/example-ts-ets/app/extend/context.ts create mode 100644 test/fixtures/example-ts-ets/app/extend/helper.ts create mode 100644 test/fixtures/example-ts-ets/app/extend/request.ts create mode 100644 test/fixtures/example-ts-ets/app/extend/response.ts create mode 100644 test/fixtures/example-ts-ets/app/middleware/access.ts create mode 100644 test/fixtures/example-ts-ets/app/model/User.ts create mode 100644 test/fixtures/example-ts-ets/app/router.ts create mode 100644 test/fixtures/example-ts-ets/app/service/db.ts create mode 100644 test/fixtures/example-ts-ets/config/config.default.ts create mode 100644 test/fixtures/example-ts-ets/config/config.local.ts create mode 100644 test/fixtures/example-ts-ets/config/config.prod.ts create mode 100644 test/fixtures/example-ts-ets/config/plugin.ts create mode 100644 test/fixtures/example-ts-ets/node_modules/egg/index.js create mode 100644 test/fixtures/example-ts-ets/node_modules/egg/package.json create mode 100644 test/fixtures/example-ts-ets/package.json create mode 100644 test/fixtures/example-ts-ets/tsconfig.json create mode 100644 test/fixtures/example-ts-ets/tshelper.js diff --git a/.gitignore b/.gitignore index 69e56212..6a505f28 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,8 @@ test/fixtures/ts/node_modules/aliyun-egg/ !test/fixtures/example/node_modules/ !test/fixtures/example-ts-error-stack/node_modules/ !test/fixtures/egg-require/node_modules/ +test/fixtures/example-ts-ets/typings/ +!test/fixtures/example-ts-ets/node_modules/ **/run/*.json diff --git a/README.md b/README.md index 307b1db6..06f28a8b 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ $ egg-bin dev - `--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`. Also support read from `package.json`'s `egg.typescript`. +- `--declarations` / `--dts` enable [egg-ts-helper](https://github.com/whxaxes/egg-ts-helper) support, default to `false`. Also support read from `package.json`'s `egg.declarations`. - `--require` will add to `execArgv`, support multiple. Also support read from `package.json`'s `egg.require` ### debug diff --git a/lib/command.js b/lib/command.js index 7d05ddd3..fd8c5926 100644 --- a/lib/command.js +++ b/lib/command.js @@ -21,6 +21,13 @@ class Command extends BaseCommand { alias: 'ts', default: undefined, }, + + declarations: { + description: 'whether create dts, will load `egg-ts-helper/register`', + type: 'boolean', + alias: 'dts', + default: undefined, + }, }; } @@ -43,8 +50,13 @@ class Command extends BaseCommand { execArgvObj.require = execArgvObj.require || []; // read `egg.typescript` from package.json if not pass argv - if (argv.typescript === undefined && eggInfo && eggInfo.typescript === true) { - argv.typescript = true; + if (argv.typescript === undefined && eggInfo) { + argv.typescript = eggInfo.typescript === true; + } + + // read `egg.declarations` from package.json if not pass argv + if (argv.declarations === undefined && eggInfo) { + argv.declarations = eggInfo.declarations === true; } // read `egg.require` from package.json @@ -52,7 +64,7 @@ class Command extends BaseCommand { execArgvObj.require = execArgvObj.require.concat(eggInfo.require); } - // execArgv + // load ts-node if (argv.typescript) { execArgvObj.require.push(require.resolve('ts-node/register')); @@ -66,6 +78,11 @@ class Command extends BaseCommand { env.TS_NODE_FILES = process.env.TS_NODE_FILES || 'true'; } + // load egg-ts-helper + if (argv.declarations) { + execArgvObj.require.push(require.resolve('egg-ts-helper/register')); + } + return context; } } diff --git a/package.json b/package.json index 9d379bf3..252a251c 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "common-bin": "^2.7.3", "debug": "^3.1.0", "detect-port": "^1.2.3", + "egg-ts-helper": "^1.22.0", "egg-utils": "^2.4.0", "espower-typescript": "^9.0.1", "globby": "^8.0.1", diff --git a/test/fixtures/example-ts-ets/app.ts b/test/fixtures/example-ts-ets/app.ts new file mode 100644 index 00000000..330e6a8e --- /dev/null +++ b/test/fixtures/example-ts-ets/app.ts @@ -0,0 +1,17 @@ +import * as path from 'path'; + +export default (app: Egg.Application) => { + let directory = path.resolve(app.baseDir, './app/model'); + app.loader.loadToApp(directory, 'model', { + caseStyle: 'upper', + directory, + }); + + directory = path.resolve(app.baseDir, './app/custom'); + app.loader.loadToApp(directory, 'custom', { + caseStyle: 'lower', + directory, + }); + + app.customLog(); +}; diff --git a/test/fixtures/example-ts-ets/app/controller/home.ts b/test/fixtures/example-ts-ets/app/controller/home.ts new file mode 100644 index 00000000..edce9c7e --- /dev/null +++ b/test/fixtures/example-ts-ets/app/controller/home.ts @@ -0,0 +1,17 @@ +import { Controller } from 'egg'; + +export default class HomeController extends Controller { + async index() { + const { ctx, app } = this; + ctx.customLog(); + app.customLog(); + ctx.request.customLog(); + ctx.response.customLog(); + ctx.helper.customLog(); + console.info(app.config.otherBizConfig.type); + console.info(await ctx.service.db.fetch()); + console.info(await app.model.User.get()); + console.info('biz config', ctx.app.config.biz.type); + ctx.body = 'ok'; + } +} diff --git a/test/fixtures/example-ts-ets/app/custom/test.ts b/test/fixtures/example-ts-ets/app/custom/test.ts new file mode 100644 index 00000000..16e9b857 --- /dev/null +++ b/test/fixtures/example-ts-ets/app/custom/test.ts @@ -0,0 +1,5 @@ +export default () => { + return { + abc: '123', + }; +}; diff --git a/test/fixtures/example-ts-ets/app/extend/application.ts b/test/fixtures/example-ts-ets/app/extend/application.ts new file mode 100644 index 00000000..ce563f1e --- /dev/null +++ b/test/fixtures/example-ts-ets/app/extend/application.ts @@ -0,0 +1,5 @@ +export default { + customLog() { + console.info('application log'); + }, +}; diff --git a/test/fixtures/example-ts-ets/app/extend/context.ts b/test/fixtures/example-ts-ets/app/extend/context.ts new file mode 100644 index 00000000..b7f7282d --- /dev/null +++ b/test/fixtures/example-ts-ets/app/extend/context.ts @@ -0,0 +1,5 @@ +export default { + customLog() { + console.info('context log'); + }, +}; diff --git a/test/fixtures/example-ts-ets/app/extend/helper.ts b/test/fixtures/example-ts-ets/app/extend/helper.ts new file mode 100644 index 00000000..67d34cf8 --- /dev/null +++ b/test/fixtures/example-ts-ets/app/extend/helper.ts @@ -0,0 +1,5 @@ +export default { + customLog() { + console.info('helper log'); + }, +}; diff --git a/test/fixtures/example-ts-ets/app/extend/request.ts b/test/fixtures/example-ts-ets/app/extend/request.ts new file mode 100644 index 00000000..28f6c5ff --- /dev/null +++ b/test/fixtures/example-ts-ets/app/extend/request.ts @@ -0,0 +1,5 @@ +export default { + customLog() { + console.info('request log'); + }, +}; diff --git a/test/fixtures/example-ts-ets/app/extend/response.ts b/test/fixtures/example-ts-ets/app/extend/response.ts new file mode 100644 index 00000000..42d25486 --- /dev/null +++ b/test/fixtures/example-ts-ets/app/extend/response.ts @@ -0,0 +1,5 @@ +export default { + customLog() { + console.info('response log'); + }, +}; diff --git a/test/fixtures/example-ts-ets/app/middleware/access.ts b/test/fixtures/example-ts-ets/app/middleware/access.ts new file mode 100644 index 00000000..bf38c3d7 --- /dev/null +++ b/test/fixtures/example-ts-ets/app/middleware/access.ts @@ -0,0 +1,5 @@ +export default () => { + return async (_ctx, next) => { + await next(); + }; +}; diff --git a/test/fixtures/example-ts-ets/app/model/User.ts b/test/fixtures/example-ts-ets/app/model/User.ts new file mode 100644 index 00000000..c68ac2b2 --- /dev/null +++ b/test/fixtures/example-ts-ets/app/model/User.ts @@ -0,0 +1,7 @@ +export default function() { + return { + get() { + return 'model get'; + }, + }; +} diff --git a/test/fixtures/example-ts-ets/app/router.ts b/test/fixtures/example-ts-ets/app/router.ts new file mode 100644 index 00000000..d64e067f --- /dev/null +++ b/test/fixtures/example-ts-ets/app/router.ts @@ -0,0 +1,8 @@ +import { Application } from 'egg'; + +export default function(app: Application) { + const { router, controller } = app; + + console.info(app.custom.test.abc); + router.get('/', controller.home.index); +} diff --git a/test/fixtures/example-ts-ets/app/service/db.ts b/test/fixtures/example-ts-ets/app/service/db.ts new file mode 100644 index 00000000..5cec398f --- /dev/null +++ b/test/fixtures/example-ts-ets/app/service/db.ts @@ -0,0 +1,7 @@ +import { Service } from 'egg'; + +export default class DbService extends Service { + async fetch() { + return 'service fetch'; + } +} diff --git a/test/fixtures/example-ts-ets/config/config.default.ts b/test/fixtures/example-ts-ets/config/config.default.ts new file mode 100644 index 00000000..dae5d3be --- /dev/null +++ b/test/fixtures/example-ts-ets/config/config.default.ts @@ -0,0 +1,18 @@ +export default function() { + // built-in config + const config: Egg.PowerPartial = {}; + + config.keys = '123123'; + + // biz config + const bizConfig = { + biz: { + type: 'biz', + }, + }; + + return { + ...config as {}, + ...bizConfig, + }; +} diff --git a/test/fixtures/example-ts-ets/config/config.local.ts b/test/fixtures/example-ts-ets/config/config.local.ts new file mode 100644 index 00000000..cbe5ea0c --- /dev/null +++ b/test/fixtures/example-ts-ets/config/config.local.ts @@ -0,0 +1,14 @@ +import { EggAppConfig, PowerPartial } from 'egg'; + +export default function() { + // built-in config + const config: PowerPartial = {}; + + config.keys = '123123'; + + config.biz = { + type: 'local', + }; + + return config; +} diff --git a/test/fixtures/example-ts-ets/config/config.prod.ts b/test/fixtures/example-ts-ets/config/config.prod.ts new file mode 100644 index 00000000..cc0a4ad0 --- /dev/null +++ b/test/fixtures/example-ts-ets/config/config.prod.ts @@ -0,0 +1,7 @@ +export default function() { + return { + otherBizConfig: { + type: 'value', + }, + }; +} diff --git a/test/fixtures/example-ts-ets/config/plugin.ts b/test/fixtures/example-ts-ets/config/plugin.ts new file mode 100644 index 00000000..1ac4403c --- /dev/null +++ b/test/fixtures/example-ts-ets/config/plugin.ts @@ -0,0 +1,6 @@ +import { EggPlugin } from 'egg'; + +const plugin: EggPlugin = { +}; + +export default plugin; diff --git a/test/fixtures/example-ts-ets/node_modules/egg/index.js b/test/fixtures/example-ts-ets/node_modules/egg/index.js new file mode 100644 index 00000000..7102f7f7 --- /dev/null +++ b/test/fixtures/example-ts-ets/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); +}, require('os').platform() === 'win32' ? 10000 : 6000); diff --git a/test/fixtures/example-ts-ets/node_modules/egg/package.json b/test/fixtures/example-ts-ets/node_modules/egg/package.json new file mode 100644 index 00000000..6697ad3f --- /dev/null +++ b/test/fixtures/example-ts-ets/node_modules/egg/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg" +} diff --git a/test/fixtures/example-ts-ets/package.json b/test/fixtures/example-ts-ets/package.json new file mode 100644 index 00000000..59a8b9a0 --- /dev/null +++ b/test/fixtures/example-ts-ets/package.json @@ -0,0 +1,7 @@ +{ + "name": "real-app", + "egg": { + "typescript": true, + "declarations": false + } +} \ No newline at end of file diff --git a/test/fixtures/example-ts-ets/tsconfig.json b/test/fixtures/example-ts-ets/tsconfig.json new file mode 100644 index 00000000..0f0b2d22 --- /dev/null +++ b/test/fixtures/example-ts-ets/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "strict": true, + "target": "es2017", + "module": "commonjs", + "moduleResolution": "node", + "noImplicitAny": false, + "baseUrl": ".", + "paths": { + "egg": [ + "../../../node_modules/egg" + ], + "egg-mock": [ + "../../../node_modules/egg-mock" + ] + } + }, + "include": [ + "./**/*" + ] +} diff --git a/test/fixtures/example-ts-ets/tshelper.js b/test/fixtures/example-ts-ets/tshelper.js new file mode 100644 index 00000000..daecd813 --- /dev/null +++ b/test/fixtures/example-ts-ets/tshelper.js @@ -0,0 +1,19 @@ +'use strict'; + +module.exports = { + watchDirs: { + model: { + path: 'app/model', // dir path + generator: 'class', // generator name + interface: 'IModel', // interface name + declareTo: 'Application.model', // declare to this interface + interfaceHandle: val => `ReturnType`, // interfaceHandle + }, + + custom: { + path: 'app/custom', // dir path + generator: 'auto', // generator name + declareTo: 'Application.custom', // declare to this interface + }, + }, +}; diff --git a/test/ts.test.js b/test/ts.test.js index 25a1627a..946ad04e 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -3,6 +3,8 @@ const path = require('path'); const coffee = require('coffee'); const mm = require('mm'); +const fs = require('fs'); +const rimraf = require('mz-modules/rimraf'); describe('test/ts.test.js', () => { const eggBin = require.resolve('../bin/egg-bin'); @@ -152,7 +154,7 @@ describe('test/ts.test.js', () => { it('should cov app', () => { return coffee.fork(eggBin, [ 'cov' ], { cwd }) - .debug() + // .debug() .expect('stdout', /hi, egg, 123456/) .expect('stdout', /ts env: true/) .expect('stdout', process.env.NYC_ROOT_ID ? /Coverage summary/ : /Statements.*100%/) @@ -160,4 +162,59 @@ describe('test/ts.test.js', () => { .end(); }); }); + + describe('egg.declarations = true', () => { + if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { + console.log('skip egg@1'); + return; + } + + let pkgJson; + before(() => { + cwd = path.join(__dirname, './fixtures/example-ts-ets'); + pkgJson = JSON.parse(fs.readFileSync(path.resolve(cwd, './package.json')).toString()); + }); + + beforeEach(() => rimraf(path.resolve(cwd, './typings'))); + + afterEach(() => { + pkgJson.egg.declarations = false; + fs.writeFileSync(path.resolve(cwd, './package.json'), JSON.stringify(pkgJson, null, 2)); + }); + + it('should load egg-ts-helper with dts flag', () => { + return coffee.fork(eggBin, [ 'dev', '--dts' ], { cwd }) + // .debug() + .expect('stdout', /application log/) + .expect('stdout', /"typescript":true/) + .expect('stdout', /started/) + .expect('code', 0) + .end(); + }); + + it('should load egg-ts-helper with egg.declarations = true', () => { + pkgJson.egg.declarations = true; + fs.writeFileSync(path.resolve(cwd, './package.json'), JSON.stringify(pkgJson, null, 2)); + + return coffee.fork(eggBin, [ 'dev' ], { cwd }) + // .debug() + .expect('stdout', /application log/) + .expect('stdout', /"typescript":true/) + .expect('stdout', /"declarations":true/) + .expect('stdout', /started/) + .expect('code', 0) + .end(); + }); + + it('should not load egg-ts-helper without flag and egg.declarations', () => { + return coffee.fork(eggBin, [ 'dev' ], { cwd }) + // .debug() + .expect('stdout', /"typescript":true/) + .notExpect('stdout', /application log/) + .notExpect('stdout', /"declarations":true/) + .notExpect('stdout', /started/) + .expect('code', 1) + .end(); + }); + }); });