From 50ee2d2a2dde635748dbf3cc07269f2520e7025c Mon Sep 17 00:00:00 2001 From: TZ Date: Thu, 29 Mar 2018 14:07:08 +0800 Subject: [PATCH 1/2] 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); + }); }); From cc318121cc1977af862a272317547c9d798da112 Mon Sep 17 00:00:00 2001 From: TZ Date: Fri, 30 Mar 2018 15:51:47 +0800 Subject: [PATCH 2/2] fix: detect --- lib/format_options.js | 9 +++--- test/fixtures/ts-false/package.json | 6 ++++ test/format_options.test.js | 47 +++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/ts-false/package.json diff --git a/lib/format_options.js b/lib/format_options.js index 7a53fae..684889a 100644 --- a/lib/format_options.js +++ b/lib/format_options.js @@ -41,14 +41,15 @@ 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); + if (!('typescript' in options)) { + // process.env maybe force to string type + if (String(process.env.EGG_TYPESCRIPT).toLowerCase() === 'true') { + options.typescript = true; } else { const pkgFile = path.join(options.baseDir, 'package.json'); if (fs.existsSync(pkgFile)) { const pkgInfo = require(pkgFile); - if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) { + if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript === true) { options.typescript = true; } } diff --git a/test/fixtures/ts-false/package.json b/test/fixtures/ts-false/package.json new file mode 100644 index 0000000..1aa314a --- /dev/null +++ b/test/fixtures/ts-false/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts-false", + "egg": { + "typescript": false + } +} \ No newline at end of file diff --git a/test/format_options.test.js b/test/format_options.test.js index 9c9a57c..381d8d6 100644 --- a/test/format_options.test.js +++ b/test/format_options.test.js @@ -147,25 +147,46 @@ describe('test/format_options.test.js', () => { it('should not mock process.env.HOME when it has mocked', () => { const baseDir = process.cwd(); - mm(process.env, 'HOME', '/mockpath'); mm(process.env, 'EGG_SERVER_ENV', 'default'); 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); - }); + describe('typescript', () => { + 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); + it('should read process.env.EGG_TYPESCRIPT', () => { + const baseDir = path.join(__dirname, 'fixtures/demo'); + mm(process, 'cwd', () => baseDir); + + mm(process.env, 'EGG_TYPESCRIPT', true); + assert(formatOptions().typescript === true); + + mm(process.env, 'EGG_TYPESCRIPT', false); + assert(!formatOptions().typescript); + + mm(process.env, 'EGG_TYPESCRIPT', 'true'); + assert(formatOptions().typescript === true); + + mm(process.env, 'EGG_TYPESCRIPT', 'false'); + assert(!formatOptions().typescript); + + mm(process.env, 'EGG_TYPESCRIPT', undefined); + assert(!formatOptions().typescript); + }); + + it('should read process.env.EGG_TYPESCRIPT with pkg', () => { + const baseDir = path.join(__dirname, 'fixtures/ts-false'); + mm(process, 'cwd', () => baseDir); + mm(process.env, 'EGG_TYPESCRIPT', true); + const opts = formatOptions(); + assert(opts.typescript === true); + }); }); });