From 50ee2d2a2dde635748dbf3cc07269f2520e7025c Mon Sep 17 00:00:00 2001 From: TZ Date: Thu, 29 Mar 2018 14:07:08 +0800 Subject: [PATCH] feat: support ts from env and pkg --- bootstrap.js | 1 - lib/format_options.js | 16 ++++++++++++++++ test/fixtures/ts/package.json | 6 ++++++ test/format_options.test.js | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/ts/package.json diff --git a/bootstrap.js b/bootstrap.js index 1eaf5a8..2c80e19 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -5,7 +5,6 @@ const mock = require('./index').default; const options = {}; if (process.env.EGG_BASE_DIR) options.baseDir = process.env.EGG_BASE_DIR; -if (process.env.EGG_TYPESCRIPT) options.typescript = process.env.EGG_TYPESCRIPT; const app = mock.app(options); before(() => app.ready()); diff --git a/lib/format_options.js b/lib/format_options.js index 5ee50f4..7a53fae 100644 --- a/lib/format_options.js +++ b/lib/format_options.js @@ -1,5 +1,6 @@ 'use strict'; +const fs = require('fs'); const path = require('path'); const mm = require('mm'); const debug = require('debug')('mm'); @@ -39,6 +40,21 @@ module.exports = function formatOptions(options) { } options.customEgg = options.framework = framework; + // typescript + if (!options.hasOwnProperty('typescript')) { + if (process.env.EGG_TYPESCRIPT) { + options.typescript = Boolean(process.env.EGG_TYPESCRIPT); + } else { + const pkgFile = path.join(options.baseDir, 'package.json'); + if (fs.existsSync(pkgFile)) { + const pkgInfo = require(pkgFile); + if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) { + options.typescript = true; + } + } + } + } + const plugins = options.plugins = options.plugins || {}; // add self as a plugin diff --git a/test/fixtures/ts/package.json b/test/fixtures/ts/package.json new file mode 100644 index 0000000..8c2c0c2 --- /dev/null +++ b/test/fixtures/ts/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts", + "egg": { + "typescript": true + } +} \ No newline at end of file diff --git a/test/format_options.test.js b/test/format_options.test.js index 850e1c6..9c9a57c 100644 --- a/test/format_options.test.js +++ b/test/format_options.test.js @@ -153,4 +153,19 @@ describe('test/format_options.test.js', () => { formatOptions(); assert.notEqual(process.env.HOME, baseDir); }); + + it('should read egg.typescript', () => { + const baseDir = path.join(__dirname, 'fixtures/ts'); + mm(process, 'cwd', () => baseDir); + const opts = formatOptions(); + assert(opts.typescript === true); + }); + + it('should read process.env.EGG_TYPESCRIPT', () => { + const baseDir = path.join(__dirname, 'fixtures/demo'); + mm(process, 'cwd', () => baseDir); + mm(process.env, 'EGG_TYPESCRIPT', true); + const opts = formatOptions(); + assert(opts.typescript === true); + }); });