deleteFile.js 644 B

12345678910111213141516171819202122232425
  1. import fs from 'fs'
  2. import path from 'path'
  3. export default function deleteFile(url, excludes = []) {
  4. let files = []
  5. if (fs.existsSync(url)) {
  6. files = fs.readdirSync(url)
  7. files.forEach(function (file, index) {
  8. if (excludes.indexOf(file) === -1) {
  9. const curPath = path.join(url, file)
  10. if (fs.statSync(curPath).isDirectory()) {
  11. deleteFile(curPath)
  12. } else {
  13. if (curPath.endsWith('.html.html')) {
  14. fs.unlinkSync(curPath)
  15. console.log('已删除文件:', file)
  16. }
  17. }
  18. }
  19. })
  20. } else {
  21. console.log('给定的路径不存在!')
  22. }
  23. }