add getDynamicConfig function
This commit is contained in:
		
							parent
							
								
									d431b8a09d
								
							
						
					
					
						commit
						adfc450f0e
					
				
							
								
								
									
										16
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								index.js
									
									
									
									
									
								
							@ -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 {}
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user