时间:2021-08-31 10:00:38 | 栏目:NodeJS | 点击:次
项目/框架初始化时可能需要保留一些空文件,这时候就需要批量新增gitkeep
const fs = require('fs') const baseurl = 'D:/test' const ignoreDir = ['.git', '.vscode', 'node_modules'] addGitkeep(baseurl) function addGitkeep(url) { fs.readdir(url, {withFileTypes: true}, (err, files) => { err && console.log(err) //该目录下没有文件 if (!files.length) { return fs.writeFile(url + '/.gitkeep', null, err => { err && console.log(err) }) } files.forEach(dirent => { if (!ignoreDir.includes(dirent.name) && dirent.isDirectory()) { addGitkeep(url + '/' + dirent.name) } }) }) }