From 013ecd25ad9e7263262105bb9f21dba4a206b692 Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 19:59:51 +0800 Subject: [PATCH 1/2] feat: add typescript docs --- README.md | 1 + lib/utils/options.js | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 78f33b5..b37f8e5 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ startCluster(options, () => { | sticky | `Boolean` | sticky mode server | | port | `Number` | port | | https | `Object` | start a https server, note: `key` / `cert` should be full path to file | +| typescript | `Boolean` | enable loader's typescript support | ## License diff --git a/lib/utils/options.js b/lib/utils/options.js index a0aaf6e..eebd996 100644 --- a/lib/utils/options.js +++ b/lib/utils/options.js @@ -18,6 +18,7 @@ module.exports = function(options) { https: false, key: '', cert: '', + typescript: false, }; options = extend(defaults, options); if (!options.workers) { From 2e72cad93854a8a078d030e0800f3df99ac7dc5e Mon Sep 17 00:00:00 2001 From: TZ Date: Mon, 26 Mar 2018 21:16:14 +0800 Subject: [PATCH 2/2] test: add ts test --- package.json | 4 +- test/fixtures/apps/ts/app.ts | 7 ++++ .../fixtures/apps/ts/config/config.default.ts | 9 ++++ test/fixtures/apps/ts/package.json | 3 ++ test/ts.test.js | 41 +++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/apps/ts/app.ts create mode 100644 test/fixtures/apps/ts/config/config.default.ts create mode 100644 test/fixtures/apps/ts/package.json create mode 100644 test/ts.test.js diff --git a/package.json b/package.json index bac7701..64af3e9 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,9 @@ "mz-modules": "^2.1.0", "pedding": "^1.1.0", "semver": "^5.5.0", - "supertest": "^3.0.0" + "supertest": "^3.0.0", + "ts-node": "^5.0.1", + "typescript": "^2.7.2" }, "engines": { "node": ">= 6.0.0" diff --git a/test/fixtures/apps/ts/app.ts b/test/fixtures/apps/ts/app.ts new file mode 100644 index 0000000..a063672 --- /dev/null +++ b/test/fixtures/apps/ts/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/apps/ts/config/config.default.ts b/test/fixtures/apps/ts/config/config.default.ts new file mode 100644 index 0000000..834e2c2 --- /dev/null +++ b/test/fixtures/apps/ts/config/config.default.ts @@ -0,0 +1,9 @@ +'use strict'; + +import { EggAppConfig } from 'egg'; + +export default (appInfo: EggAppConfig) => { + const config: any = {}; + config.keys = '123456'; + return config; +}; diff --git a/test/fixtures/apps/ts/package.json b/test/fixtures/apps/ts/package.json new file mode 100644 index 0000000..b3b0d50 --- /dev/null +++ b/test/fixtures/apps/ts/package.json @@ -0,0 +1,3 @@ +{ + "name": "ts" +} diff --git a/test/ts.test.js b/test/ts.test.js new file mode 100644 index 0000000..6394e45 --- /dev/null +++ b/test/ts.test.js @@ -0,0 +1,41 @@ +'use strict'; + +const mm = require('egg-mock'); +const utils = require('./utils'); + +describe('test/ts.test.js', () => { + if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') { + console.log('skip egg@1'); + return; + } + + let app; + + afterEach(mm.restore); + afterEach(() => app.close()); + + it('support typescript', done => { + mm.env('local'); + app = utils.cluster('apps/ts', { + typescript: true, + opt: { execArgv: [ '--require', require.resolve('ts-node/register') ] }, + }); + // app.debug(); + app.expect('stdout', /hi, egg, 123456/) + .expect('stdout', /egg started/) + .notExpect('stdout', /\[master\] agent_worker#1:\d+ start with clusterPort:\d+/) + .expect('code', 0) + .end(done); + }); + + it('require ts-node register', done => { + mm.env('local'); + app = utils.cluster('apps/ts', { + typescript: true, + }); + // app.debug(); + app.expect('stderr', /`require.extensions` should contains `.ts`/) + .expect('code', 1) + .end(done); + }); +});