ptSend/util/markdir.js

55 lines
1.1 KiB
JavaScript
Raw Permalink 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,dir)
//如果该路径且不是文件返回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) {
console.log("mark",dir)
mkdir(dir);
mkdirStatus = true
}
return mkdirStatus;
}
module.exports = {
mkdirRecursions
}