ptSend/util/markdir.js
2023-06-14 22:02:50 +08:00

52 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
console.log(isExists)
//如果该路径且不是文件返回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) {
mkdirStatus = mkdir(dir);
}
return mkdirStatus;
}
module.exports = {
mkdirRecursions
}