添加 .prettierrc.js 并格式化
This commit is contained in:
parent
162f1f2c8d
commit
38d2c0c26b
16
.prettierrc.js
Normal file
16
.prettierrc.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
对 VSCode Prettier 有效;建议一直要有本配置文件,否则不同版本的 Prettier 的默认配置会不同,例如 TrailingComma
|
||||||
|
对 VSCode Prettier Standard 无效,似乎是集成了不能修改的配置。
|
||||||
|
*/
|
||||||
|
module.exports = {
|
||||||
|
printWidth: 160, // default 80
|
||||||
|
tabWidth: 2, // default 2
|
||||||
|
useTabs: false,
|
||||||
|
semi: false, // default true
|
||||||
|
singleQuote: true, // default false
|
||||||
|
trailingComma: 'es5', // none (default in v 1.*), es5 (default in v2.0.0), all
|
||||||
|
bracketSpacing: true, // default true
|
||||||
|
jsxBracketSameLine: false, // default false
|
||||||
|
arrowParens: 'always', // avoid (default in v1.9.0), always (default since v2.0.0)
|
||||||
|
quoteProps: 'as-needed', // as-needed (default), consistent, preserve
|
||||||
|
}
|
21
index.js
21
index.js
@ -4,7 +4,8 @@ const commander = require('commander')
|
|||||||
const deepmerge = require('deepmerge')
|
const deepmerge = require('deepmerge')
|
||||||
|
|
||||||
module.exports = (function () {
|
module.exports = (function () {
|
||||||
if (!global.Config) { // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
if (!global.Config) {
|
||||||
|
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||||||
console.info('★★★★★★★★ 配置参数:依次载入系统配置、用户配置、命令行参数 ★★★★★★★★')
|
console.info('★★★★★★★★ 配置参数:依次载入系统配置、用户配置、命令行参数 ★★★★★★★★')
|
||||||
|
|
||||||
// 配置参数(按优先级从低到高):
|
// 配置参数(按优先级从低到高):
|
||||||
@ -13,21 +14,23 @@ module.exports = (function(){
|
|||||||
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
||||||
// 命令行参数
|
// 命令行参数
|
||||||
|
|
||||||
console.info(" -- 读取配置文件")
|
console.info(' -- 读取配置文件')
|
||||||
let configFile
|
let configFile
|
||||||
if (fs.existsSync(configFile = path.join(process.cwd(), './ConfigBasic.js'))) {
|
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigBasic.js')))) {
|
||||||
global.Config = deepmerge({}, require(configFile))
|
global.Config = deepmerge({}, require(configFile))
|
||||||
console.info(`${configFile} loaded`)
|
console.info(`${configFile} loaded`)
|
||||||
} else {
|
} else {
|
||||||
console.info(`Missing and omitting ${configFile}`)
|
console.info(`Missing and omitting ${configFile}`)
|
||||||
}
|
}
|
||||||
if (fs.existsSync(configFile = path.join(process.cwd(), './ConfigCustom.js'))) { // 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigCustom.js')))) {
|
||||||
|
// 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
||||||
global.Config = deepmerge(global.Config, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
global.Config = deepmerge(global.Config, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
||||||
console.info(`${configFile} loaded`)
|
console.info(`${configFile} loaded`)
|
||||||
} else {
|
} else {
|
||||||
console.info(`Missing and omitting ${configFile}`)
|
console.info(`Missing and omitting ${configFile}`)
|
||||||
}
|
}
|
||||||
if (fs.existsSync(configFile = path.join(process.cwd(), './ConfigSecret.js'))) { // 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigSecret.js')))) {
|
||||||
|
// 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
||||||
global.Config = deepmerge(global.Config, require(configFile))
|
global.Config = deepmerge(global.Config, require(configFile))
|
||||||
console.info(`${configFile} loaded`)
|
console.info(`${configFile} loaded`)
|
||||||
} else {
|
} else {
|
||||||
@ -35,23 +38,23 @@ module.exports = (function(){
|
|||||||
}
|
}
|
||||||
global.Config = global.Config || {}
|
global.Config = global.Config || {}
|
||||||
|
|
||||||
console.log(" -- 载入命令行参数")
|
console.log(' -- 载入命令行参数')
|
||||||
commander.version(global.Config.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
commander.version(global.Config.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||||||
for (let [key, param, desc] of global.Config.commanderOptions || []) {
|
for (let [key, param, desc] of global.Config.commanderOptions || []) {
|
||||||
commander.option(param, `${desc} Default = "${global.Config[key]}"`)
|
commander.option(param, `${desc} Default = "${global.Config[key]}"`)
|
||||||
}
|
}
|
||||||
commander.parse(process.argv).parse(process.argv)
|
commander.parse(process.argv).parse(process.argv)
|
||||||
|
|
||||||
console.log(" -- 如果是生产环境,加载生产配置")
|
console.log(' -- 如果是生产环境,加载生产配置')
|
||||||
global.Config.env = commander.env || global.Config.env || process.env.NODE_ENV
|
global.Config.env = commander.env || global.Config.env || process.env.NODE_ENV
|
||||||
if (global.Config.env === 'production' && global.Config.production) {
|
if (global.Config.env === 'production' && global.Config.production) {
|
||||||
global.Config = deepmerge(global.Config, global.Config.production)
|
global.Config = deepmerge(global.Config, global.Config.production)
|
||||||
}
|
}
|
||||||
delete global.Config.production
|
delete global.Config.production
|
||||||
|
|
||||||
console.log(" -- 把命令行参数合并入配置")
|
console.log(' -- 把命令行参数合并入配置')
|
||||||
for (let key in commander) {
|
for (let key in commander) {
|
||||||
if (typeof(commander[key])==='string' && !/^_/.test(key)) {
|
if (typeof commander[key] === 'string' && !/^_/.test(key)) {
|
||||||
global.Config[key] = commander[key] || global.Config[key]
|
global.Config[key] = commander[key] || global.Config[key]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "so.base",
|
"name": "sysconfig",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
Loading…
Reference in New Issue
Block a user