writeFileTree.js 842 B

123456789101112131415161718192021222324252627
  1. const fs = require('fs-extra')
  2. const path = require('path')
  3. function deleteRemovedFiles (directory, newFiles, previousFiles) {
  4. // get all files that are not in the new filesystem and are still existing
  5. const filesToDelete = Object.keys(previousFiles)
  6. .filter(filename => !newFiles[filename])
  7. // delete each of these files
  8. return Promise.all(filesToDelete.map(filename => {
  9. return fs.unlink(path.join(directory, filename))
  10. }))
  11. }
  12. module.exports = async function writeFileTree (dir, files, previousFiles) {
  13. if (process.env.VUE_CLI_SKIP_WRITE) {
  14. return
  15. }
  16. if (previousFiles) {
  17. await deleteRemovedFiles(dir, files, previousFiles)
  18. }
  19. Object.keys(files).forEach((name) => {
  20. const filePath = path.join(dir, name)
  21. fs.ensureDirSync(path.dirname(filePath))
  22. fs.writeFileSync(filePath, files[name])
  23. })
  24. }