From 38d2c0c26b855c5aca56ed0015beef726c414406 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Sun, 24 Jan 2021 11:35:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20.prettierrc.js=20=E5=B9=B6?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierrc.js | 16 ++++++++++++++++ index.js | 35 +++++++++++++++++++---------------- package.json | 2 +- 3 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 .prettierrc.js diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..e001ecd --- /dev/null +++ b/.prettierrc.js @@ -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 +} diff --git a/index.js b/index.js index 6d649fb..0dcacd6 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,9 @@ const path = require('path') const commander = require('commander') const deepmerge = require('deepmerge') -module.exports = (function(){ - if (!global.Config) { // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。 +module.exports = (function () { + if (!global.Config) { + // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。 console.info('★★★★★★★★ 配置参数:依次载入系统配置、用户配置、命令行参数 ★★★★★★★★') // 配置参数(按优先级从低到高): @@ -13,45 +14,47 @@ module.exports = (function(){ // ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。 // 命令行参数 - console.info(" -- 读取配置文件") + console.info(' -- 读取配置文件') 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)) console.info(`${configFile} loaded`) - }else { + } else { 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里添加 console.info(`${configFile} loaded`) - }else { + } else { 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)) console.info(`${configFile} loaded`) - }else { + } else { console.info(`Missing and omitting ${configFile}`) } global.Config = global.Config || {} - console.log(" -- 载入命令行参数") + console.log(' -- 载入命令行参数') 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.parse(process.argv).parse(process.argv) - - console.log(" -- 如果是生产环境,加载生产配置") + + console.log(' -- 如果是生产环境,加载生产配置') global.Config.env = commander.env || global.Config.env || process.env.NODE_ENV if (global.Config.env === 'production' && global.Config.production) { global.Config = deepmerge(global.Config, global.Config.production) } delete global.Config.production - console.log(" -- 把命令行参数合并入配置") - for (let key in commander){ - if (typeof(commander[key])==='string' && !/^_/.test(key)) { + console.log(' -- 把命令行参数合并入配置') + for (let key in commander) { + if (typeof commander[key] === 'string' && !/^_/.test(key)) { global.Config[key] = commander[key] || global.Config[key] } } diff --git a/package.json b/package.json index 19d0758..419fa7c 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "so.base", + "name": "sysconfig", "version": "0.1.0", "private": true, "dependencies": {