版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

index.js 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. const {promisify} = require('util');
  3. const path = require('path');
  4. const globby = require('globby');
  5. const isGlob = require('is-glob');
  6. const slash = require('slash');
  7. const gracefulFs = require('graceful-fs');
  8. const isPathCwd = require('is-path-cwd');
  9. const isPathInside = require('is-path-inside');
  10. const rimraf = require('rimraf');
  11. const pMap = require('p-map');
  12. const rimrafP = promisify(rimraf);
  13. const rimrafOptions = {
  14. glob: false,
  15. unlink: gracefulFs.unlink,
  16. unlinkSync: gracefulFs.unlinkSync,
  17. chmod: gracefulFs.chmod,
  18. chmodSync: gracefulFs.chmodSync,
  19. stat: gracefulFs.stat,
  20. statSync: gracefulFs.statSync,
  21. lstat: gracefulFs.lstat,
  22. lstatSync: gracefulFs.lstatSync,
  23. rmdir: gracefulFs.rmdir,
  24. rmdirSync: gracefulFs.rmdirSync,
  25. readdir: gracefulFs.readdir,
  26. readdirSync: gracefulFs.readdirSync
  27. };
  28. function safeCheck(file, cwd) {
  29. if (isPathCwd(file)) {
  30. throw new Error('Cannot delete the current working directory. Can be overridden with the `force` option.');
  31. }
  32. if (!isPathInside(file, cwd)) {
  33. throw new Error('Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.');
  34. }
  35. }
  36. function normalizePatterns(patterns) {
  37. patterns = Array.isArray(patterns) ? patterns : [patterns];
  38. patterns = patterns.map(pattern => {
  39. if (process.platform === 'win32' && isGlob(pattern) === false) {
  40. return slash(pattern);
  41. }
  42. return pattern;
  43. });
  44. return patterns;
  45. }
  46. module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), onProgress = () => {}, ...options} = {}) => {
  47. options = {
  48. expandDirectories: false,
  49. onlyFiles: false,
  50. followSymbolicLinks: false,
  51. cwd,
  52. ...options
  53. };
  54. patterns = normalizePatterns(patterns);
  55. const files = (await globby(patterns, options))
  56. .sort((a, b) => b.localeCompare(a));
  57. if (files.length === 0) {
  58. onProgress({
  59. totalCount: 0,
  60. deletedCount: 0,
  61. percent: 1
  62. });
  63. }
  64. let deletedCount = 0;
  65. const mapper = async file => {
  66. file = path.resolve(cwd, file);
  67. if (!force) {
  68. safeCheck(file, cwd);
  69. }
  70. if (!dryRun) {
  71. await rimrafP(file, rimrafOptions);
  72. }
  73. deletedCount += 1;
  74. onProgress({
  75. totalCount: files.length,
  76. deletedCount,
  77. percent: deletedCount / files.length
  78. });
  79. return file;
  80. };
  81. const removedFiles = await pMap(files, mapper, options);
  82. removedFiles.sort((a, b) => a.localeCompare(b));
  83. return removedFiles;
  84. };
  85. module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
  86. options = {
  87. expandDirectories: false,
  88. onlyFiles: false,
  89. followSymbolicLinks: false,
  90. cwd,
  91. ...options
  92. };
  93. patterns = normalizePatterns(patterns);
  94. const files = globby.sync(patterns, options)
  95. .sort((a, b) => b.localeCompare(a));
  96. const removedFiles = files.map(file => {
  97. file = path.resolve(cwd, file);
  98. if (!force) {
  99. safeCheck(file, cwd);
  100. }
  101. if (!dryRun) {
  102. rimraf.sync(file, rimrafOptions);
  103. }
  104. return file;
  105. });
  106. removedFiles.sort((a, b) => a.localeCompare(b));
  107. return removedFiles;
  108. };