Skip to content

Commit

Permalink
fix: 🐛 Refactor @ddn/core and fix some field type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
limsbase committed Jun 26, 2020
1 parent 0fa11ed commit 9e9173c
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 166 deletions.
3 changes: 2 additions & 1 deletion examples/fun-tests/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function genOptions () {
.option('-l, --log <level>', 'Log level')
.option('-d, --daemon', 'Run ddn node as daemon')
.option('-e, --execute <path>', 'exe')
.option('-r, --reindex', 'verify or not when block loading')
.option('--dapps <dir>', 'DApps directory')
.option('--base <dir>', 'Base directory')
.parse(process.argv)
Expand Down Expand Up @@ -68,7 +69,7 @@ function genOptions () {
}
})
} else {
configObject.peers.list = []
configObject.peers.list = command.peers
}
}

Expand Down
8 changes: 5 additions & 3 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# `core`
# @DDN/core

> TODO: description
用于对 DDN 区块链 相关的配置等相关的基本操作,让系统支持 .ddnrc.js 等根据环境不同的配置文件

## Usage

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

```
const ddnCore = require('core');
const ddnCore = require('@ddn/core');
// TODO: DEMONSTRATE API
```
4 changes: 1 addition & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
"@babel/preset-typescript": "7.3.3",
"@babel/register": "7.4.4",
"@umijs/error-code-map": "^1.0.0",
"babel-preset-umi": "1.8.1",
"chalk": "2.4.2",
"extend2": "1.0.0",
"marked": "0.6.2",
"marked-terminal": "3.2.0",
"os-locale": "^4.0.0",
"signale": "1.4.0",
"slash2": "2.0.0",
"umi-utils": "1.7.1"
"slash2": "2.0.0"
},
"devDependencies": {
"umi-types": "0.5.3"
Expand Down
38 changes: 19 additions & 19 deletions packages/core/src/getPaths.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { join } from 'path';
import { existsSync, statSync } from 'fs';
import { IConfig } from 'umi-types';
import { join } from 'path'
import { existsSync, statSync } from 'fs'
import { IConfig } from 'umi-types'

function test(path) {
return existsSync(path) && statSync(path).isDirectory();
function test (path) {
return existsSync(path) && statSync(path).isDirectory()
}

interface IOpts {
cwd: string;
config: IConfig;
}

export default function(opts: IOpts) {
const { cwd, config } = opts;
const outputPath = config.outputPath || './dist';
export default function (opts: IOpts) {
const { cwd, config } = opts
const outputPath = config.outputPath || './dist'

let publicPath = 'public';
let publicPath = 'public'
if (process.env.PUBLIC_PATH) {
publicPath = process.env.PUBLIC_PATH;
publicPath = process.env.PUBLIC_PATH
} else {
if (test(join(cwd, 'src/public'))) {
publicPath = 'src/public';
publicPath = 'src/public'
}
if (test(join(cwd, 'public'))) {
publicPath = 'public';
publicPath = 'public'
}
}

const absPagesPath = join(cwd, publicPath);
const absSrcPath = join(absPagesPath, '../');
const absPagesPath = join(cwd, publicPath)
const absSrcPath = join(absPagesPath, '../')

const envAffix = process.env.NODE_ENV === 'development' ? '' : `-production`;
const envAffix = process.env.NODE_ENV === 'development' ? '' : '-production'
const tmpDirPath = process.env.DDN_TEMP_DIR
? `${process.env.DDN_TEMP_DIR}${envAffix}`
: `${publicPath}/.ddn${envAffix}`;
: `${publicPath}/.ddn${envAffix}`

const absTmpDirPath = join(cwd, tmpDirPath);
const absTmpDirPath = join(cwd, tmpDirPath)

return {
cwd,
Expand All @@ -50,6 +50,6 @@ export default function(opts: IOpts) {
absRouterJSPath: join(absTmpDirPath, 'router.js'),
absLibraryJSPath: join(absTmpDirPath, 'ddn.js'),
absRegisterSWJSPath: join(absTmpDirPath, 'registerServiceWorker.js'),
absPageDocumentPath: join(absPagesPath, 'document.ejs'),
};
absPageDocumentPath: join(absPagesPath, 'document.ejs')
}
}
192 changes: 96 additions & 96 deletions packages/core/src/getUserConfig.ts
Original file line number Diff line number Diff line change
@@ -1,118 +1,118 @@
import { join, extname } from "path";
import { existsSync } from "fs";
import assert from "assert";
import extend from "extend2";
import { winPath } from "umi-utils";
import { IConfig } from "umi-types";
import { join, extname } from 'path'
import { existsSync } from 'fs'
import assert from 'assert'
import extend from 'extend2'
import winPath from './winPath'
import { IConfig } from 'umi-types'

interface IOpts {
cwd?: string;
defaultConfig?: IConfig;
onError?: Function;
}

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.json",
"config/config.ts",
"config/config.js"
];

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

assert(
validFiles.length <= 1,
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.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.`
);
if (validFiles[0]) {
return winPath(join(cwd, validFiles[0]));
}
)
if (validFiles[0]) {
return winPath(join(cwd, validFiles[0]))
}
}

export function addAffix(file, affix) {
const ext = extname(file);
return file.replace(new RegExp(`${ext}$`), `.${affix}${ext}`);
export function addAffix (file, affix) {
const ext = extname(file)
return file.replace(new RegExp(`${ext}$`), `.${affix}${ext}`)
}

function defaultOnError(e) {
console.error(e);
function defaultOnError (e) {
console.error(e)
}

export function requireFile(f, opts: IOpts = {}) {
if (!existsSync(f)) {
return {};
}
export function requireFile (f, opts: IOpts = {}) {
if (!existsSync(f)) {
return {}
}

const { onError = defaultOnError } = opts;
let ret: any = {};
try {
const { onError = defaultOnError } = opts
let ret: any = {}
try {
ret = require(f) || {}; // eslint-disable-line
} catch (e) {
onError(e, f);
}
// support esm + babel transform
return ret.default || ret;
} catch (e) {
onError(e, f)
}
// support esm + babel transform
return ret.default || ret
}

export function mergeConfigs(...configs): IConfig {
return extend(true, ...configs);
export function mergeConfigs (...configs): IConfig {
return extend(true, ...configs)
}

export function getConfigByConfigFile(configFile, opts: IOpts = {}): IConfig {
const ddnEnv = process.env.DDN_ENV;
const isDev = process.env.NODE_ENV === "development";
const { defaultConfig, onError } = opts;
export function getConfigByConfigFile (configFile, opts: IOpts = {}): IConfig {
const ddnEnv = process.env.DDN_ENV
const isDev = process.env.NODE_ENV === 'development'
const { defaultConfig, onError } = opts

const requireOpts = { onError };
const requireOpts = { onError }

/**
/**
* development: config.local.* and config.testnet.*;
* other: mainnet
*/
const configs = [
defaultConfig,
requireFile(configFile, requireOpts),
ddnEnv && requireFile(addAffix(configFile, ddnEnv), requireOpts),
isDev && requireFile(addAffix(configFile, "testnet"), requireOpts),
isDev && requireFile(addAffix(configFile, "local"), requireOpts),
!isDev && requireFile(addAffix(configFile, "mainnet"), requireOpts)
];

return mergeConfigs(...configs);
const configs = [
defaultConfig,
requireFile(configFile, requireOpts),
ddnEnv && requireFile(addAffix(configFile, ddnEnv), requireOpts),
isDev && requireFile(addAffix(configFile, 'testnet'), requireOpts),
isDev && requireFile(addAffix(configFile, 'local'), requireOpts),
!isDev && requireFile(addAffix(configFile, 'mainnet'), requireOpts)
]

return mergeConfigs(...configs)
}

// Use DDN_ENV to add yoursef config, e.g: DDN_ENV=prod config.prod.js
export function getConfigPaths(cwd): string[] {
const env = process.env.DDN_ENV;
return [
join(cwd, "config/"),
join(cwd, ".ddnrc.js"),
join(cwd, ".ddnrc.ts"),
join(cwd, ".ddnrc.local.js"),
join(cwd, ".ddnrc.local.ts"),
...(env
? [join(cwd, `.ddnrc.${env}.js`), join(cwd, `.ddnrc.${env}.ts`)]
: [])
];
// Use DDN_ENV to add yoursef config, e.g: DDN_ENV=prod config.prod.js
export function getConfigPaths (cwd): string[] {
const env = process.env.DDN_ENV
return [
join(cwd, 'config/'),
join(cwd, '.ddnrc.js'),
join(cwd, '.ddnrc.ts'),
join(cwd, '.ddnrc.local.js'),
join(cwd, '.ddnrc.local.ts'),
...(env
? [join(cwd, `.ddnrc.${env}.js`), join(cwd, `.ddnrc.${env}.ts`)]
: [])
]
}

export function cleanConfigRequireCache(cwd) {
const paths = getConfigPaths(cwd);
Object.keys(require.cache).forEach(file => {
if (
paths.some(path => {
return file.indexOf(path) === 0;
})
) {
delete require.cache[file];
}
});
export function cleanConfigRequireCache (cwd) {
const paths = getConfigPaths(cwd)
Object.keys(require.cache).forEach(file => {
if (
paths.some(path => {
return file.indexOf(path) === 0
})
) {
delete require.cache[file]
}
})
}

/**
Expand All @@ -122,15 +122,15 @@ 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);

if (absConfigFile) {
return getConfigByConfigFile(absConfigFile, {
defaultConfig
});
} else {
return {};
}
export default function (opts: IOpts = {}): IConfig {
const { cwd, defaultConfig } = opts
const absConfigFile = getConfigFile(cwd)

if (absConfigFile) {
return getConfigByConfigFile(absConfigFile, {
defaultConfig
})
} else {
return {}
}
}
4 changes: 1 addition & 3 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import getPaths from './getPaths'
import getUserConfig from './getUserConfig'
import registerBabel from './registerBabel'

export default {
getPaths,
getUserConfig,
registerBabel
getUserConfig
}
Loading

0 comments on commit 9e9173c

Please sign in to comment.