@netlify/zip-it-and-ship-it 5.0.0 → 5.4.0-isc-listfunctions

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.
@@ -6,7 +6,6 @@ const FLAGS = {
6
6
  buildGoSource: Boolean(process_1.env.NETLIFY_EXPERIMENTAL_BUILD_GO_SOURCE),
7
7
  buildRustSource: Boolean(process_1.env.NETLIFY_EXPERIMENTAL_BUILD_RUST_SOURCE),
8
8
  defaultEsModulesToEsbuild: Boolean(process_1.env.NETLIFY_EXPERIMENTAL_DEFAULT_ES_MODULES_TO_ESBUILD),
9
- nftTranspile: false,
10
9
  parseWithEsbuild: false,
11
10
  traceWithNft: false,
12
11
  };
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { Stats } from 'fs';
3
3
  import type { FunctionConfig } from './config';
4
+ import { ISCValues } from './runtimes/node/in_source_config';
4
5
  import type { Runtime, ZipFunctionResult } from './runtimes/runtime';
5
6
  declare type FunctionArchive = ZipFunctionResult & {
6
7
  mainFile: string;
@@ -16,6 +17,7 @@ interface SourceFile {
16
17
  srcDir: string;
17
18
  srcPath: string;
18
19
  stat: Stats;
20
+ inSourceConfig?: ISCValues;
19
21
  }
20
22
  declare type FunctionSource = SourceFile & {
21
23
  config: FunctionConfig;
package/dist/main.d.ts CHANGED
@@ -6,6 +6,7 @@ interface ListedFunction {
6
6
  mainFile: string;
7
7
  runtime: RuntimeName;
8
8
  extension: string;
9
+ schedule?: string;
9
10
  }
10
11
  declare type ListedFunctionFile = ListedFunction & {
11
12
  srcFile: string;
@@ -15,9 +16,14 @@ interface ListFunctionsOptions {
15
16
  config?: Config;
16
17
  featureFlags?: FeatureFlags;
17
18
  }
18
- declare const listFunctions: (relativeSrcFolders: string | string[], { featureFlags: inputFeatureFlags }?: {
19
- featureFlags?: FeatureFlags;
19
+ declare const listFunctions: (relativeSrcFolders: string | string[], { featureFlags: inputFeatureFlags, config }?: {
20
+ featureFlags?: FeatureFlags | undefined;
21
+ config?: Config | undefined;
20
22
  }) => Promise<ListedFunction[]>;
23
+ declare const listFunction: (mainFile: string, { featureFlags: inputFeatureFlags, config }?: {
24
+ featureFlags?: FeatureFlags | undefined;
25
+ config?: Config | undefined;
26
+ }) => Promise<ListedFunction | undefined>;
21
27
  declare const listFunctionsFiles: (relativeSrcFolders: string | string[], { basePath, config, featureFlags: inputFeatureFlags }?: ListFunctionsOptions) => Promise<ListedFunctionFile[]>;
22
- export { listFunctions, listFunctionsFiles };
28
+ export { listFunctions, listFunction, listFunctionsFiles };
23
29
  export { zipFunction, zipFunctions } from './zip';
package/dist/main.js CHANGED
@@ -20,23 +20,35 @@ var __rest = (this && this.__rest) || function (s, e) {
20
20
  return t;
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.zipFunctions = exports.zipFunction = exports.listFunctionsFiles = exports.listFunctions = void 0;
23
+ exports.zipFunctions = exports.zipFunction = exports.listFunctionsFiles = exports.listFunction = exports.listFunctions = void 0;
24
24
  const path_1 = require("path");
25
25
  const feature_flags_1 = require("./feature_flags");
26
26
  const runtimes_1 = require("./runtimes");
27
27
  const fs_1 = require("./utils/fs");
28
28
  // List all Netlify Functions main entry files for a specific directory
29
- const listFunctions = function (relativeSrcFolders, { featureFlags: inputFeatureFlags } = {}) {
29
+ const listFunctions = function (relativeSrcFolders, { featureFlags: inputFeatureFlags, config } = {}) {
30
30
  return __awaiter(this, void 0, void 0, function* () {
31
31
  const featureFlags = (0, feature_flags_1.getFlags)(inputFeatureFlags);
32
32
  const srcFolders = (0, fs_1.resolveFunctionsDirectories)(relativeSrcFolders);
33
33
  const paths = yield (0, fs_1.listFunctionsDirectories)(srcFolders);
34
- const functions = yield (0, runtimes_1.getFunctionsFromPaths)(paths, { featureFlags });
34
+ const functions = yield (0, runtimes_1.getFunctionsFromPaths)(paths, { featureFlags, config });
35
35
  const listedFunctions = [...functions.values()].map(getListedFunction);
36
36
  return listedFunctions;
37
37
  });
38
38
  };
39
39
  exports.listFunctions = listFunctions;
40
+ // List one Netlify Functions main entry file for a specific directory
41
+ const listFunction = function (mainFile, { featureFlags: inputFeatureFlags, config } = {}) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ const featureFlags = (0, feature_flags_1.getFlags)(inputFeatureFlags);
44
+ const func = yield (0, runtimes_1.getFunctionFromPath)(mainFile, { featureFlags, config });
45
+ if (!func) {
46
+ return;
47
+ }
48
+ return getListedFunction(func);
49
+ });
50
+ };
51
+ exports.listFunction = listFunction;
40
52
  // List all Netlify Functions files for a specific directory
41
53
  const listFunctionsFiles = function (relativeSrcFolders, { basePath, config, featureFlags: inputFeatureFlags } = {}) {
42
54
  return __awaiter(this, void 0, void 0, function* () {
@@ -49,14 +61,14 @@ const listFunctionsFiles = function (relativeSrcFolders, { basePath, config, fea
49
61
  });
50
62
  };
51
63
  exports.listFunctionsFiles = listFunctionsFiles;
52
- const getListedFunction = function ({ runtime, name, mainFile, extension }) {
53
- return { name, mainFile, runtime: runtime.name, extension };
64
+ const getListedFunction = function ({ runtime, name, mainFile, extension, config, inSourceConfig, }) {
65
+ var _a;
66
+ return { name, mainFile, runtime: runtime.name, extension, schedule: (_a = inSourceConfig === null || inSourceConfig === void 0 ? void 0 : inSourceConfig.schedule) !== null && _a !== void 0 ? _a : config.schedule };
54
67
  };
55
68
  const getListedFunctionFiles = function (func, options) {
56
69
  return __awaiter(this, void 0, void 0, function* () {
57
70
  const srcFiles = yield getSrcFiles(Object.assign(Object.assign({}, func), options));
58
- const { name, mainFile, runtime } = func;
59
- return srcFiles.map((srcFile) => ({ srcFile, name, mainFile, runtime: runtime.name, extension: (0, path_1.extname)(srcFile) }));
71
+ return srcFiles.map((srcFile) => (Object.assign(Object.assign({}, getListedFunction(func)), { srcFile, extension: (0, path_1.extname)(srcFile) })));
60
72
  });
61
73
  };
62
74
  const getSrcFiles = function (_a) {
@@ -35,22 +35,25 @@ const detectGoFunction = ({ fsCache, path }) => __awaiter(void 0, void 0, void 0
35
35
  });
36
36
  const findFunctionsInPaths = function ({ featureFlags, fsCache, paths }) {
37
37
  return __awaiter(this, void 0, void 0, function* () {
38
- const functions = yield Promise.all(paths.map((path) => __awaiter(this, void 0, void 0, function* () {
39
- const runtime = yield (0, detect_runtime_1.detectBinaryRuntime)({ fsCache, path });
40
- if (runtime === 'go') {
41
- return processBinary({ fsCache, path });
42
- }
43
- if (featureFlags.buildGoSource !== true) {
44
- return;
45
- }
46
- const goSourceFile = yield detectGoFunction({ fsCache, path });
47
- if (goSourceFile) {
48
- return processSource({ fsCache, mainFile: goSourceFile, path });
49
- }
50
- })));
38
+ const functions = yield Promise.all(paths.map((path) => getFunctionAtPath(path, { featureFlags, fsCache })));
51
39
  return functions.filter(non_nullable_1.nonNullable);
52
40
  });
53
41
  };
42
+ const getFunctionAtPath = function (path, { featureFlags, fsCache }) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const runtime = yield (0, detect_runtime_1.detectBinaryRuntime)({ fsCache, path });
45
+ if (runtime === 'go') {
46
+ return processBinary({ fsCache, path });
47
+ }
48
+ if (featureFlags.buildGoSource !== true) {
49
+ return;
50
+ }
51
+ const goSourceFile = yield detectGoFunction({ fsCache, path });
52
+ if (goSourceFile) {
53
+ return processSource({ fsCache, mainFile: goSourceFile, path });
54
+ }
55
+ });
56
+ };
54
57
  const processBinary = ({ fsCache, path }) => __awaiter(void 0, void 0, void 0, function* () {
55
58
  const stat = (yield (0, fs_1.cachedLstat)(fsCache, path));
56
59
  const extension = (0, path_1.extname)(path);
@@ -96,6 +99,6 @@ const zipFunction = function ({ config, destFolder, filename, mainFile, srcDir,
96
99
  return { config, path: destPath };
97
100
  });
98
101
  };
99
- const runtime = { findFunctionsInPaths, name: 'go', zipFunction };
102
+ const runtime = { findFunctionsInPaths, getFunctionAtPath, name: 'go', zipFunction };
100
103
  exports.default = runtime;
101
104
  //# sourceMappingURL=index.js.map
@@ -10,4 +10,11 @@ declare const getFunctionsFromPaths: (paths: string[], { config, dedupe, feature
10
10
  dedupe?: boolean | undefined;
11
11
  featureFlags?: FeatureFlags | undefined;
12
12
  }) => Promise<FunctionMap>;
13
- export { getFunctionsFromPaths };
13
+ /**
14
+ * Gets a list of functions found in a list of paths.
15
+ */
16
+ declare const getFunctionFromPath: (path: string, { config, featureFlags }?: {
17
+ config?: Config | undefined;
18
+ featureFlags?: FeatureFlags | undefined;
19
+ }) => Promise<FunctionSource | undefined>;
20
+ export { getFunctionsFromPaths, getFunctionFromPath };
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.getFunctionsFromPaths = void 0;
15
+ exports.getFunctionFromPath = exports.getFunctionsFromPaths = void 0;
16
16
  const path_1 = require("path");
17
17
  const config_1 = require("../config");
18
18
  const feature_flags_1 = require("../feature_flags");
@@ -42,23 +42,24 @@ const findFunctionsInRuntime = function ({ dedupe = false, featureFlags, fsCache
42
42
  return { functions: augmentedFunctions, remainingPaths };
43
43
  });
44
44
  };
45
+ // An object to cache filesystem operations. This allows different functions
46
+ // to perform IO operations on the same file (i.e. getting its stats or its
47
+ // contents) without duplicating work.
48
+ const makeFsCache = () => ({});
49
+ // The order of this array determines the priority of the runtimes. If a path
50
+ // is used by the first time, it won't be made available to the subsequent
51
+ // runtimes.
52
+ const RUNTIMES = [node_1.default, go_1.default, rust_1.default];
45
53
  /**
46
54
  * Gets a list of functions found in a list of paths.
47
55
  */
48
56
  const getFunctionsFromPaths = (paths, { config, dedupe = false, featureFlags = feature_flags_1.defaultFlags, } = {}) => __awaiter(void 0, void 0, void 0, function* () {
49
- // An object to cache filesystem operations. This allows different functions
50
- // to perform IO operations on the same file (i.e. getting its stats or its
51
- // contents) without duplicating work.
52
- const fsCache = {};
53
- // The order of this array determines the priority of the runtimes. If a path
54
- // is used by the first time, it won't be made available to the subsequent
55
- // runtimes.
56
- const runtimes = [node_1.default, go_1.default, rust_1.default];
57
+ const fsCache = makeFsCache();
57
58
  // We cycle through the ordered array of runtimes, passing each one of them
58
59
  // through `findFunctionsInRuntime`. For each iteration, we collect all the
59
60
  // functions found plus the list of paths that still need to be evaluated,
60
61
  // using them as the input for the next iteration until the last runtime.
61
- const { functions } = yield runtimes.reduce((aggregate, runtime) => __awaiter(void 0, void 0, void 0, function* () {
62
+ const { functions } = yield RUNTIMES.reduce((aggregate, runtime) => __awaiter(void 0, void 0, void 0, function* () {
62
63
  const { functions: aggregateFunctions, remainingPaths: aggregatePaths } = yield aggregate;
63
64
  const { functions: runtimeFunctions, remainingPaths: runtimePaths } = yield findFunctionsInRuntime({
64
65
  dedupe,
@@ -79,4 +80,19 @@ const getFunctionsFromPaths = (paths, { config, dedupe = false, featureFlags = f
79
80
  return new Map(functionsWithConfig);
80
81
  });
81
82
  exports.getFunctionsFromPaths = getFunctionsFromPaths;
83
+ /**
84
+ * Gets a list of functions found in a list of paths.
85
+ */
86
+ const getFunctionFromPath = (path, { config, featureFlags = feature_flags_1.defaultFlags } = {}) => __awaiter(void 0, void 0, void 0, function* () {
87
+ const fsCache = makeFsCache();
88
+ for (const runtime of RUNTIMES) {
89
+ // eslint-disable-next-line no-await-in-loop
90
+ const func = yield runtime.getFunctionAtPath(path, { fsCache, featureFlags });
91
+ if (func) {
92
+ return Object.assign(Object.assign({}, func), { runtime, config: (0, config_1.getConfigForFunction)({ config, func: Object.assign(Object.assign({}, func), { runtime }) }) });
93
+ }
94
+ }
95
+ return undefined;
96
+ });
97
+ exports.getFunctionFromPath = getFunctionFromPath;
82
98
  //# sourceMappingURL=index.js.map
@@ -24,7 +24,7 @@ const es_modules_1 = require("./es_modules");
24
24
  // Paths that will be excluded from the tracing process.
25
25
  const ignore = ['node_modules/aws-sdk/**'];
26
26
  const appearsToBeModuleName = (name) => !name.startsWith('.');
27
- const bundle = ({ basePath, config, featureFlags, mainFile, pluginsModulesPath, repositoryRoot = basePath, }) => __awaiter(void 0, void 0, void 0, function* () {
27
+ const bundle = ({ basePath, config, mainFile, pluginsModulesPath, repositoryRoot = basePath, }) => __awaiter(void 0, void 0, void 0, function* () {
28
28
  const { includedFiles = [], includedFilesBasePath } = config;
29
29
  const { exclude: excludedPaths, paths: includedFilePaths } = yield (0, included_files_1.getPathsOfIncludedFiles)(includedFiles, includedFilesBasePath || basePath);
30
30
  const { paths: dependencyPaths, rewrites } = yield traceFilesAndTranspile({
@@ -32,7 +32,6 @@ const bundle = ({ basePath, config, featureFlags, mainFile, pluginsModulesPath,
32
32
  config,
33
33
  mainFile,
34
34
  pluginsModulesPath,
35
- transpile: featureFlags.nftTranspile,
36
35
  });
37
36
  const filteredIncludedPaths = (0, included_files_1.filterExcludedPaths)([...dependencyPaths, ...includedFilePaths], excludedPaths);
38
37
  const dirnames = filteredIncludedPaths.map((filePath) => (0, path_1.normalize)((0, path_1.dirname)(filePath))).sort();
@@ -51,7 +50,7 @@ const ignoreFunction = (path) => {
51
50
  const shouldIgnore = ignore.some((expression) => (0, minimatch_1.default)(normalizedPath, expression));
52
51
  return shouldIgnore;
53
52
  };
54
- const traceFilesAndTranspile = function ({ basePath, config, mainFile, pluginsModulesPath, transpile, }) {
53
+ const traceFilesAndTranspile = function ({ basePath, config, mainFile, pluginsModulesPath, }) {
55
54
  return __awaiter(this, void 0, void 0, function* () {
56
55
  const fsCache = {};
57
56
  const { fileList: dependencyPaths, esmFileList, reasons, } = yield (0, nft_1.nodeFileTrace)([mainFile], {
@@ -86,9 +85,7 @@ const traceFilesAndTranspile = function ({ basePath, config, mainFile, pluginsMo
86
85
  }),
87
86
  });
88
87
  const normalizedDependencyPaths = [...dependencyPaths].map((path) => basePath ? (0, path_1.resolve)(basePath, path) : (0, path_1.resolve)(path));
89
- const rewrites = transpile
90
- ? yield (0, es_modules_1.transpileESM)({ basePath, config, esmPaths: esmFileList, fsCache, reasons })
91
- : undefined;
88
+ const rewrites = yield (0, es_modules_1.transpileESM)({ basePath, config, esmPaths: esmFileList, fsCache, reasons });
92
89
  return {
93
90
  paths: normalizedDependencyPaths,
94
91
  rewrites,
@@ -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>;
1
+ import { FindFunctionsInPathsFunction, GetFunctionAtPathFunction } from '../runtime';
2
+ declare const findFunctionsInPaths: FindFunctionsInPathsFunction;
3
+ declare const getFunctionAtPath: GetFunctionAtPathFunction;
6
4
  export { findFunctionsInPaths, getFunctionAtPath };
@@ -18,6 +18,7 @@ const path_1 = require("path");
18
18
  const util_1 = require("util");
19
19
  const locate_path_1 = __importDefault(require("locate-path"));
20
20
  const non_nullable_1 = require("../../utils/non_nullable");
21
+ const in_source_config_1 = require("./in_source_config");
21
22
  const pLstat = (0, util_1.promisify)(fs_1.lstat);
22
23
  // List of extensions that this runtime will look for, in order of precedence.
23
24
  const allowedExtensions = ['.js', '.zip', '.cjs', '.mjs', '.ts'];
@@ -30,9 +31,9 @@ const sortByExtension = (fA, fB) => {
30
31
  const indexB = allowedExtensions.indexOf(fB.extension);
31
32
  return indexB - indexA;
32
33
  };
33
- const findFunctionsInPaths = function ({ paths }) {
34
+ const findFunctionsInPaths = function ({ paths, fsCache, featureFlags }) {
34
35
  return __awaiter(this, void 0, void 0, function* () {
35
- const functions = yield Promise.all(paths.map(getFunctionAtPath));
36
+ const functions = yield Promise.all(paths.map((path) => getFunctionAtPath(path, { fsCache, featureFlags })));
36
37
  // It's fine to mutate the array since its scope is local to this function.
37
38
  const sortedFunctions = functions.filter(non_nullable_1.nonNullable).sort((fA, fB) => {
38
39
  // We first sort the functions array to put directories first. This is so
@@ -63,10 +64,11 @@ const getFunctionAtPath = function (srcPath) {
63
64
  if (mainFile === undefined) {
64
65
  return;
65
66
  }
67
+ const iscValues = yield (0, in_source_config_1.findISCDeclarationsInPath)(mainFile);
66
68
  const extension = (0, path_1.extname)(srcPath);
67
69
  const srcDir = stat.isDirectory() ? srcPath : (0, path_1.dirname)(srcPath);
68
70
  const name = (0, path_1.basename)(srcPath, (0, path_1.extname)(srcPath));
69
- return { extension, filename, mainFile, name, srcDir, srcPath, stat };
71
+ return { extension, filename, mainFile, name, srcDir, srcPath, stat, inSourceConfig: iscValues };
70
72
  });
71
73
  };
72
74
  exports.getFunctionAtPath = getFunctionAtPath;
@@ -0,0 +1,12 @@
1
+ import { ArgumentPlaceholder, Expression, SpreadElement, JSXNamespacedName } from '@babel/types';
2
+ import { parse as parseSchedule } from './properties/schedule';
3
+ declare const IN_SOURCE_CONFIG_MODULE = "@netlify/functions";
4
+ declare type ISCValues = Partial<ReturnType<typeof parseSchedule>>;
5
+ declare const findISCDeclarationsInPath: (sourcePath: string) => Promise<ISCValues>;
6
+ declare type ISCHandlerArg = ArgumentPlaceholder | Expression | SpreadElement | JSXNamespacedName;
7
+ interface ISCExport {
8
+ local: string;
9
+ args: ISCHandlerArg[];
10
+ }
11
+ export { findISCDeclarationsInPath, IN_SOURCE_CONFIG_MODULE };
12
+ export type { ISCExport, ISCHandlerArg, ISCValues };
@@ -0,0 +1,49 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.IN_SOURCE_CONFIG_MODULE = exports.findISCDeclarationsInPath = void 0;
13
+ const non_nullable_1 = require("../../../utils/non_nullable");
14
+ const parser_1 = require("../parser");
15
+ const exports_1 = require("../parser/exports");
16
+ const imports_1 = require("../parser/imports");
17
+ const schedule_1 = require("./properties/schedule");
18
+ const IN_SOURCE_CONFIG_MODULE = '@netlify/functions';
19
+ exports.IN_SOURCE_CONFIG_MODULE = IN_SOURCE_CONFIG_MODULE;
20
+ // Parses a JS/TS file and looks for in-source config declarations. It returns
21
+ // an array of all declarations found, with `property` indicating the name of
22
+ // 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);
25
+ if (ast === null) {
26
+ return {};
27
+ }
28
+ const imports = ast.body.map((node) => (0, imports_1.getImports)(node, IN_SOURCE_CONFIG_MODULE)).flat();
29
+ const exports = (0, exports_1.getMainExport)(ast.body);
30
+ const iscExports = exports
31
+ .map(({ args, local: exportName }) => {
32
+ const matchingImport = imports.find(({ local: importName }) => importName === exportName);
33
+ if (matchingImport === undefined) {
34
+ return null;
35
+ }
36
+ switch (matchingImport.imported) {
37
+ case 'schedule':
38
+ return (0, schedule_1.parse)({ args });
39
+ default:
40
+ // no-op
41
+ }
42
+ return null;
43
+ })
44
+ .filter(non_nullable_1.nonNullable);
45
+ const mergedExports = iscExports.reduce((acc, obj) => (Object.assign(Object.assign({}, acc), obj)), {});
46
+ return mergedExports;
47
+ });
48
+ exports.findISCDeclarationsInPath = findISCDeclarationsInPath;
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ import type { ISCHandlerArg } from '..';
2
+ export declare const parse: ({ args }: {
3
+ args: ISCHandlerArg[];
4
+ }) => {
5
+ schedule: string | undefined;
6
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parse = void 0;
4
+ const parse = ({ args }) => {
5
+ const [expression] = args;
6
+ const schedule = expression.type === 'StringLiteral' ? expression.value : undefined;
7
+ return {
8
+ schedule,
9
+ };
10
+ };
11
+ exports.parse = parse;
12
+ //# sourceMappingURL=schedule.js.map
@@ -27,6 +27,7 @@ const path_1 = require("path");
27
27
  const cp_file_1 = __importDefault(require("cp-file"));
28
28
  const bundlers_1 = require("./bundlers");
29
29
  const finder_1 = require("./finder");
30
+ const in_source_config_1 = require("./in_source_config");
30
31
  const detect_es_module_1 = require("./utils/detect_es_module");
31
32
  const plugin_modules_path_1 = require("./utils/plugin_modules_path");
32
33
  const zip_1 = require("./utils/zip");
@@ -88,6 +89,7 @@ const zipFunction = function ({ archiveFormat, basePath, config = {}, destFolder
88
89
  srcPath,
89
90
  stat,
90
91
  });
92
+ const inSourceConfig = yield (0, in_source_config_1.findISCDeclarationsInPath)(mainFile);
91
93
  (0, plugin_modules_path_1.createAliases)(srcFiles, pluginsModulesPath, aliases, finalBasePath);
92
94
  const zipPath = yield (0, zip_1.zipNodeJs)({
93
95
  aliases,
@@ -106,6 +108,7 @@ const zipFunction = function ({ archiveFormat, basePath, config = {}, destFolder
106
108
  bundlerWarnings,
107
109
  config,
108
110
  inputs,
111
+ inSourceConfig,
109
112
  nativeNodeModules,
110
113
  nodeModulesWithDynamicImports,
111
114
  path: zipPath,
@@ -135,6 +138,7 @@ const zipWithFunctionWithFallback = (_a) => __awaiter(void 0, void 0, void 0, fu
135
138
  });
136
139
  const runtime = {
137
140
  findFunctionsInPaths: finder_1.findFunctionsInPaths,
141
+ getFunctionAtPath: finder_1.getFunctionAtPath,
138
142
  getSrcFiles: getSrcFilesWithBundler,
139
143
  name: 'js',
140
144
  zipFunction: zipWithFunctionWithFallback,
@@ -0,0 +1,4 @@
1
+ import { Statement } from '@babel/types';
2
+ import type { ISCExport } from '../in_source_config';
3
+ declare const getMainExport: (nodes: Statement[]) => ISCExport[];
4
+ export { getMainExport };
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMainExport = void 0;
4
+ const helpers_1 = require("./helpers");
5
+ // Finds the main handler export in an AST.
6
+ const getMainExport = (nodes) => {
7
+ let handlerExport = [];
8
+ nodes.find((node) => {
9
+ const esmExports = getMainExportFromESM(node);
10
+ if (esmExports.length !== 0) {
11
+ handlerExport = esmExports;
12
+ return true;
13
+ }
14
+ const cjsExports = getMainExportFromCJS(node);
15
+ if (cjsExports.length !== 0) {
16
+ handlerExport = cjsExports;
17
+ return true;
18
+ }
19
+ return false;
20
+ });
21
+ return handlerExport;
22
+ };
23
+ exports.getMainExport = getMainExport;
24
+ // Finds the main handler export in a CJS AST.
25
+ const getMainExportFromCJS = (node) => {
26
+ const handlerPaths = [
27
+ ['module', 'exports', 'handler'],
28
+ ['exports', 'handler'],
29
+ ];
30
+ return handlerPaths.flatMap((handlerPath) => {
31
+ if (!(0, helpers_1.isModuleExports)(node, handlerPath) || node.expression.right.type !== 'CallExpression') {
32
+ return [];
33
+ }
34
+ return getExportsFromCallExpression(node.expression.right);
35
+ });
36
+ };
37
+ // Finds the main handler export in an ESM AST.
38
+ // eslint-disable-next-line complexity
39
+ const getMainExportFromESM = (node) => {
40
+ var _a;
41
+ if (node.type !== 'ExportNamedDeclaration' || node.exportKind !== 'value') {
42
+ return [];
43
+ }
44
+ const { declaration } = node;
45
+ if (!declaration || declaration.type !== 'VariableDeclaration') {
46
+ return [];
47
+ }
48
+ const handlerDeclaration = declaration.declarations.find((childDeclaration) => {
49
+ const { id, type } = childDeclaration;
50
+ return type === 'VariableDeclarator' && id.type === 'Identifier' && id.name === 'handler';
51
+ });
52
+ if (((_a = handlerDeclaration === null || handlerDeclaration === void 0 ? void 0 : handlerDeclaration.init) === null || _a === void 0 ? void 0 : _a.type) !== 'CallExpression') {
53
+ return [];
54
+ }
55
+ const exports = getExportsFromCallExpression(handlerDeclaration.init);
56
+ return exports;
57
+ };
58
+ const getExportsFromCallExpression = (node) => {
59
+ const { arguments: args, callee } = node;
60
+ if (callee.type !== 'Identifier') {
61
+ return [];
62
+ }
63
+ const exports = [{ local: callee.name, args }];
64
+ return exports;
65
+ };
66
+ //# sourceMappingURL=exports.js.map
@@ -0,0 +1,7 @@
1
+ import { AssignmentExpression, Expression, ExpressionStatement, ImportDeclaration, Statement } from '@babel/types';
2
+ declare const isImport: (node: Statement, importPath: string) => node is ImportDeclaration;
3
+ declare const isModuleExports: (node: Statement, dotExpression?: string[]) => node is ExpressionStatement & {
4
+ expression: AssignmentExpression;
5
+ };
6
+ declare const isRequire: (node: Expression | undefined | null, requirePath: string) => boolean;
7
+ export { isImport, isModuleExports, isRequire };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isRequire = exports.isModuleExports = exports.isImport = void 0;
4
+ // eslint-disable-next-line complexity
5
+ const isDotExpression = (node, expression) => {
6
+ if (node.type !== 'MemberExpression') {
7
+ return false;
8
+ }
9
+ const object = expression.slice(0, -1);
10
+ const [property] = expression.slice(-1);
11
+ if (node.property.type !== 'Identifier' || node.property.name !== property) {
12
+ return false;
13
+ }
14
+ if (object.length > 1) {
15
+ return isDotExpression(node.object, object);
16
+ }
17
+ return node.object.type === 'Identifier' && object[0] === node.object.name && property === node.property.name;
18
+ };
19
+ const isImport = (node, importPath) => node.type === 'ImportDeclaration' && node.source.value === importPath;
20
+ exports.isImport = isImport;
21
+ const isModuleExports = (node, dotExpression = ['module', 'exports']) => node.type === 'ExpressionStatement' &&
22
+ node.expression.type === 'AssignmentExpression' &&
23
+ node.expression.left.type === 'MemberExpression' &&
24
+ isDotExpression(node.expression.left, dotExpression);
25
+ exports.isModuleExports = isModuleExports;
26
+ const isRequire = (node, requirePath) => {
27
+ var _a;
28
+ if (!node || node.type !== 'CallExpression') {
29
+ return false;
30
+ }
31
+ const { arguments: args, callee } = node;
32
+ const isRequiredModule = ((_a = args[0]) === null || _a === void 0 ? void 0 : _a.type) === 'StringLiteral' && isRequirePath(args[0], requirePath);
33
+ return isRequireCall(callee) && isRequiredModule;
34
+ };
35
+ exports.isRequire = isRequire;
36
+ const isRequireCall = (node) => node.type === 'Identifier' && node.name === 'require';
37
+ const isRequirePath = (node, path) => node.type === 'StringLiteral' && node.value === path;
38
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1,6 @@
1
+ import { Statement } from '@babel/types';
2
+ declare const getImports: (node: Statement, importPath: string) => {
3
+ imported: string;
4
+ local: string;
5
+ }[];
6
+ export { getImports };
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getImports = void 0;
4
+ const non_nullable_1 = require("../../../utils/non_nullable");
5
+ const helpers_1 = require("./helpers");
6
+ // Finds import/require statements of a given path in an AST.
7
+ const getImports = (node, importPath) => {
8
+ const esmImports = getImportsFromESM(node, importPath);
9
+ if (esmImports.length !== 0) {
10
+ return esmImports;
11
+ }
12
+ const cjsImports = getImportsFromCJS(node, importPath);
13
+ return cjsImports;
14
+ };
15
+ exports.getImports = getImports;
16
+ // Finds import/require statements of a given path in a CJS AST.
17
+ const getImportsFromCJS = (node, importPath) => {
18
+ if (node.type !== 'VariableDeclaration') {
19
+ return [];
20
+ }
21
+ const { declarations } = node;
22
+ const requireDeclaration = declarations.find((declaration) => declaration.type === 'VariableDeclarator' && (0, helpers_1.isRequire)(declaration.init, importPath));
23
+ if (requireDeclaration === undefined || requireDeclaration.id.type !== 'ObjectPattern') {
24
+ return [];
25
+ }
26
+ const imports = requireDeclaration.id.properties
27
+ .map((property) => {
28
+ if (property.type !== 'ObjectProperty') {
29
+ return;
30
+ }
31
+ const { key, value } = property;
32
+ if (key.type !== 'Identifier' || value.type !== 'Identifier') {
33
+ return;
34
+ }
35
+ return { imported: key.name, local: value.name };
36
+ })
37
+ .filter(non_nullable_1.nonNullable);
38
+ return imports;
39
+ };
40
+ // Finds import/require statements of a given path in an ESM AST.
41
+ const getImportsFromESM = (node, importPath) => {
42
+ if (!(0, helpers_1.isImport)(node, importPath)) {
43
+ return [];
44
+ }
45
+ const { specifiers } = node;
46
+ const imports = specifiers
47
+ .map((specifier) => {
48
+ if (specifier.type !== 'ImportSpecifier') {
49
+ return;
50
+ }
51
+ const { imported, local } = specifier;
52
+ if (imported.type !== 'Identifier' || local.type !== 'Identifier') {
53
+ return;
54
+ }
55
+ return { imported: imported.name, local: local.name };
56
+ })
57
+ .filter(non_nullable_1.nonNullable);
58
+ return imports;
59
+ };
60
+ //# sourceMappingURL=imports.js.map
@@ -6,4 +6,5 @@ declare const parseExpression: ({ basePath, expression: rawExpression, resolveDi
6
6
  includedPathsGlob: string | null | undefined;
7
7
  type: string;
8
8
  } | undefined;
9
- export { parseExpression };
9
+ declare const safelyParseFile: (path: string) => Promise<import("@babel/types").Program | null>;
10
+ export { parseExpression, safelyParseFile };
@@ -1,8 +1,19 @@
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseExpression = void 0;
12
+ exports.safelyParseFile = exports.parseExpression = void 0;
13
+ /* eslint-disable max-lines */
4
14
  const path_1 = require("path");
5
15
  const parser_1 = require("@babel/parser");
16
+ const fs_1 = require("../../../utils/fs");
6
17
  const non_nullable_1 = require("../../../utils/non_nullable");
7
18
  const GLOB_WILDCARD = '**';
8
19
  // Transforms an array of glob nodes into a glob string including an absolute
@@ -75,6 +86,29 @@ const parseExpression = ({ basePath, expression: rawExpression, resolveDir, }) =
75
86
  }
76
87
  };
77
88
  exports.parseExpression = parseExpression;
89
+ // Parses a JS/TS file and returns the resulting AST.
90
+ const parseFile = (path) => __awaiter(void 0, void 0, void 0, function* () {
91
+ const code = yield (0, fs_1.readFile)(path, 'utf8');
92
+ const ast = (0, parser_1.parse)(code, {
93
+ plugins: ['typescript'],
94
+ sourceType: 'module',
95
+ });
96
+ return ast.program;
97
+ });
98
+ // Attemps to parse a JS/TS file at the given path, returning its AST if
99
+ // successful, or `null` if not.
100
+ const safelyParseFile = (path) => __awaiter(void 0, void 0, void 0, function* () {
101
+ if (!path) {
102
+ return null;
103
+ }
104
+ try {
105
+ return yield parseFile(path);
106
+ }
107
+ catch (error) {
108
+ return null;
109
+ }
110
+ });
111
+ exports.safelyParseFile = safelyParseFile;
78
112
  // Parses a `require()` and returns a glob string with an absolute path.
79
113
  const parseRequire = ({ basePath, expression, resolveDir, }) => {
80
114
  const { arguments: args = [] } = expression;
@@ -146,4 +180,5 @@ const validateGlobNodes = (globNodes) => {
146
180
  const hasStaticHead = globNodes[0] !== GLOB_WILDCARD;
147
181
  return hasStrings && hasStaticHead;
148
182
  };
183
+ /* eslint-enable max-lines */
149
184
  //# sourceMappingURL=index.js.map
@@ -4,12 +4,17 @@ import { FeatureFlags } from '../feature_flags';
4
4
  import { FunctionSource, SourceFile } from '../function';
5
5
  import { FsCache } from '../utils/fs';
6
6
  import type { NodeBundlerName } from './node';
7
+ import type { ISCValues } from './node/in_source_config';
7
8
  declare type RuntimeName = 'go' | 'js' | 'rs';
8
9
  declare type FindFunctionsInPathsFunction = (args: {
9
10
  featureFlags: FeatureFlags;
10
11
  fsCache: FsCache;
11
12
  paths: string[];
12
13
  }) => Promise<SourceFile[]>;
14
+ declare type GetFunctionAtPathFunction = (path: string, args: {
15
+ featureFlags: FeatureFlags;
16
+ fsCache: FsCache;
17
+ }) => Promise<SourceFile | undefined>;
13
18
  declare type GetSrcFilesFunction = (args: {
14
19
  basePath?: string;
15
20
  config: FunctionConfig;
@@ -22,6 +27,7 @@ interface ZipFunctionResult {
22
27
  bundlerWarnings?: object[];
23
28
  config: FunctionConfig;
24
29
  inputs?: string[];
30
+ inSourceConfig?: ISCValues;
25
31
  nativeNodeModules?: object;
26
32
  nodeModulesWithDynamicImports?: string[];
27
33
  path: string;
@@ -36,8 +42,9 @@ declare type ZipFunction = (args: {
36
42
  } & FunctionSource) => Promise<ZipFunctionResult>;
37
43
  interface Runtime {
38
44
  findFunctionsInPaths: FindFunctionsInPathsFunction;
45
+ getFunctionAtPath: GetFunctionAtPathFunction;
39
46
  getSrcFiles?: GetSrcFilesFunction;
40
47
  name: RuntimeName;
41
48
  zipFunction: ZipFunction;
42
49
  }
43
- export { FindFunctionsInPathsFunction, GetSrcFilesFunction, Runtime, RuntimeName, ZipFunction, ZipFunctionResult };
50
+ export { FindFunctionsInPathsFunction, GetSrcFilesFunction, Runtime, RuntimeName, ZipFunction, ZipFunctionResult, GetFunctionAtPathFunction, };
@@ -41,22 +41,28 @@ const detectRustFunction = ({ fsCache, path }) => __awaiter(void 0, void 0, void
41
41
  });
42
42
  const findFunctionsInPaths = function ({ featureFlags, fsCache, paths, }) {
43
43
  return __awaiter(this, void 0, void 0, function* () {
44
- const functions = yield Promise.all(paths.map((path) => __awaiter(this, void 0, void 0, function* () {
45
- const runtime = yield (0, detect_runtime_1.detectBinaryRuntime)({ fsCache, path });
46
- if (runtime === 'rs') {
47
- return processBinary({ fsCache, path });
48
- }
49
- if (featureFlags.buildRustSource !== true) {
50
- return;
51
- }
52
- const rustSourceFile = yield detectRustFunction({ fsCache, path });
53
- if (rustSourceFile) {
54
- return processSource({ fsCache, mainFile: rustSourceFile, path });
55
- }
44
+ const functions = yield Promise.all(paths.map((path) => getFunctionAtPath(path, {
45
+ featureFlags,
46
+ fsCache,
56
47
  })));
57
48
  return functions.filter(non_nullable_1.nonNullable);
58
49
  });
59
50
  };
51
+ const getFunctionAtPath = function (path, { featureFlags, fsCache, }) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const runtime = yield (0, detect_runtime_1.detectBinaryRuntime)({ fsCache, path });
54
+ if (runtime === 'rs') {
55
+ return processBinary({ fsCache, path });
56
+ }
57
+ if (featureFlags.buildRustSource !== true) {
58
+ return;
59
+ }
60
+ const rustSourceFile = yield detectRustFunction({ fsCache, path });
61
+ if (rustSourceFile) {
62
+ return processSource({ fsCache, mainFile: rustSourceFile, path });
63
+ }
64
+ });
65
+ };
60
66
  const processBinary = ({ fsCache, path }) => __awaiter(void 0, void 0, void 0, function* () {
61
67
  const stat = (yield (0, fs_1.cachedLstat)(fsCache, path));
62
68
  const filename = (0, path_1.basename)(path);
@@ -116,6 +122,6 @@ const zipFunction = function ({ config, destFolder, filename, mainFile, runtime,
116
122
  return { config, path: destPath };
117
123
  });
118
124
  };
119
- const runtime = { findFunctionsInPaths, name: 'rs', zipFunction };
125
+ const runtime = { findFunctionsInPaths, getFunctionAtPath, name: 'rs', zipFunction };
120
126
  exports.default = runtime;
121
127
  //# sourceMappingURL=index.js.map
@@ -4,8 +4,8 @@ exports.formatZipResult = void 0;
4
4
  const remove_undefined_1 = require("./remove_undefined");
5
5
  // Takes the result of zipping a function and formats it for output.
6
6
  const formatZipResult = (archive) => {
7
- var _a;
8
- const functionResult = Object.assign(Object.assign({}, archive), { runtime: archive.runtime.name, schedule: (_a = archive === null || archive === void 0 ? void 0 : archive.config) === null || _a === void 0 ? void 0 : _a.schedule });
7
+ var _a, _b, _c;
8
+ const functionResult = Object.assign(Object.assign({}, archive), { inSourceConfig: undefined, runtime: archive.runtime.name, schedule: (_b = (_a = archive.inSourceConfig) === null || _a === void 0 ? void 0 : _a.schedule) !== null && _b !== void 0 ? _b : (_c = archive === null || archive === void 0 ? void 0 : archive.config) === null || _c === void 0 ? void 0 : _c.schedule });
9
9
  return (0, remove_undefined_1.removeUndefined)(functionResult);
10
10
  };
11
11
  exports.formatZipResult = formatZipResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/zip-it-and-ship-it",
3
- "version": "5.0.0",
3
+ "version": "5.4.0-isc-listfunctions",
4
4
  "description": "Zip it and ship it",
5
5
  "main": "./dist/main.js",
6
6
  "bin": {
@@ -93,7 +93,7 @@
93
93
  },
94
94
  "devDependencies": {
95
95
  "@babel/types": "^7.15.6",
96
- "@netlify/eslint-config-node": "^3.3.8",
96
+ "@netlify/eslint-config-node": "^4.0.1",
97
97
  "@types/archiver": "^5.1.1",
98
98
  "@types/end-of-stream": "^1.4.1",
99
99
  "@types/resolve": "^1.20.1",