add getDynamicConfig function

This commit is contained in:
陆柯 2021-08-28 16:16:56 +08:00
parent d431b8a09d
commit adfc450f0e

View File

@ -1,8 +1,12 @@
/* /*
*/ */
const fs = require('fs')
const path = require('path')
module.exports = { module.exports = {
sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)), sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
parseJsonPossible(value) { parseJsonPossible(value) {
try { try {
return JSON.parse(value) return JSON.parse(value)
@ -10,6 +14,7 @@ module.exports = {
return value return value
} }
}, },
sortAndFilterJson({ fields, entity, exclude = [] } = {}) { sortAndFilterJson({ fields, entity, exclude = [] } = {}) {
const newEntity = {} const newEntity = {}
for (let key of Object.keys(fields).sort()) { for (let key of Object.keys(fields).sort()) {
@ -22,4 +27,15 @@ module.exports = {
} }
return JSON.stringify(newEntity) return JSON.stringify(newEntity)
}, },
getDynamicConfig(dynamicConfigFile='ConfigDynamic.js') { // dynamicConfigFile should be absolute or relative to the node process's dir.
const fullpath = path.join(process.cwd(), dynamicConfigFile)
if (fs.existsSync(fullpath)) {
delete require.cache[require.resolve(fullpath)] // delete require.cache[fullpath] 不起作用
return require(fullpath)
} else {
return {}
}
},
} }