2022-07-23 20:32:10 +00:00
|
|
|
|
import lodash from 'lodash'
|
|
|
|
|
import fs from 'fs'
|
2022-03-26 08:21:44 +00:00
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
const _path = process.cwd()
|
2022-06-29 23:05:31 +00:00
|
|
|
|
|
2022-03-26 08:21:44 +00:00
|
|
|
|
let Data = {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 根据指定的path依次检查与创建目录
|
|
|
|
|
* */
|
2022-07-23 20:32:10 +00:00
|
|
|
|
createDir (rootPath = '', path = '', includeFile = false) {
|
|
|
|
|
let pathList = path.split('/')
|
|
|
|
|
let nowPath = rootPath
|
2022-03-26 08:21:44 +00:00
|
|
|
|
pathList.forEach((name, idx) => {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
name = name.trim()
|
2022-07-03 21:32:23 +00:00
|
|
|
|
if (!includeFile && idx <= pathList.length - 1) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
nowPath += name + '/'
|
2022-07-03 21:32:23 +00:00
|
|
|
|
if (name) {
|
|
|
|
|
if (!fs.existsSync(nowPath)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
fs.mkdirSync(nowPath)
|
2022-07-03 21:32:23 +00:00
|
|
|
|
}
|
2022-03-26 08:21:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 读取json
|
|
|
|
|
* */
|
2022-07-23 20:32:10 +00:00
|
|
|
|
readJSON (root, path) {
|
2022-03-26 08:21:44 +00:00
|
|
|
|
if (!/\.json$/.test(path)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
path = path + '.json'
|
2022-03-26 08:21:44 +00:00
|
|
|
|
}
|
|
|
|
|
// 检查并创建目录
|
2022-07-23 20:32:10 +00:00
|
|
|
|
Data.createDir(root, path, true)
|
2022-04-10 05:36:31 +00:00
|
|
|
|
if (fs.existsSync(`${root}/${path}`)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let jsonRet = fs.readFileSync(`${root}/${path}`, 'utf8')
|
|
|
|
|
return JSON.parse(jsonRet)
|
2022-04-10 05:36:31 +00:00
|
|
|
|
}
|
|
|
|
|
return {}
|
2022-03-26 08:21:44 +00:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 写JSON
|
|
|
|
|
* */
|
2022-07-23 20:32:10 +00:00
|
|
|
|
writeJson (path, file, data, space = '\t') {
|
2022-03-26 08:21:44 +00:00
|
|
|
|
if (!/\.json$/.test(file)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
file = file + '.json'
|
2022-03-26 08:21:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查并创建目录
|
2022-07-23 20:32:10 +00:00
|
|
|
|
Data.createDir(_path, path, false)
|
|
|
|
|
console.log(data)
|
|
|
|
|
delete data._res
|
|
|
|
|
return fs.writeFileSync(`${_path}/${path}/${file}`, JSON.stringify(data, null, space))
|
2022-03-26 08:21:44 +00:00
|
|
|
|
},
|
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
async importModule (path, file, rootPath = _path) {
|
2022-06-29 23:05:31 +00:00
|
|
|
|
if (!/\.js$/.test(file)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
file = file + '.js'
|
2022-06-29 23:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
// 检查并创建目录
|
2022-07-23 20:32:10 +00:00
|
|
|
|
Data.createDir(_path, path, true)
|
2022-06-29 23:05:31 +00:00
|
|
|
|
if (fs.existsSync(`${_path}/${path}/${file}`)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let data = await import(`file://${_path}/${path}/${file}`)
|
|
|
|
|
return data || {}
|
2022-06-29 23:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
return {}
|
|
|
|
|
},
|
|
|
|
|
|
2022-03-26 08:21:44 +00:00
|
|
|
|
/*
|
|
|
|
|
* 返回一个从 target 中选中的属性的对象
|
|
|
|
|
*
|
|
|
|
|
* keyList : 获取字段列表,逗号分割字符串
|
|
|
|
|
* key1, key2, toKey1:fromKey1, toKey2:fromObj.key
|
|
|
|
|
*
|
|
|
|
|
* defaultData: 当某个字段为空时会选取defaultData的对应内容
|
|
|
|
|
* toKeyPrefix:返回数据的字段前缀,默认为空。defaultData中的键值无需包含toKeyPrefix
|
|
|
|
|
*
|
|
|
|
|
* */
|
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
getData (target, keyList = '', cfg = {}) {
|
|
|
|
|
target = target || {}
|
|
|
|
|
let defaultData = cfg.defaultData || {}
|
|
|
|
|
let ret = {}
|
2022-03-26 08:21:44 +00:00
|
|
|
|
// 分割逗号
|
2022-07-23 20:32:10 +00:00
|
|
|
|
if (typeof (keyList) === 'string') {
|
|
|
|
|
keyList = keyList.split(',')
|
2022-03-26 08:21:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lodash.forEach(keyList, (keyCfg) => {
|
|
|
|
|
// 处理通过:指定 toKey & fromKey
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let _keyCfg = keyCfg.split(':')
|
|
|
|
|
let keyTo = _keyCfg[0].trim()
|
|
|
|
|
let keyFrom = (_keyCfg[1] || _keyCfg[0]).trim()
|
|
|
|
|
let keyRet = keyTo
|
2022-03-26 08:21:44 +00:00
|
|
|
|
if (cfg.lowerFirstKey) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
keyRet = lodash.lowerFirst(keyRet)
|
2022-03-26 08:21:44 +00:00
|
|
|
|
}
|
|
|
|
|
if (cfg.keyPrefix) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
keyRet = cfg.keyPrefix + keyRet
|
2022-03-26 08:21:44 +00:00
|
|
|
|
}
|
|
|
|
|
// 通过Data.getVal获取数据
|
2022-07-23 20:32:10 +00:00
|
|
|
|
ret[keyRet] = Data.getVal(target, keyFrom, defaultData[keyTo], cfg)
|
2022-03-26 08:21:44 +00:00
|
|
|
|
})
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return ret
|
2022-03-26 08:21:44 +00:00
|
|
|
|
},
|
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
getVal (target, keyFrom, defaultValue) {
|
|
|
|
|
return lodash.get(target, keyFrom, defaultValue)
|
2022-04-03 22:06:03 +00:00
|
|
|
|
},
|
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
getUrlPath (url) {
|
|
|
|
|
let reg = /^https*:\/\/(.*)\/(\w+\.(png|jpg|jpeg|webp))(\?.*)?$/
|
|
|
|
|
let ret = reg.exec(url)
|
2022-04-03 22:47:06 +00:00
|
|
|
|
if (!ret) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return false
|
2022-04-03 22:47:06 +00:00
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
path: ret[1],
|
|
|
|
|
filename: ret[2],
|
|
|
|
|
type: ret[3],
|
|
|
|
|
url
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-07-23 20:32:10 +00:00
|
|
|
|
pathExists (root, path) {
|
|
|
|
|
if (fs.existsSync(root + '/' + path)) {
|
|
|
|
|
return true
|
2022-04-03 22:47:06 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
path = path.replace('\\', '/')
|
|
|
|
|
const dirList = path.split('/')
|
|
|
|
|
let currentDir = root
|
2022-04-03 22:47:06 +00:00
|
|
|
|
|
|
|
|
|
for (let dir of dirList) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
currentDir = currentDir + '/' + dir
|
2022-04-03 22:47:06 +00:00
|
|
|
|
if (!fs.existsSync(currentDir)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
fs.mkdirSync(currentDir)
|
2022-04-03 22:47:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return true
|
2022-04-03 22:47:06 +00:00
|
|
|
|
},
|
2022-07-23 20:32:10 +00:00
|
|
|
|
async asyncPool (poolLimit, array, iteratorFn) {
|
|
|
|
|
const ret = [] // 存储所有的异步任务
|
|
|
|
|
const executing = [] // 存储正在执行的异步任务
|
2022-04-03 22:47:06 +00:00
|
|
|
|
for (const item of array) {
|
|
|
|
|
// 调用iteratorFn函数创建异步任务
|
2022-07-23 20:32:10 +00:00
|
|
|
|
const p = Promise.resolve().then(() => iteratorFn(item, array))
|
2022-04-03 22:47:06 +00:00
|
|
|
|
// 保存新的异步任务
|
2022-07-23 20:32:10 +00:00
|
|
|
|
ret.push(p)
|
2022-04-03 22:47:06 +00:00
|
|
|
|
|
|
|
|
|
// 当poolLimit值小于或等于总任务个数时,进行并发控制
|
|
|
|
|
if (poolLimit <= array.length) {
|
|
|
|
|
// 当任务完成后,从正在执行的任务数组中移除已完成的任务
|
2022-07-23 20:32:10 +00:00
|
|
|
|
const e = p.then(() => executing.splice(executing.indexOf(e), 1))
|
|
|
|
|
executing.push(e) // 保存正在执行的异步任务
|
2022-04-03 22:47:06 +00:00
|
|
|
|
if (executing.length >= poolLimit) {
|
|
|
|
|
// 等待较快的任务执行完成
|
2022-07-23 20:32:10 +00:00
|
|
|
|
await Promise.race(executing)
|
2022-04-03 22:47:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return Promise.all(ret)
|
2022-04-03 22:47:06 +00:00
|
|
|
|
},
|
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
sleep (ms) {
|
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
2022-06-30 18:53:05 +00:00
|
|
|
|
},
|
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
def () {
|
2022-06-30 18:53:05 +00:00
|
|
|
|
for (let idx in arguments) {
|
|
|
|
|
if (!lodash.isUndefined(arguments[idx])) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return arguments[idx]
|
2022-06-30 18:53:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
eachStr: (arr, fn) => {
|
|
|
|
|
if (lodash.isString(arr)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
arr = arr.replace(/\s*(;|;|、|,)\s*/, ',')
|
|
|
|
|
arr = arr.split(',')
|
2022-06-30 18:53:05 +00:00
|
|
|
|
}
|
|
|
|
|
lodash.forEach(arr, (str, idx) => {
|
|
|
|
|
if (!lodash.isUndefined(str)) {
|
|
|
|
|
fn(str.trim ? str.trim() : str, idx)
|
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
})
|
2022-04-03 22:47:06 +00:00
|
|
|
|
}
|
2022-04-03 22:06:03 +00:00
|
|
|
|
|
2022-04-03 22:47:06 +00:00
|
|
|
|
}
|
2022-04-03 22:06:03 +00:00
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
export default Data
|