js
function writeFileSyncRecursive(filename, content = '') {
fs.mkdirSync(path.dirname(filename), {recursive: true})
fs.writeFileSync(filename, content)
}
function writeFileSyncRecursive(filename, content = '') {
fs.mkdirSync(path.dirname(filename), {recursive: true})
fs.writeFileSync(filename, content)
}
path.dirname
返回的是除了路径中的文件名(包括文件扩展名)之外的部分。例如,path.dirname('/home/user/documents/file.txt')
将返回'/home/user/documents'
- mkdirSync 创建目录,支持递归创建需要的父级目录
参考: gist