@netlify/zip-it-and-ship-it 5.3.0 → 5.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +1 -1
  2. package/dist/archive.js +16 -16
  3. package/dist/bin.js +13 -35
  4. package/dist/feature_flags.js +4 -1
  5. package/dist/main.d.ts +13 -4
  6. package/dist/main.js +54 -57
  7. package/dist/manifest.js +4 -13
  8. package/dist/runtimes/detect_runtime.js +16 -27
  9. package/dist/runtimes/go/builder.js +5 -14
  10. package/dist/runtimes/go/index.js +35 -47
  11. package/dist/runtimes/index.d.ts +8 -1
  12. package/dist/runtimes/index.js +54 -41
  13. package/dist/runtimes/node/bundlers/esbuild/bundler.js +81 -92
  14. package/dist/runtimes/node/bundlers/esbuild/index.js +14 -18
  15. package/dist/runtimes/node/bundlers/esbuild/plugin_dynamic_imports.js +13 -22
  16. package/dist/runtimes/node/bundlers/esbuild/plugin_native_modules.js +3 -13
  17. package/dist/runtimes/node/bundlers/esbuild/special_cases.js +8 -17
  18. package/dist/runtimes/node/bundlers/esbuild/src_files.js +28 -41
  19. package/dist/runtimes/node/bundlers/nft/es_modules.js +16 -22
  20. package/dist/runtimes/node/bundlers/nft/index.js +47 -60
  21. package/dist/runtimes/node/bundlers/nft/transpile.js +3 -12
  22. package/dist/runtimes/node/bundlers/zisi/index.js +7 -13
  23. package/dist/runtimes/node/bundlers/zisi/list_imports.js +5 -14
  24. package/dist/runtimes/node/bundlers/zisi/published.js +8 -19
  25. package/dist/runtimes/node/bundlers/zisi/resolve.js +37 -55
  26. package/dist/runtimes/node/bundlers/zisi/side_files.js +6 -17
  27. package/dist/runtimes/node/bundlers/zisi/src_files.js +58 -75
  28. package/dist/runtimes/node/bundlers/zisi/traverse.js +41 -56
  29. package/dist/runtimes/node/bundlers/zisi/tree_files.js +8 -19
  30. package/dist/runtimes/node/finder.d.ts +4 -6
  31. package/dist/runtimes/node/finder.js +48 -65
  32. package/dist/runtimes/node/in_source_config/index.js +5 -14
  33. package/dist/runtimes/node/index.js +65 -87
  34. package/dist/runtimes/node/parser/index.js +11 -20
  35. package/dist/runtimes/node/utils/detect_es_module.js +4 -15
  36. package/dist/runtimes/node/utils/included_files.js +3 -12
  37. package/dist/runtimes/node/utils/package_json.js +19 -27
  38. package/dist/runtimes/node/utils/zip.js +63 -94
  39. package/dist/runtimes/runtime.d.ts +7 -1
  40. package/dist/runtimes/rust/builder.js +23 -36
  41. package/dist/runtimes/rust/index.js +48 -60
  42. package/dist/utils/archive_size.js +5 -14
  43. package/dist/utils/format_result.js +6 -1
  44. package/dist/utils/fs.d.ts +6 -10
  45. package/dist/utils/fs.js +35 -63
  46. package/dist/zip.js +60 -73
  47. package/dist/zip_binary.js +5 -16
  48. package/package.json +7 -15
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -29,22 +20,20 @@ const tree_shake_1 = require("./tree_shake");
29
20
  // Retrieve the paths to the Node.js files to zip.
30
21
  // We only include the files actually needed by the function because AWS Lambda
31
22
  // has a size limit for the zipped file. It also makes cold starts faster.
