Skip to content

Commit

Permalink
improvement(ddn-core): configured according to different environments
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 可以通过环境变量 `DDN_ENV` 区分不同环境来指定配置
  • Loading branch information
limsbase committed Dec 26, 2019
1 parent 502982b commit 6c717a4
Show file tree
Hide file tree
Showing 24 changed files with 354 additions and 78 deletions.
74 changes: 74 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Contribute

## Set up

Clone the repo.

```bash
$ git clone https://github.com/ddnlink/ddn.git
```

Install dev deps after git clone the repo.

```bash
$ cd ddn
$ yarn
```

Bootstrap every package with yarn. (Need to execute when new package is included)

```bash
$ yarn bootstrap
```

Build first.

```bash
$ yarn build
```

Run it.

```bash
$ yarn start
```

## Common Tasks

Monitor file changes and transform with babel.

```bash
$ yarn build --watch
```

Run test.

```bash
# Including e2e test
$ yarn test

# Unit test only
$ yarn test .test.(t|j)s

# Test specified file and watch
$ yarn test getMockData.test.js -w

# Test specified package
$ PACKAGE=ddn-core yarn test

# Don't run e2e test
$ E2E=none yarn test

# Generate coverage
$ yarn test --coverage
```

Publish to npm.

```bash
# Generator the changelog first.
$ yarn changelog

# Do not use yarn for this command.
$ npm run publish
```
64 changes: 64 additions & 0 deletions doc/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 配置

## 配置文件

DDN 允许在 `.ddnrc.js``config/config.json``config/config.js`(三选一,`.ddnrc.js` 优先)中进行配置,支持 ES6 语法。

> 为简化说明,后续文档里只会出现 `.ddnrc.js`
比如:

```js
export default {
base: '/admin/',
publicPath: 'http://cdn.com/foo',
plugins: [
['umi-plugin-react', {
dva: true,
}],
],
};
```

具体配置项详见[配置](/zh/config/)

## .ddnrc.local.js

`.ddnrc.local.js` 是本地的配置文件,**不要提交到 git**,所以通常需要配置到 `.gitignore`。如果存在,会和 `.ddnrc.js` 合并后再返回。

## DDN_ENV

可以通过环境变量 `DDN_ENV` 区分不同环境来指定配置。

举个例子,

```js
// .ddnrc.js
export default { a: 1, b: 2 };

// .ddnrc.cloud.js
export default { b: 'cloud', c: 'cloud' };

// .ddnrc.local.js
export default { c: 'local' };
```

不指定 `DDN_ENV` 时,拿到的配置是:

```js
{
a: 1,
b: 2,
c: 'local',
}
```

指定 `DDN_ENV=cloud` 时,拿到的配置是:

```js
{
a: 1,
b: 'cloud',
c: 'local',
}
```
47 changes: 47 additions & 0 deletions doc/config/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
sidebarDepth: 2
---

# 配置

## 基本配置

### 资产插件

- 类型:`Array`
- 默认值:`[]`

配置插件列表。

数组项为指向插件的路径,可以是 npm 依赖、相对路径或绝对路径。如果是相对路径,则会从项目根目录开始找。比如:

```js
export default {
plugins: [
// npm 依赖
'ddn-aob',
// 相对路径
'./plugin',
// 绝对路径
`${__dirname}/plugin.js`,
],
};
```

如果插件有参数,则通过数组的形式进行配置,第一项是路径,第二项是参数,类似 babel 插件的配置方式。比如:

