AppFileWalker.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AppFileWalker = exports.FileCopyHelper = void 0;
  4. const fs_extra_1 = require("fs-extra");
  5. const path = require("path");
  6. const nodeModulesSystemDependentSuffix = `${path.sep}node_modules`;
  7. function addAllPatternIfNeed(matcher) {
  8. if (!matcher.isSpecifiedAsEmptyArray && (matcher.isEmpty() || matcher.containsOnlyIgnore())) {
  9. matcher.prependPattern("**/*");
  10. }
  11. return matcher;
  12. }
  13. class FileCopyHelper {
  14. constructor(matcher, filter, packager) {
  15. this.matcher = matcher;
  16. this.filter = filter;
  17. this.packager = packager;
  18. this.metadata = new Map();
  19. }
  20. handleFile(file, parent, fileStat) {
  21. if (!fileStat.isSymbolicLink()) {
  22. return null;
  23. }
  24. return (0, fs_extra_1.readlink)(file).then((linkTarget) => {
  25. // http://unix.stackexchange.com/questions/105637/is-symlinks-target-relative-to-the-destinations-parent-directory-and-if-so-wh
  26. return this.handleSymlink(fileStat, file, parent, linkTarget);
  27. });
  28. }
  29. handleSymlink(fileStat, file, parent, linkTarget) {
  30. const resolvedLinkTarget = path.resolve(parent, linkTarget);
  31. const link = path.relative(this.matcher.from, resolvedLinkTarget);
  32. if (link.startsWith("..")) {
  33. // outside of project, linked module (https://github.com/electron-userland/electron-builder/issues/675)
  34. return (0, fs_extra_1.stat)(resolvedLinkTarget).then(targetFileStat => {
  35. this.metadata.set(file, targetFileStat);
  36. return targetFileStat;
  37. });
  38. }
  39. else {
  40. const s = fileStat;
  41. s.relativeLink = link;
  42. s.linkRelativeToFile = path.relative(parent, resolvedLinkTarget);
  43. }
  44. return null;
  45. }
  46. }
  47. exports.FileCopyHelper = FileCopyHelper;
  48. function createAppFilter(matcher, packager) {
  49. if (packager.areNodeModulesHandledExternally) {
  50. return matcher.isEmpty() ? null : matcher.createFilter();
  51. }
  52. const nodeModulesFilter = (file, fileStat) => {
  53. return !(fileStat.isDirectory() && file.endsWith(nodeModulesSystemDependentSuffix));
  54. };
  55. if (matcher.isEmpty()) {
  56. return nodeModulesFilter;
  57. }
  58. const filter = matcher.createFilter();
  59. return (file, fileStat) => {
  60. if (!nodeModulesFilter(file, fileStat)) {
  61. return !!packager.config.includeSubNodeModules;
  62. }
  63. return filter(file, fileStat);
  64. };
  65. }
  66. /** @internal */
  67. class AppFileWalker extends FileCopyHelper {
  68. constructor(matcher, packager) {
  69. super(addAllPatternIfNeed(matcher), createAppFilter(matcher, packager), packager);
  70. this.matcherFilter = matcher.createFilter();
  71. }
  72. // noinspection JSUnusedGlobalSymbols
  73. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  74. consume(file, fileStat, parent, siblingNames) {
  75. if (fileStat.isDirectory()) {
  76. // https://github.com/electron-userland/electron-builder/issues/1539
  77. // but do not filter if we inside node_modules dir
  78. // update: solution disabled, node module resolver should support such setup
  79. if (file.endsWith(nodeModulesSystemDependentSuffix)) {
  80. if (!this.packager.config.includeSubNodeModules) {
  81. const matchesFilter = this.matcherFilter(file, fileStat);
  82. if (!matchesFilter) {
  83. // Skip the file
  84. return false;
  85. }
  86. }
  87. }
  88. }
  89. else {
  90. // save memory - no need to store stat for directory
  91. this.metadata.set(file, fileStat);
  92. }
  93. return this.handleFile(file, parent, fileStat);
  94. }
  95. }
  96. exports.AppFileWalker = AppFileWalker;
  97. //# sourceMappingURL=AppFileWalker.js.map