32
- const getSrcFiles = function ({ config, featureFlags, mainFile, name, pluginsModulesPath, srcDir, srcPath, stat, }) {
33
- return __awaiter(this, void 0, void 0, function* () {
34
- const { includedFiles = [], includedFilesBasePath } = config;
35
- const { exclude: excludedPaths, paths: includedFilePaths } = yield (0, included_files_1.getPathsOfIncludedFiles)(includedFiles, includedFilesBasePath);
36
- const [treeFiles, depFiles] = yield Promise.all([
37
- (0, tree_files_1.getTreeFiles)(srcPath, stat),
38
- getDependencies({ featureFlags, functionName: name, mainFile, pluginsModulesPath, srcDir }),
39
- ]);
40
- const files = [...treeFiles, ...depFiles].map(path_1.normalize);
41
- const uniqueFiles = [...new Set(files)];
42
- // We sort so that the archive's checksum is deterministic.
43
- // Mutating is fine since `Array.filter()` returns a shallow copy
44
- const filteredFiles = uniqueFiles.filter(isNotJunk).sort();
45
- const includedPaths = (0, included_files_1.filterExcludedPaths)([...filteredFiles, ...includedFilePaths], excludedPaths);
46
- return includedPaths;
47
- });
23
+ const getSrcFiles = async function ({ config, featureFlags, mainFile, name, pluginsModulesPath, srcDir, srcPath, stat, }) {
24
+ const { includedFiles = [], includedFilesBasePath } = config;
25
+ const { exclude: excludedPaths, paths: includedFilePaths } = await (0, included_files_1.getPathsOfIncludedFiles)(includedFiles, includedFilesBasePath);
26
+ const [treeFiles, depFiles] = await Promise.all([
27
+ (0, tree_files_1.getTreeFiles)(srcPath, stat),
28
+ getDependencies({ featureFlags, functionName: name, mainFile, pluginsModulesPath, srcDir }),
29
+ ]);
30
+ const files = [...treeFiles, ...depFiles].map(path_1.normalize);
31
+ const uniqueFiles = [...new Set(files)];
32
+ // We sort so that the archive's checksum is deterministic.
33
+ // Mutating is fine since `Array.filter()` returns a shallow copy
34
+ const filteredFiles = uniqueFiles.filter(isNotJunk).sort();
35
+ const includedPaths = (0, included_files_1.filterExcludedPaths)([...filteredFiles, ...includedFilePaths], excludedPaths);
36
+ return includedPaths;
48
37
  };
49
38
  exports.getSrcFiles = getSrcFiles;
50
39
  // Remove temporary files like *~, *.swp, etc.
@@ -52,48 +41,44 @@ const isNotJunk = function (file) {
52
41
  return (0, junk_1.not)((0, path_1.basename)(file));
53
42
  };
54
43
  // Retrieve all the files recursively required by a Node.js file
55
- const getDependencies = function ({ featureFlags, functionName, mainFile, pluginsModulesPath, srcDir, }) {
56
- return __awaiter(this, void 0, void 0, function* () {
57
- const packageJson = yield (0, package_json_1.getPackageJson)(srcDir);
58
- const state = (0, traversal_cache_1.getNewCache)();
59
- try {
60
- return yield getFileDependencies({
61
- featureFlags,
62
- functionName,
63
- path: mainFile,
64
- packageJson,
65
- pluginsModulesPath,
66
- state,
67
- });
68
- }
69
- catch (error) {
70
- error.message = `In file "${mainFile}"\n${error.message}`;
71
- throw error;
72
- }
73
- });
74
- };
75
- const getFileDependencies = function ({ featureFlags, functionName, path, packageJson, pluginsModulesPath, state, treeShakeNext = false, }) {
76
- return __awaiter(this, void 0, void 0, function* () {
77
- if (state.localFiles.has(path)) {
78
- return [];
79
- }
80
- state.localFiles.add(path);
81
- const basedir = (0, path_1.dirname)(path);
82
- const dependencies = featureFlags.parseWithEsbuild
83
- ? yield (0, list_imports_1.listImports)({ functionName, path })
84
- : yield precinct_1.default.paperwork(path, { includeCore: false });
85
- const depsPaths = yield Promise.all(dependencies.filter(non_nullable_1.nonNullable).map((dependency) => getImportDependencies({
86
- dependency,
87
- basedir,
44
+ const getDependencies = async function ({ featureFlags, functionName, mainFile, pluginsModulesPath, srcDir, }) {
45
+ const packageJson = await (0, package_json_1.getPackageJson)(srcDir);
46
+ const state = (0, traversal_cache_1.getNewCache)();
47
+ try {
48
+ return await getFileDependencies({
88
49
  featureFlags,
89
50
  functionName,
51
+ path: mainFile,
90
52
  packageJson,
91
53
  pluginsModulesPath,
92
54
  state,
93
- treeShakeNext,
94
- })));
95
- return depsPaths.flat();
96
- });
55
+ });
56
+ }
57
+ catch (error) {
58
+ error.message = `In file "${mainFile}"\n${error.message}`;
59
+ throw error;
60
+ }
61
+ };
62
+ const getFileDependencies = async function ({ featureFlags, functionName, path, packageJson, pluginsModulesPath, state, treeShakeNext = false, }) {
63
+ if (state.localFiles.has(path)) {
64
+ return [];
65
+ }
66
+ state.localFiles.add(path);
67
+ const basedir = (0, path_1.dirname)(path);
68
+ const dependencies = featureFlags.parseWithEsbuild
69
+ ? await (0, list_imports_1.listImports)({ functionName, path })
70
+ : await precinct_1.default.paperwork(path, { includeCore: false });
71
+ const depsPaths = await Promise.all(dependencies.filter(non_nullable_1.nonNullable).map((dependency) => getImportDependencies({
72
+ dependency,
73
+ basedir,
74
+ featureFlags,
75
+ functionName,
76
+ packageJson,
77
+ pluginsModulesPath,
78
+ state,
79
+ treeShakeNext,
80
+ })));
81
+ return depsPaths.flat();
97
82
  };
98
83
  const getImportDependencies = function ({ dependency, basedir, featureFlags, functionName, packageJson, pluginsModulesPath, state, treeShakeNext, }) {
99
84
  const shouldTreeShakeNext = treeShakeNext || isNextOnNetlify(dependency);
@@ -115,20 +100,18 @@ const isNextOnNetlify = function (dependency) {
115
100
  return (0, path_1.basename)(dependency, '.js') === 'renderNextPage';
116
101
  };
117
102
  // When a file requires another one, we apply the top-level logic recursively
118
- const getTreeShakedDependencies = function ({ dependency, basedir, featureFlags, functionName, packageJson, pluginsModulesPath, state, treeShakeNext, }) {
119
- return __awaiter(this, void 0, void 0, function* () {
120
- const path = yield (0, resolve_1.resolvePathPreserveSymlinks)(dependency, [basedir, pluginsModulesPath].filter(non_nullable_1.nonNullable));
121
- const depsPath = yield getFileDependencies({
122
- featureFlags,
123
- functionName,
124
- path,
125
- packageJson,
126
- pluginsModulesPath,
127
- state,
128
- treeShakeNext,
129
- });
130
- return [path, ...depsPath];
103
+ const getTreeShakedDependencies = async function ({ dependency, basedir, featureFlags, functionName, packageJson, pluginsModulesPath, state, treeShakeNext, }) {
104
+ const path = await (0, resolve_1.resolvePathPreserveSymlinks)(dependency, [basedir, pluginsModulesPath].filter(non_nullable_1.nonNullable));
105
+ const depsPath = await getFileDependencies({
106
+ featureFlags,
107
+ functionName,
108
+ path,
109
+ packageJson,
110
+ pluginsModulesPath,
111
+ state,
112
+ treeShakeNext,
131
113
  });
114
+ return [path, ...depsPath];
132
115
  };
133
116
  /* eslint-enable max-lines */
134
117
  //# sourceMappingURL=src_files.js.map
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.getDependencyPathsForDependency = void 0;
13
4
  const path_1 = require("path");
@@ -20,58 +11,52 @@ const side_files_1 = require("./side_files");
20
11
  const EXCLUDED_MODULES = new Set(['aws-sdk']);
21
12
  // When a file requires a module, we find its path inside `node_modules` and
22
13
  // use all its published files. We also recurse on the module's dependencies.
23
- const getDependencyPathsForDependency = function ({ dependency, basedir, state, packageJson, pluginsModulesPath, }) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- const moduleName = (0, module_1.getModuleName)(dependency);
26
- // Happens when doing require("@scope") (not "@scope/name") or other oddities
27
- // Ignore those.
28
- if (moduleName === null) {
29
- return [];
30
- }
31
- try {
32
- return yield getDependenciesForModuleName({ moduleName, basedir, state, pluginsModulesPath });
33
- }
34
- catch (error) {
35
- return (0, nested_1.handleModuleNotFound)({ error, moduleName, packageJson });
36
- }
37
- });
14
+ const getDependencyPathsForDependency = async function ({ dependency, basedir, state, packageJson, pluginsModulesPath, }) {
15
+ const moduleName = (0, module_1.getModuleName)(dependency);
16
+ // Happens when doing require("@scope") (not "@scope/name") or other oddities
17
+ // Ignore those.
18
+ if (moduleName === null) {
19
+ return [];
20
+ }
21
+ try {
22
+ return await getDependenciesForModuleName({ moduleName, basedir, state, pluginsModulesPath });
23
+ }
24
+ catch (error) {
25
+ return (0, nested_1.handleModuleNotFound)({ error, moduleName, packageJson });
26
+ }
38
27
  };
39
28
  exports.getDependencyPathsForDependency = getDependencyPathsForDependency;
40
- const getDependenciesForModuleName = function ({ moduleName, basedir, state, pluginsModulesPath, }) {
41
- return __awaiter(this, void 0, void 0, function* () {
42
- if (isExcludedModule(moduleName)) {
43
- return [];
44
- }
45
- // Find the Node.js module directory path
46
- const packagePath = yield (0, resolve_1.resolvePackage)(moduleName, [basedir, pluginsModulesPath].filter(non_nullable_1.nonNullable));
47
- if (packagePath === undefined) {
48
- return [];
49
- }
50
- const modulePath = (0, path_1.dirname)(packagePath);
51
- if (state.modulePaths.has(modulePath)) {
52
- return [];
53
- }
54
- state.moduleNames.add(moduleName);
55
- state.modulePaths.add(modulePath);
56
- // The path depends on the user's build, i.e. must be dynamic
57
- // eslint-disable-next-line import/no-dynamic-require, node/global-require, @typescript-eslint/no-var-requires
58
- const packageJson = require(packagePath);
59
- const [publishedFiles, sideFiles, depsPaths] = yield Promise.all([
60
- (0, published_1.getPublishedFiles)(modulePath),
61
- (0, side_files_1.getSideFiles)(modulePath, moduleName),
62
- getNestedModules({ modulePath, state, packageJson, pluginsModulesPath }),
63
- ]);
64
- return [...publishedFiles, ...sideFiles, ...depsPaths];
65
- });
29
+ const getDependenciesForModuleName = async function ({ moduleName, basedir, state, pluginsModulesPath, }) {
30
+ if (isExcludedModule(moduleName)) {
31
+ return [];
32
+ }
33
+ // Find the Node.js module directory path
34
+ const packagePath = await (0, resolve_1.resolvePackage)(moduleName, [basedir, pluginsModulesPath].filter(non_nullable_1.nonNullable));
35
+ if (packagePath === undefined) {
36
+ return [];
37
+ }
38
+ const modulePath = (0, path_1.dirname)(packagePath);
39
+ if (state.modulePaths.has(modulePath)) {
40
+ return [];
41
+ }
42
+ state.moduleNames.add(moduleName);
43
+ state.modulePaths.add(modulePath);
44
+ // The path depends on the user's build, i.e. must be dynamic
45
+ // eslint-disable-next-line import/no-dynamic-require, node/global-require, @typescript-eslint/no-var-requires
46
+ const packageJson = require(packagePath);
47
+ const [publishedFiles, sideFiles, depsPaths] = await Promise.all([
48
+ (0, published_1.getPublishedFiles)(modulePath),
49
+ (0, side_files_1.getSideFiles)(modulePath, moduleName),
50
+ getNestedModules({ modulePath, state, packageJson, pluginsModulesPath }),
51
+ ]);
52
+ return [...publishedFiles, ...sideFiles, ...depsPaths];
66
53
  };
67
54
  const isExcludedModule = function (moduleName) {
68
55
  return EXCLUDED_MODULES.has(moduleName) || moduleName.startsWith('@types/');
69
56
  };
70
- const getNestedModules = function ({ modulePath, state, packageJson, pluginsModulesPath, }) {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- const dependencies = (0, nested_1.getNestedDependencies)(packageJson);
73
- const depsPaths = yield Promise.all(dependencies.map((dependency) => getDependencyPathsForDependency({ dependency, basedir: modulePath, state, packageJson, pluginsModulesPath })));
74
- return depsPaths.flat();
75
- });
57
+ const getNestedModules = async function ({ modulePath, state, packageJson, pluginsModulesPath, }) {
58
+ const dependencies = (0, nested_1.getNestedDependencies)(packageJson);
59
+ const depsPaths = await Promise.all(dependencies.map((dependency) => getDependencyPathsForDependency({ dependency, basedir: modulePath, state, packageJson, pluginsModulesPath })));
60
+ return depsPaths.flat();
76
61
  };
77
62
  //# sourceMappingURL=traverse.js.map
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -17,16 +8,14 @@ const util_1 = require("util");
17
8
  const glob_1 = __importDefault(require("glob"));
18
9
  const pGlob = (0, util_1.promisify)(glob_1.default);
19
10
  // When using a directory, we include all its descendants except `node_modules`
20
- const getTreeFiles = function (srcPath, stat) {
21
- return __awaiter(this, void 0, void 0, function* () {
22
- if (!stat.isDirectory()) {
23
- return [srcPath];
24
- }
25
- return yield pGlob(`${srcPath}/**`, {
26
- ignore: `${srcPath}/**/node_modules/**`,
27
- nodir: true,
28
- absolute: true,
29
- });
11
+ const getTreeFiles = async function (srcPath, stat) {
12
+ if (!stat.isDirectory()) {
13
+ return [srcPath];
14
+ }
15
+ return await pGlob(`${srcPath}/**`, {
16
+ ignore: `${srcPath}/**/node_modules/**`,
17
+ nodir: true,
18
+ absolute: true,
30
19
  });
31
20
  };
32
21
  exports.getTreeFiles = getTreeFiles;
@@ -1,6 +1,4 @@
1
- import { SourceFile } from '../../function';
2
- declare const findFunctionsInPaths: ({ paths }: {
3
- paths: string[];
4
- }) => Promise<SourceFile[]>;
5
- declare const getFunctionAtPath: (srcPath: string) => Promise<SourceFile | undefined>;
6
- export { findFunctionsInPaths, getFunctionAtPath };
1
+ import { FindFunctionsInPathsFunction, FindFunctionInPathFunction } from '../runtime';
2
+ declare const findFunctionsInPaths: FindFunctionsInPathsFunction;
3
+ declare const findFunctionInPath: FindFunctionInPathFunction;
4
+ export { findFunctionsInPaths, findFunctionInPath };
@@ -1,24 +1,13 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
14
5
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.getFunctionAtPath = exports.findFunctionsInPaths = void 0;
6
+ exports.findFunctionInPath = exports.findFunctionsInPaths = void 0;
16
7
  const fs_1 = require("fs");
17
8
  const path_1 = require("path");
18
- const util_1 = require("util");
19
9
  const locate_path_1 = __importDefault(require("locate-path"));
20
10
  const non_nullable_1 = require("../../utils/non_nullable");
21
- const pLstat = (0, util_1.promisify)(fs_1.lstat);
22
11
  // List of extensions that this runtime will look for, in order of precedence.
23
12
  const allowedExtensions = ['.js', '.zip', '.cjs', '.mjs', '.ts'];
24
13
  // Sorting function, compatible with the callback of Array.sort, which sorts
@@ -30,64 +19,58 @@ const sortByExtension = (fA, fB) => {
30
19
  const indexB = allowedExtensions.indexOf(fB.extension);
31
20
  return indexB - indexA;
32
21
  };
33
- const findFunctionsInPaths = function ({ paths }) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- const functions = yield Promise.all(paths.map(getFunctionAtPath));
36
- // It's fine to mutate the array since its scope is local to this function.
37
- const sortedFunctions = functions.filter(non_nullable_1.nonNullable).sort((fA, fB) => {
38
- // We first sort the functions array to put directories first. This is so
39
- // that `{name}/{name}.js` takes precedence over `{name}.js`.
40
- const directorySort = Number(fA.stat.isDirectory()) - Number(fB.stat.isDirectory());
41
- if (directorySort !== 0) {
42
- return directorySort;
43
- }
44
- // If the functions have the same name, we sort them according to the order
45
- // defined in `allowedExtensions`.
46
- if (fA.name === fB.name) {
47
- return sortByExtension(fA, fB);
48
- }
49
- return 0;
50
- });
51
- return sortedFunctions;
52
- });
53
- };
54
- exports.findFunctionsInPaths = findFunctionsInPaths;
55
- const getFunctionAtPath = function (srcPath) {
56
- return __awaiter(this, void 0, void 0, function* () {
57
- const filename = (0, path_1.basename)(srcPath);
58
- if (filename === 'node_modules') {
59
- return;
22
+ const findFunctionsInPaths = async function ({ paths, fsCache, featureFlags }) {
23
+ const functions = await Promise.all(paths.map((path) => findFunctionInPath({ path, fsCache, featureFlags })));
24
+ // It's fine to mutate the array since its scope is local to this function.
25
+ const sortedFunctions = functions.filter(non_nullable_1.nonNullable).sort((fA, fB) => {
26
+ // We first sort the functions array to put directories first. This is so
27
+ // that `{name}/{name}.js` takes precedence over `{name}.js`.
28
+ const directorySort = Number(fA.stat.isDirectory()) - Number(fB.stat.isDirectory());
29
+ if (directorySort !== 0) {
30
+ return directorySort;
60
31
  }
61
- const stat = yield pLstat(srcPath);
62
- const mainFile = yield getMainFile(srcPath, filename, stat);
63
- if (mainFile === undefined) {
64
- return;
32
+ // If the functions have the same name, we sort them according to the order
33
+ // defined in `allowedExtensions`.
34
+ if (fA.name === fB.name) {
35
+ return sortByExtension(fA, fB);
65
36
  }
66
- const extension = (0, path_1.extname)(srcPath);
67
- const srcDir = stat.isDirectory() ? srcPath : (0, path_1.dirname)(srcPath);
68
- const name = (0, path_1.basename)(srcPath, (0, path_1.extname)(srcPath));
69
- return { extension, filename, mainFile, name, srcDir, srcPath, stat };
37
+ return 0;
70
38
  });
39
+ return sortedFunctions;
71
40
  };
72
- exports.getFunctionAtPath = getFunctionAtPath;
41
+ exports.findFunctionsInPaths = findFunctionsInPaths;
42
+ const findFunctionInPath = async function ({ path: srcPath }) {
43
+ const filename = (0, path_1.basename)(srcPath);
44
+ if (filename === 'node_modules') {
45
+ return;
46
+ }
47
+ const stat = await fs_1.promises.lstat(srcPath);
48
+ const mainFile = await getMainFile(srcPath, filename, stat);
49
+ if (mainFile === undefined) {
50
+ return;
51
+ }
52
+ const extension = (0, path_1.extname)(srcPath);
53
+ const srcDir = stat.isDirectory() ? srcPath : (0, path_1.dirname)(srcPath);
54
+ const name = (0, path_1.basename)(srcPath, (0, path_1.extname)(srcPath));
55
+ return { extension, filename, mainFile, name, srcDir, srcPath, stat };
56
+ };
57
+ exports.findFunctionInPath = findFunctionInPath;
73
58
  // Each `srcPath` can also be a directory with an `index` file or a file using
74
59
  // the same filename as its directory.
75
- const getMainFile = function (srcPath, filename, stat) {
76
- return __awaiter(this, void 0, void 0, function* () {
77
- if (stat.isDirectory()) {
78
- return yield (0, locate_path_1.default)([
79
- (0, path_1.join)(srcPath, `${filename}.js`),
80
- (0, path_1.join)(srcPath, 'index.js'),
81
- (0, path_1.join)(srcPath, `${filename}.mjs`),
82
- (0, path_1.join)(srcPath, 'index.mjs'),
83
- (0, path_1.join)(srcPath, `${filename}.ts`),
84
- (0, path_1.join)(srcPath, 'index.ts'),
85
- ], { type: 'file' });
86
- }
87
- const extension = (0, path_1.extname)(srcPath);
88
- if (allowedExtensions.includes(extension)) {
89
- return srcPath;
90
- }
91
- });
60
+ const getMainFile = async function (srcPath, filename, stat) {
61
+ if (stat.isDirectory()) {
62
+ return await (0, locate_path_1.default)([
63
+ (0, path_1.join)(srcPath, `${filename}.js`),
64
+ (0, path_1.join)(srcPath, 'index.js'),
65
+ (0, path_1.join)(srcPath, `${filename}.mjs`),
66
+ (0, path_1.join)(srcPath, 'index.mjs'),
67
+ (0, path_1.join)(srcPath, `${filename}.ts`),
68
+ (0, path_1.join)(srcPath, 'index.ts'),
69
+ ], { type: 'file' });
70
+ }
71
+ const extension = (0, path_1.extname)(srcPath);
72
+ if (allowedExtensions.includes(extension)) {
73
+ return srcPath;
74
+ }
92
75
  };
93
76
  //# sourceMappingURL=finder.js.map
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.IN_SOURCE_CONFIG_MODULE = exports.findISCDeclarationsInPath = void 0;
13
4
  const non_nullable_1 = require("../../../utils/non_nullable");
@@ -20,12 +11,12 @@ exports.IN_SOURCE_CONFIG_MODULE = IN_SOURCE_CONFIG_MODULE;
20
11
  // Parses a JS/TS file and looks for in-source config declarations. It returns
21
12
  // an array of all declarations found, with `property` indicating the name of
22
13
  // the property and `data` its value.
23
- const findISCDeclarationsInPath = (sourcePath) => __awaiter(void 0, void 0, void 0, function* () {
24
- const ast = yield (0, parser_1.safelyParseFile)(sourcePath);
14
+ const findISCDeclarationsInPath = async (sourcePath) => {
15
+ const ast = await (0, parser_1.safelyParseFile)(sourcePath);
25
16
  if (ast === null) {
26
17
  return {};
27
18
  }
28
- const imports = ast.body.map((node) => (0, imports_1.getImports)(node, IN_SOURCE_CONFIG_MODULE)).flat();
19
+ const imports = ast.body.flatMap((node) => (0, imports_1.getImports)(node, IN_SOURCE_CONFIG_MODULE));
29
20
  const exports = (0, exports_1.getMainExport)(ast.body);
30
21
  const iscExports = exports
31
22
  .map(({ args, local: exportName }) => {
@@ -42,8 +33,8 @@ const findISCDeclarationsInPath = (sourcePath) => __awaiter(void 0, void 0, void
42
33
  return null;
43
34
  })
44
35
  .filter(non_nullable_1.nonNullable);
45
- const mergedExports = iscExports.reduce((acc, obj) => (Object.assign(Object.assign({}, acc), obj)), {});
36
+ const mergedExports = iscExports.reduce((acc, obj) => ({ ...acc, ...obj }), {});
46
37
  return mergedExports;
47
- });
38
+ };
48
39
  exports.findISCDeclarationsInPath = findISCDeclarationsInPath;
49
40
  //# sourceMappingURL=index.js.map