diff --git a/index.js b/index.js index 083a840..1e1922c 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,12 @@ /* 基础小工具,可通用于服务端和用户端 */ +const fs = require('fs') +const path = require('path') + module.exports = { sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)), + parseJsonPossible(value) { try { return JSON.parse(value) @@ -10,6 +14,7 @@ module.exports = { return value } }, + sortAndFilterJson({ fields, entity, exclude = [] } = {}) { const newEntity = {} for (let key of Object.keys(fields).sort()) { @@ -22,4 +27,15 @@ module.exports = { } 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 {} + } + }, + } \ No newline at end of file