添加 .prettierrc.js 并格式化

This commit is contained in:
陆柯 2021-01-24 11:35:17 +08:00
parent 162f1f2c8d
commit 38d2c0c26b
3 changed files with 36 additions and 17 deletions

16
.prettierrc.js Normal file
View 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
}

View File

@ -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]
}
}

View File

@ -1,5 +1,5 @@
{
"name": "so.base",
"name": "sysconfig",
"version": "0.1.0",
"private": true,
"dependencies": {