请求API抽离出独立方法 requestUtils
This commit is contained in:
49
src/utils/fileUtils.js
Normal file
49
src/utils/fileUtils.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const fs = require('fs');
|
||||
|
||||
const LATEST_DATA_ONLY = process.env.LATEST_DATA_ONLY == true;
|
||||
|
||||
// 创建目录
|
||||
async function createFolder(folderToCreate) {
|
||||
let currentFolder = folderToCreate.replace(/\\/g, '/');
|
||||
let parentFolder = currentFolder.substring(0, currentFolder.lastIndexOf('/'));
|
||||
if (!fs.existsSync(currentFolder)) {
|
||||
// 文件夹不存在,创建文件夹
|
||||
createFolder(parentFolder); // 保证父级文件夹存在
|
||||
fs.mkdirSync(currentFolder); // 创建当前级文件夹
|
||||
} else {
|
||||
// 否则就什么也不做
|
||||
}
|
||||
}
|
||||
|
||||
// 保存 JSON
|
||||
function saveJSON({ saveFolder, now, fileNameSuffix, object, compress = true, uncompress = true }) {
|
||||
if (LATEST_DATA_ONLY) return;
|
||||
|
||||
let year = now.substring(0, 4);
|
||||
let month = now.substring(5, 7);
|
||||
let day = now.substring(8, 10);
|
||||
let hour = now.substring(11, 13);
|
||||
let minute = now.substring(14, 16);
|
||||
// console.log(now);
|
||||
// console.log( "year, month, day, hour, minute: " + year + ", " + month + ", " + day + ", " + hour + ", " + minute);
|
||||
|
||||
// 创建当前文件夹
|
||||
let folder = `${saveFolder}/${fileNameSuffix}/${year}/${month}/${day}`;
|
||||
createFolder(folder);
|
||||
let fileName = `${folder}/${year}${month}${day}_${hour}${minute}`;
|
||||
|
||||
// 生成文件名
|
||||
// '2022-07-23T10:11:38.650Z' => '20220723_1011'
|
||||
// let fileName = now.replace(/T/, '_').replace(/:\d{2}.\d{3}Z/, '').replace(/[-:]/g, '');
|
||||
// console.log(`fileName is ${fileName}`);
|
||||
|
||||
if (compress)
|
||||
fs.writeFileSync(`${fileName}.min.json`, JSON.stringify(object));
|
||||
if (uncompress)
|
||||
fs.writeFileSync(`${fileName}.json`, JSON.stringify(object, "", "\t"));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createFolder,
|
||||
saveJSON,
|
||||
}
|
Reference in New Issue
Block a user