Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ts from env and pkg #71

Merged
merged 2 commits into from
Mar 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
17 changes: 17 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,22 @@ module.exports = function formatOptions(options) {
}
options.customEgg = options.framework = framework;

// typescript
if (!('typescript' in options)) {
// process.env maybe force to string type
if (String(process.env.EGG_TYPESCRIPT).toLowerCase() === 'true') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 env 配置的时候,都会转换为 string 的,所以要这么处理

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 === true) {
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-false/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ts-false",
"egg": {
"typescript": false
}
}
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
}
}
38 changes: 37 additions & 1 deletion test/format_options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +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);
});

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);
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);
});
});
});