```js
export default {
plugins: [
// 有参数
[
'ddn-aob',
{
a: true,
b: true,
},
],
'./plugin',
],
};
```
8 changes: 7 additions & 1 deletion packages/ddn-core/src/getUserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ interface IOpts {
export function getConfigFile(cwd) {
const files = process.env.DDN_CONFIG_FILE
? process.env.DDN_CONFIG_FILE.split(',').filter(v => v && v.trim())
: ['.ddnrc.ts', '.ddnrc.js', 'config/config.ts', 'config/config.js'];
: ['.ddnrc.ts', '.ddnrc.js', 'config/config.json', 'config/config.ts', 'config/config.js'];

const validFiles = files.filter(f => existsSync(join(cwd, f)));

assert(
validFiles.length <= 1,
`Multiple config files (${validFiles.join(', ')}) were detected, please keep only one.`,
Expand Down Expand Up @@ -94,6 +96,10 @@ export function cleanConfigRequireCache(cwd) {
});
}

/**
* 配置调用
* @param opts {cwd: cwd, defaultConfig: config.default.js }
*/
export default function(opts: IOpts = {}): IConfig {
const { cwd, defaultConfig } = opts;
const absConfigFile = getConfigFile(cwd);
Expand Down
2 changes: 1 addition & 1 deletion packages/ddn-core/test/error.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DdnError, printDdnError } from '../src/error';
import { DdnError, printDdnError } from '../lib/error';

test('ERR_CORE_PLUGIN_RESOLVE_FAILED', () => {
const error = new DdnError({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"history": "hash"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export default {
history: 'local',
story: 'ok'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export default {
history: 'sorry',
story: 'ok',
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export default {
history: 'prod',
story: 'yes',
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"a": 1,
"b": 2
}
2 changes: 1 addition & 1 deletion packages/ddn-core/test/getPaths.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path';
import getPaths from '../src/getPaths';
import getPaths from '../lib/getPaths';

const fixtures = join(__dirname, 'fixtures/getPaths');

Expand Down
59 changes: 58 additions & 1 deletion packages/ddn-core/test/getUserConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import getUserConfig, {
addAffix,
getConfigPaths,
cleanConfigRequireCache,
} from '../src/getUserConfig';
} from '../lib/getUserConfig';

const fixtures = winPath(`${__dirname}/fixtures/getUserConfig`);

Expand All @@ -28,6 +28,12 @@ describe('getUserConfig', () => {
expect(stripPrefix(getConfigFile(`${fixtures}/ddnrc`))).toEqual('ddnrc/.ddnrc.js');
});

it('config/config.json', () => {
expect(stripPrefix(getConfigFile(`${fixtures}/config-configjson`))).toEqual(
'config-configjson/config/config.json',
);
});

it('config/config.js', () => {
expect(stripPrefix(getConfigFile(`${fixtures}/config-directory`))).toEqual(
'config-directory/config/config.js',
Expand Down Expand Up @@ -77,6 +83,49 @@ test('config with .ddnrc.js', () => {
});
});

test('config with mergeConfigs .ddnrc.local.js', () => {
process.env.NODE_ENV = 'development'

const config = getUserConfig({
cwd: join(fixtures, 'config-ddnrc'),
});

expect(config).toEqual({
history: 'sorry',
story: 'ok',
});

process.env.NODE_ENV = ''
});

test('config with mergeConfigs .ddnrc.prod.js', () => {
process.env.DDN_ENV = 'prod'

const config = getUserConfig({
cwd: join(fixtures, 'config-ddnrc'),
});
expect(config).toEqual({
history: 'prod',
story: 'yes'
});

process.env.DDN_ENV = ''
});

test('config with mergeConfigs config/config.local.js and config.jsons', () => {
process.env.NODE_ENV = 'development'

const config = getUserConfig({
cwd: join(fixtures, 'config-configjson'),
});
expect(config).toEqual({
history: 'local',
story: 'ok'
});

process.env.NODE_ENV = ''
});

test('config with DDN_CONFIG_FILE env', () => {
process.env.DDN_CONFIG_FILE = 'foo.js';
const config = getUserConfig({
Expand All @@ -92,6 +141,7 @@ test('getConfigPaths', () => {
function winPathFiles(files) {
return files.map(f => slash2(f));
}
process.env.DDN_ENV = ''
expect(winPathFiles(getConfigPaths('foo'))).toEqual([
'foo/config/',
'foo/.ddnrc.js',
Expand All @@ -116,6 +166,13 @@ test('requireFile', () => {
expect(requireFile('/file/not/exists')).toEqual({});
});

test('requireFile from config.json', () => {
expect(requireFile(join(fixtures, 'requireFile', 'config.json'))).toEqual({
"a": 1,
"b": 2
});
});

test('requireFile with syntax error', () => {
let error;
requireFile(join(fixtures, 'requireFile', 'syntaxError.js'), {
Expand Down
6 changes: 3 additions & 3 deletions packages/ddn/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const command =require('commander');
const path =require('path');
const fs =require('fs');
const { Utils } =require('@ddn/ddn-utils');
const { getConfigFile, requireFile } =require('@ddn/ddn-core/lib/getUserConfig');
const getUserConfig =require('@ddn/ddn-core/lib/getUserConfig');
const { getConfigFile } =require('@ddn/ddn-core/lib/getUserConfig');
const Program =require('./lib/kernal/program');

/**
Expand All @@ -31,7 +32,6 @@ function genOptions() {

const baseDir = command.base || path.resolve(__dirname, './');

// let configFile = path.join(baseDir, 'config', 'config.json');
let configFile = getConfigFile(baseDir);
if (command.config) {
configFile = path.resolve(process.cwd(), command.config);
Expand Down Expand Up @@ -66,7 +66,7 @@ function genOptions() {
return;
}

const configObject = requireFile(configFile);
const configObject = getUserConfig.default({cwd: baseDir});
const genesisblockObject = JSON.parse(fs.readFileSync(genesisblockFile, 'utf8'));

//wxm 修改config.json文件,自动生成dapp的masterpassword,这个不应该在代码中,应该在外围生成
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
{
"port": 8001,
"address": "127.0.0.1",
"publicIp": "",
Expand Down Expand Up @@ -149,4 +149,4 @@ module.exports = {
"delegateNumber": 101,
"blockIntervalTime": 10
}
};
}
3 changes: 3 additions & 0 deletions packages/ddn/config/config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
"port": 8000
}
Loading

0 comments on commit 6c717a4

Please sign in to comment.