項目/框架初始化時可能須要保留一些空文件,這時候就須要批量新增gitkeepnode
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) } }) }) }