fs.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. //@ts-check
  6. const fs = require('fs');
  7. const path = require('path');
  8. const REPO_ROOT = path.join(__dirname, '../');
  9. const existingDirCache = new Set();
  10. /**
  11. * @param {string} dirname
  12. */
  13. function ensureDir(dirname) {
  14. /** @type {string[]} */
  15. const dirs = [];
  16. while (dirname.length > REPO_ROOT.length) {
  17. dirs.push(dirname);
  18. dirname = path.dirname(dirname);
  19. }
  20. dirs.reverse();
  21. dirs.forEach((dir) => {
  22. if (!existingDirCache.has(dir)) {
  23. try {
  24. fs.mkdirSync(dir);
  25. } catch (err) {}
  26. existingDirCache.add(dir);
  27. }
  28. });
  29. }
  30. exports.ensureDir = ensureDir;
  31. /**
  32. * Copy a file.
  33. *
  34. * @param {string} _source
  35. * @param {string} _destination
  36. */
  37. function copyFile(_source, _destination) {
  38. const source = path.join(REPO_ROOT, _source);
  39. const destination = path.join(REPO_ROOT, _destination);
  40. ensureDir(path.dirname(destination));
  41. fs.writeFileSync(destination, fs.readFileSync(source));
  42. console.log(`Copied ${_source} to ${_destination}`);
  43. }
  44. exports.copyFile = copyFile;
  45. /**
  46. * Remove a directory and all its contents.
  47. *
  48. * @param {string} _dirPath
  49. * @param {((filename:string)=>boolean)} [keep]
  50. */
  51. function removeDir(_dirPath, keep) {
  52. if (typeof keep === 'undefined') {
  53. keep = () => false;
  54. }
  55. const dirPath = path.join(REPO_ROOT, _dirPath);
  56. if (!fs.existsSync(dirPath)) {
  57. return;
  58. }
  59. rmDir(dirPath, _dirPath);
  60. console.log(`Deleted ${_dirPath}`);
  61. /**
  62. * @param {string} dirPath
  63. * @param {string} relativeDirPath
  64. * @returns {boolean}
  65. */
  66. function rmDir(dirPath, relativeDirPath) {
  67. let keepsFiles = false;
  68. const entries = fs.readdirSync(dirPath);
  69. for (const entry of entries) {
  70. const filePath = path.join(dirPath, entry);
  71. const relativeFilePath = path.join(relativeDirPath, entry);
  72. if (keep(relativeFilePath)) {
  73. keepsFiles = true;
  74. continue;
  75. }
  76. if (fs.statSync(filePath).isFile()) {
  77. fs.unlinkSync(filePath);
  78. } else {
  79. keepsFiles = rmDir(filePath, relativeFilePath) || keepsFiles;
  80. }
  81. }
  82. if (!keepsFiles) {
  83. fs.rmdirSync(dirPath);
  84. }
  85. return keepsFiles;
  86. }
  87. }
  88. exports.removeDir = removeDir;