Skip to content

Commit

Permalink
feat: support ts from env and pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Mar 29, 2018
1 parent 35d4108 commit 517607d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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;
// if (process.env.EGG_TYPESCRIPT) options.typescript = process.env.EGG_TYPESCRIPT;
const app = mock.app(options);

before(() => app.ready());
Expand Down
16 changes: 16 additions & 0 deletions lib/format_options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const fs = require('fs');
const path = require('path');
const mm = require('mm');
const debug = require('debug')('mm');
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ts",
"egg": {
"typescript": true
}
}
15 changes: 15 additions & 0 deletions test/format_options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 517607d

Please sign in to comment.