Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. const fs = require('fs');
  3. const arrayUnion = require('array-union');
  4. const merge2 = require('merge2');
  5. const fastGlob = require('fast-glob');
  6. const dirGlob = require('dir-glob');
  7. const gitignore = require('./gitignore');
  8. const {FilterStream, UniqueStream} = require('./stream-utils');
  9. const DEFAULT_FILTER = () => false;
  10. const isNegative = pattern => pattern[0] === '!';
  11. const assertPatternsInput = patterns => {
  12. if (!patterns.every(pattern => typeof pattern === 'string')) {
  13. throw new TypeError('Patterns must be a string or an array of strings');
  14. }
  15. };
  16. const checkCwdOption = (options = {}) => {
  17. if (!options.cwd) {
  18. return;
  19. }
  20. let stat;
  21. try {
  22. stat = fs.statSync(options.cwd);
  23. } catch (_) {
  24. return;
  25. }
  26. if (!stat.isDirectory()) {
  27. throw new Error('The `cwd` option must be a path to a directory');
  28. }
  29. };
  30. const getPathString = p => p.stats instanceof fs.Stats ? p.path : p;
  31. const generateGlobTasks = (patterns, taskOptions) => {
  32. patterns = arrayUnion([].concat(patterns));
  33. assertPatternsInput(patterns);
  34. checkCwdOption(taskOptions);
  35. const globTasks = [];
  36. taskOptions = {
  37. ignore: [],
  38. expandDirectories: true,
  39. ...taskOptions
  40. };
  41. for (const [index, pattern] of patterns.entries()) {
  42. if (isNegative(pattern)) {
  43. continue;
  44. }
  45. const ignore = patterns
  46. .slice(index)
  47. .filter(isNegative)
  48. .map(pattern => pattern.slice(1));
  49. const options = {
  50. ...taskOptions,
  51. ignore: taskOptions.ignore.concat(ignore)
  52. };
  53. globTasks.push({pattern, options});
  54. }
  55. return globTasks;
  56. };
  57. const globDirs = (task, fn) => {
  58. let options = {};
  59. if (task.options.cwd) {
  60. options.cwd = task.options.cwd;
  61. }
  62. if (Array.isArray(task.options.expandDirectories)) {
  63. options = {
  64. ...options,
  65. files: task.options.expandDirectories
  66. };
  67. } else if (typeof task.options.expandDirectories === 'object') {
  68. options = {
  69. ...options,
  70. ...task.options.expandDirectories
  71. };
  72. }
  73. return fn(task.pattern, options);
  74. };
  75. const getPattern = (task, fn) => task.options.expandDirectories ? globDirs(task, fn) : [task.pattern];
  76. const getFilterSync = options => {
  77. return options && options.gitignore ?
  78. gitignore.sync({cwd: options.cwd, ignore: options.ignore}) :
  79. DEFAULT_FILTER;
  80. };
  81. const globToTask = task => glob => {
  82. const {options} = task;
  83. if (options.ignore && Array.isArray(options.ignore) && options.expandDirectories) {
  84. options.ignore = dirGlob.sync(options.ignore);
  85. }
  86. return {
  87. pattern: glob,
  88. options
  89. };
  90. };
  91. module.exports = async (patterns, options) => {
  92. const globTasks = generateGlobTasks(patterns, options);
  93. const getFilter = async () => {
  94. return options && options.gitignore ?
  95. gitignore({cwd: options.cwd, ignore: options.ignore}) :
  96. DEFAULT_FILTER;
  97. };
  98. const getTasks = async () => {
  99. const tasks = await Promise.all(globTasks.map(async task => {
  100. const globs = await getPattern(task, dirGlob);
  101. return Promise.all(globs.map(globToTask(task)));
  102. }));
  103. return arrayUnion(...tasks);
  104. };
  105. const [filter, tasks] = await Promise.all([getFilter(), getTasks()]);
  106. const paths = await Promise.all(tasks.map(task => fastGlob(task.pattern, task.options)));
  107. return arrayUnion(...paths).filter(path_ => !filter(getPathString(path_)));
  108. };
  109. module.exports.sync = (patterns, options) => {
  110. const globTasks = generateGlobTasks(patterns, options);
  111. const tasks = globTasks.reduce((tasks, task) => {
  112. const newTask = getPattern(task, dirGlob.sync).map(globToTask(task));
  113. return tasks.concat(newTask);
  114. }, []);
  115. const filter = getFilterSync(options);
  116. return tasks.reduce(
  117. (matches, task) => arrayUnion(matches, fastGlob.sync(task.pattern, task.options)),
  118. []
  119. ).filter(path_ => !filter(path_));
  120. };
  121. module.exports.stream = (patterns, options) => {
  122. const globTasks = generateGlobTasks(patterns, options);
  123. const tasks = globTasks.reduce((tasks, task) => {
  124. const newTask = getPattern(task, dirGlob.sync).map(globToTask(task));
  125. return tasks.concat(newTask);
  126. }, []);
  127. const filter = getFilterSync(options);
  128. const filterStream = new FilterStream(p => !filter(p));
  129. const uniqueStream = new UniqueStream();
  130. return merge2(tasks.map(task => fastGlob.stream(task.pattern, task.options)))
  131. .pipe(filterStream)
  132. .pipe(uniqueStream);
  133. };
  134. module.exports.generateGlobTasks = generateGlobTasks;
  135. module.exports.hasMagic = (patterns, options) => []
  136. .concat(patterns)
  137. .some(pattern => fastGlob.isDynamicPattern(pattern, options));
  138. module.exports.gitignore = gitignore;