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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. const {promisify} = require('util');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const fastGlob = require('fast-glob');
  6. const gitIgnore = require('ignore');
  7. const slash = require('slash');
  8. const DEFAULT_IGNORE = [
  9. '**/node_modules/**',
  10. '**/flow-typed/**',
  11. '**/coverage/**',
  12. '**/.git'
  13. ];
  14. const readFileP = promisify(fs.readFile);
  15. const mapGitIgnorePatternTo = base => ignore => {
  16. if (ignore.startsWith('!')) {
  17. return '!' + path.posix.join(base, ignore.slice(1));
  18. }
  19. return path.posix.join(base, ignore);
  20. };
  21. const parseGitIgnore = (content, options) => {
  22. const base = slash(path.relative(options.cwd, path.dirname(options.fileName)));
  23. return content
  24. .split(/\r?\n/)
  25. .filter(Boolean)
  26. .filter(line => !line.startsWith('#'))
  27. .map(mapGitIgnorePatternTo(base));
  28. };
  29. const reduceIgnore = files => {
  30. return files.reduce((ignores, file) => {
  31. ignores.add(parseGitIgnore(file.content, {
  32. cwd: file.cwd,
  33. fileName: file.filePath
  34. }));
  35. return ignores;
  36. }, gitIgnore());
  37. };
  38. const ensureAbsolutePathForCwd = (cwd, p) => {
  39. cwd = slash(cwd);
  40. if (path.isAbsolute(p)) {
  41. if (p.startsWith(cwd)) {
  42. return p;
  43. }
  44. throw new Error(`Path ${p} is not in cwd ${cwd}`);
  45. }
  46. return path.join(cwd, p);
  47. };
  48. const getIsIgnoredPredecate = (ignores, cwd) => {
  49. return p => ignores.ignores(slash(path.relative(cwd, ensureAbsolutePathForCwd(cwd, p))));
  50. };
  51. const getFile = async (file, cwd) => {
  52. const filePath = path.join(cwd, file);
  53. const content = await readFileP(filePath, 'utf8');
  54. return {
  55. cwd,
  56. filePath,
  57. content
  58. };
  59. };
  60. const getFileSync = (file, cwd) => {
  61. const filePath = path.join(cwd, file);
  62. const content = fs.readFileSync(filePath, 'utf8');
  63. return {
  64. cwd,
  65. filePath,
  66. content
  67. };
  68. };
  69. const normalizeOptions = ({
  70. ignore = [],
  71. cwd = slash(process.cwd())
  72. } = {}) => {
  73. return {ignore, cwd};
  74. };
  75. module.exports = async options => {
  76. options = normalizeOptions(options);
  77. const paths = await fastGlob('**/.gitignore', {
  78. ignore: DEFAULT_IGNORE.concat(options.ignore),
  79. cwd: options.cwd
  80. });
  81. const files = await Promise.all(paths.map(file => getFile(file, options.cwd)));
  82. const ignores = reduceIgnore(files);
  83. return getIsIgnoredPredecate(ignores, options.cwd);
  84. };
  85. module.exports.sync = options => {
  86. options = normalizeOptions(options);
  87. const paths = fastGlob.sync('**/.gitignore', {
  88. ignore: DEFAULT_IGNORE.concat(options.ignore),
  89. cwd: options.cwd
  90. });
  91. const files = paths.map(file => getFileSync(file, options.cwd));
  92. const ignores = reduceIgnore(files);
  93. return getIsIgnoredPredecate(ignores, options.cwd);
  94. };