"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.readDirPath = readDirPath; exports.readDirGetFile = readDirGetFile; exports.parseJsonFiles = parseJsonFiles; exports.parseJsonFile = parseJsonFile; exports.deleteFolderRecursive = deleteFolderRecursive; exports.writeFile = writeFile; exports.readFile = readFile; exports.copyFolder = copyFolder; exports.checkFileExists = checkFileExists; exports.checkFileExistsAndCreate = checkFileExistsAndCreate; const fs_1 = require("fs"); const path_1 = require("path"); const enum_1 = require("./enum"); const tip_style_1 = require("./tip-style"); const pathList = new Set(); /** * @name 读取目录下所有文件 * @export * @param {string} entry 目录名称 */ function readDirPath(entry) { const dirInfo = (0, fs_1.readdirSync)(entry); for (let item of dirInfo) { const location = (0, path_1.join)(entry, item); const info = (0, fs_1.statSync)(location); if (info.isDirectory()) { readDirPath(location); } else { pathList.add(location); } } return pathList; } /** * @name 读取指定目录的文件(不进行深度遍历,只获取根目录) * @export * @param {*} entry * @returns */ function readDirGetFile(entry) { const dirInfo = (0, fs_1.readdirSync)(entry); return dirInfo; } /** * @name 解析json文件(数组) * @export * @param {Array} arr * @returns */ function parseJsonFiles(arr) { const result = []; for (let item of arr) { const data = parseJsonFile(item); result.push(data); } return result; } /** * @name 解析单个文件json * @export * @param {PathLike} file * @returns */ function parseJsonFile(file) { try { const data = (0, fs_1.readFileSync)(file, 'utf8'); return JSON.parse(data); } catch (error) { return; } } /** * @name 删除文件夹 * @export * @param {string} entry */ function deleteFolderRecursive(entry) { let files = []; // 判断给定的路径是否存在 if ((0, fs_1.existsSync)(entry)) { // 返回文件和子目录的数组 files = (0, fs_1.readdirSync)(entry); for (let file of files) { const curPath = (0, path_1.join)(entry, file); // fs.statSync同步读取文件夹文件,如果是文件夹,在重复触发函数 if ((0, fs_1.statSync)(curPath).isDirectory()) { // recurse deleteFolderRecursive(curPath); // 是文件delete file } else { (0, fs_1.unlinkSync)(curPath); } } // 清除文件夹 (0, fs_1.rmdirSync)(entry); } else { // console.log("文件夹不存在"); } } ; function writeFile(path, data) { try { (0, fs_1.writeFileSync)(path, data); } catch (err) { (0, tip_style_1.Error)((0, tip_style_1.error)(err)); (0, tip_style_1.Error)((0, tip_style_1.error)('文件写入失败')); } } function readFile(path, options) { try { const data = (0, fs_1.readFileSync)(path, options); return data; } catch (err) { (0, tip_style_1.Error)((0, tip_style_1.error)(err)); (0, tip_style_1.Error)((0, tip_style_1.error)('文件读取失败')); } } /** * @name 拷贝文件夹 * @export * @param {PathLike} currentDir * @param {PathLike} targetDir * @param {boolean} [overwrite=false] 是否覆盖已存在的文件 */ function copyFolder(currentDir, targetDir, overwrite = false) { function handleFolder(currentDir, targetDir) { const files = (0, fs_1.readdirSync)(currentDir, { withFileTypes: true }); for (let file of files) { const copyCurrentFileInfo = (0, path_1.join)(currentDir.toString(), file.name); const copyTargetFileInfo = (0, path_1.join)(targetDir.toString(), file.name); const readCurrentFile = (0, fs_1.existsSync)(copyCurrentFileInfo); const readTargetFile = (0, fs_1.existsSync)(copyTargetFileInfo); if (!readCurrentFile) { console.error(`操作失败,待拷贝的源路径${copyCurrentFileInfo}不存在`); throw new global.Error(`操作失败,待拷贝的源路径${copyCurrentFileInfo}不存在`); } else if (file.isFile() && readTargetFile && !overwrite) { // console.warn(`目标文件${copyTargetFileInfo}已存在,跳过复制`); continue; } else { if (file.isFile()) { // 使用同步复制确保文件完全写入 (0, fs_1.copyFileSync)(copyCurrentFileInfo, copyTargetFileInfo); // console.log(`复制文件: ${copyCurrentFileInfo} -> ${copyTargetFileInfo}`); } else { try { (0, fs_1.accessSync)((0, path_1.join)(copyTargetFileInfo, '..'), fs_1.constants.W_OK); if (!(0, fs_1.existsSync)(copyTargetFileInfo)) { (0, fs_1.mkdirSync)(copyTargetFileInfo); } // console.log(`创建文件夹: ${copyTargetFileInfo}`); copyFolder(copyCurrentFileInfo, copyTargetFileInfo, overwrite); } catch (error) { console.error('权限不足', error); throw error; } } } } } if ((0, fs_1.existsSync)(currentDir)) { if (!(0, fs_1.existsSync)(targetDir)) { (0, fs_1.mkdirSync)(targetDir); // console.log(`创建目标文件夹: ${targetDir}`); } handleFolder(currentDir, targetDir); } else { console.error(`需要copy的文件夹不存在: ${currentDir}`); throw new global.Error(`需要copy的文件夹不存在: ${currentDir}`); } } /** * @name 检测文件/文件夹是否存在 * @export * @param {(PathLike | string)} path * @returns */ function checkFileExists(path) { return (0, fs_1.existsSync)(path); } /** * @name 检测文件/文件夹是否存在,不存在则创建 * @export * @param {(PathLike | string)} path * @param {*} [data] * @param {checkFileExistsAndCreateType} [type=checkFileExistsAndCreateType.DIRECTORY] */ function checkFileExistsAndCreate(path, data, type = enum_1.checkFileExistsAndCreateType.DIRECTORY, overwrite) { if (!checkFileExists(path) || overwrite) { switch (type) { case enum_1.checkFileExistsAndCreateType.DIRECTORY: (0, fs_1.mkdirSync)(path); break; case enum_1.checkFileExistsAndCreateType.FILE: writeFile(path, data); break; default: (0, fs_1.mkdirSync)(path); break; } } else { throw new global.Error(`${path} already exists!`); } }