2023-06-14 22:02:50 +08:00
|
|
|
|
const fs= require("fs")
|
|
|
|
|
const path = require("path")
|
|
|
|
|
/**
|
|
|
|
|
* 读取路径信息
|
|
|
|
|
* @param {string} path 路径
|
|
|
|
|
*/
|
|
|
|
|
function getStat(dir) {
|
|
|
|
|
try {
|
|
|
|
|
const stat = fs.statSync(dir);
|
|
|
|
|
return stat;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建路径
|
|
|
|
|
* @param {string} dir 路径
|
|
|
|
|
*/
|
|
|
|
|
function mkdir(dir) {
|
|
|
|
|
return fs.mkdirSync(dir)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 路径是否存在,不存在则创建
|
|
|
|
|
* @param {string} dir 路径
|
|
|
|
|
*/
|
|
|
|
|
function mkdirRecursions(dir) {
|
|
|
|
|
let isExists = getStat(dir);
|
2023-07-31 22:03:45 +08:00
|
|
|
|
console.log(isExists,dir)
|
2023-06-14 22:02:50 +08:00
|
|
|
|
//如果该路径且不是文件,返回true
|
|
|
|
|
if (isExists && isExists.isDirectory()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if(isExists){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
//如果该路径不存在,拿到上级路径
|
|
|
|
|
let tempDir = path.parse(dir).dir;
|
|
|
|
|
//递归判断,如果上级目录也不存在,则会代码会在此处继续循环执行,直到目录存在
|
|
|
|
|
let status = mkdirRecursions(tempDir);
|
|
|
|
|
let mkdirStatus;
|
|
|
|
|
if (status) {
|
2023-07-31 22:03:45 +08:00
|
|
|
|
console.log("mark",dir)
|
|
|
|
|
|
|
|
|
|
mkdir(dir);
|
|
|
|
|
mkdirStatus = true
|
2023-06-14 22:02:50 +08:00
|
|
|
|
}
|
|
|
|
|
return mkdirStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
mkdirRecursions
|
|
|
|
|
}
|