Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

sync.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. var core = require('./core');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var caller = require('./caller.js');
  5. var nodeModulesPaths = require('./node-modules-paths.js');
  6. var normalizeOptions = require('./normalize-options.js');
  7. var defaultIsFile = function isFile(file) {
  8. try {
  9. var stat = fs.statSync(file);
  10. } catch (e) {
  11. if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
  12. throw e;
  13. }
  14. return stat.isFile() || stat.isFIFO();
  15. };
  16. var defaultIsDir = function isDirectory(dir) {
  17. try {
  18. var stat = fs.statSync(dir);
  19. } catch (e) {
  20. if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
  21. throw e;
  22. }
  23. return stat.isDirectory();
  24. };
  25. module.exports = function (x, options) {
  26. if (typeof x !== 'string') {
  27. throw new TypeError('Path must be a string.');
  28. }
  29. var opts = normalizeOptions(x, options);
  30. var isFile = opts.isFile || defaultIsFile;
  31. var readFileSync = opts.readFileSync || fs.readFileSync;
  32. var isDirectory = opts.isDirectory || defaultIsDir;
  33. var extensions = opts.extensions || ['.js'];
  34. var basedir = opts.basedir || path.dirname(caller());
  35. var parent = opts.filename || basedir;
  36. opts.paths = opts.paths || [];
  37. // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
  38. var absoluteStart = path.resolve(basedir);
  39. if (opts.preserveSymlinks === false) {
  40. try {
  41. absoluteStart = fs.realpathSync(absoluteStart);
  42. } catch (realPathErr) {
  43. if (realPathErr.code !== 'ENOENT') {
  44. throw realPathErr;
  45. }
  46. }
  47. }
  48. if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
  49. var res = path.resolve(absoluteStart, x);
  50. if (x === '..' || x.slice(-1) === '/') res += '/';
  51. var m = loadAsFileSync(res) || loadAsDirectorySync(res);
  52. if (m) return m;
  53. } else if (core[x]) {
  54. return x;
  55. } else {
  56. var n = loadNodeModulesSync(x, absoluteStart);
  57. if (n) return n;
  58. }
  59. if (core[x]) return x;
  60. var err = new Error("Cannot find module '" + x + "' from '" + parent + "'");
  61. err.code = 'MODULE_NOT_FOUND';
  62. throw err;
  63. function loadAsFileSync(x) {
  64. var pkg = loadpkg(path.dirname(x));
  65. if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {
  66. var rfile = path.relative(pkg.dir, x);
  67. var r = opts.pathFilter(pkg.pkg, x, rfile);
  68. if (r) {
  69. x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign
  70. }
  71. }
  72. if (isFile(x)) {
  73. return x;
  74. }
  75. for (var i = 0; i < extensions.length; i++) {
  76. var file = x + extensions[i];
  77. if (isFile(file)) {
  78. return file;
  79. }
  80. }
  81. }
  82. function loadpkg(dir) {
  83. if (dir === '' || dir === '/') return;
  84. if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
  85. return;
  86. }
  87. if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
  88. var pkgfile = path.join(dir, 'package.json');
  89. if (!isFile(pkgfile)) {
  90. return loadpkg(path.dirname(dir));
  91. }
  92. var body = readFileSync(pkgfile);
  93. try {
  94. var pkg = JSON.parse(body);
  95. } catch (jsonErr) {}
  96. if (pkg && opts.packageFilter) {
  97. pkg = opts.packageFilter(pkg, dir);
  98. }
  99. return { pkg: pkg, dir: dir };
  100. }
  101. function loadAsDirectorySync(x) {
  102. var pkgfile = path.join(x, '/package.json');
  103. if (isFile(pkgfile)) {
  104. try {
  105. var body = readFileSync(pkgfile, 'UTF8');
  106. var pkg = JSON.parse(body);
  107. } catch (e) {}
  108. if (opts.packageFilter) {
  109. pkg = opts.packageFilter(pkg, x);
  110. }
  111. if (pkg.main) {
  112. if (typeof pkg.main !== 'string') {
  113. var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
  114. mainError.code = 'INVALID_PACKAGE_MAIN';
  115. throw mainError;
  116. }
  117. if (pkg.main === '.' || pkg.main === './') {
  118. pkg.main = 'index';
  119. }
  120. try {
  121. var m = loadAsFileSync(path.resolve(x, pkg.main));
  122. if (m) return m;
  123. var n = loadAsDirectorySync(path.resolve(x, pkg.main));
  124. if (n) return n;
  125. } catch (e) {}
  126. }
  127. }
  128. return loadAsFileSync(path.join(x, '/index'));
  129. }
  130. function loadNodeModulesSync(x, start) {
  131. var dirs = nodeModulesPaths(start, opts, x);
  132. for (var i = 0; i < dirs.length; i++) {
  133. var dir = dirs[i];
  134. if (isDirectory(dir)) {
  135. var m = loadAsFileSync(path.join(dir, '/', x));
  136. if (m) return m;
  137. var n = loadAsDirectorySync(path.join(dir, '/', x));
  138. if (n) return n;
  139. }
  140. }
  141. }
  142. };