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 1 commit
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
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')) {
Copy link
Member

Choose a reason for hiding this comment

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

不要 hasOwnProperty 吧

Copy link
Member Author

@atian25 atian25 Mar 30, 2018

Choose a reason for hiding this comment

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

options. typescript === undefined ?

Copy link
Member

@popomore popomore Mar 30, 2018

Choose a reason for hiding this comment

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

'typescript; in options?

测试是不是就提供环境变量就好了

if (process.env.EGG_TYPESCRIPT) {
Copy link
Member Author

Choose a reason for hiding this comment

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

两个同时存在的时候,优先级是?

Copy link
Member

Choose a reason for hiding this comment

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

感觉 env 优先更合理一些

Copy link
Member Author

Choose a reason for hiding this comment

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

那就是现在这种了。

Copy link
Member

Choose a reason for hiding this comment

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

所以命令行 > 环境 > pkg

Copy link
Member Author

Choose a reason for hiding this comment

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

这里应该不存在命令行的情况, mm.app 都是手动调用的。
egg-bin 那边想影响这个,只能通过环境变量的方式默认配置

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) {
Copy link
Member

Choose a reason for hiding this comment

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

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/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();
Copy link
Member

Choose a reason for hiding this comment

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

这个不是应该传 typescript 测一下优先级?

assert(opts.typescript === true);
});
});