@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
  };
@@ -15,43 +6,39 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
6
  exports.build = void 0;
16
7
  const fs_1 = require("fs");
17
8
  const path_1 = require("path");
18
- const util_1 = require("util");
19
- const make_dir_1 = __importDefault(require("make-dir"));
20
9
  const tmp_promise_1 = __importDefault(require("tmp-promise"));
21
10
  const toml_1 = __importDefault(require("toml"));
22
- const fs_2 = require("../../utils/fs");
23
11
  const shell_1 = require("../../utils/shell");
24
12
  const constants_1 = require("./constants");
25
- const pReadFile = (0, util_1.promisify)(fs_1.readFile);
26
13
  const runtimeName = 'rs';
27
- const build = ({ config, name, srcDir }) => __awaiter(void 0, void 0, void 0, function* () {
14
+ const build = async ({ config, name, srcDir }) => {
28
15
  const functionName = (0, path_1.basename)(srcDir);
29
16
  try {
30
- yield installToolchainOnce();
17
+ await installToolchainOnce();
31
18
  }
32
19
  catch (error) {
33
20
  error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: runtimeName } };
34
21
  throw error;
35
22
  }
36
- const targetDirectory = yield getTargetDirectory({ config, name });
37
- yield cargoBuild({ functionName, srcDir, targetDirectory });
23
+ const targetDirectory = await getTargetDirectory({ config, name });
24
+ await cargoBuild({ functionName, srcDir, targetDirectory });
38
25
  // By default, the binary will have the same name as the crate and there's no
39
26
  // way to override it (https://github.com/rust-lang/cargo/issues/1706). We
40
27
  // must extract the crate name from the manifest and use it to form the path
41
28
  // to the binary.
42
- const manifest = yield pReadFile((0, path_1.join)(srcDir, constants_1.MANIFEST_NAME), 'utf8');
29
+ const manifest = await fs_1.promises.readFile((0, path_1.join)(srcDir, constants_1.MANIFEST_NAME), 'utf8');
43
30
  const { package: { name: packageName }, } = toml_1.default.parse(manifest);
44
31
  const binaryPath = (0, path_1.join)(targetDirectory, constants_1.BUILD_TARGET, 'release', packageName);
45
- const stat = yield (0, fs_2.lstat)(binaryPath);
32
+ const stat = await fs_1.promises.lstat(binaryPath);
46
33
  return {
47
34
  path: binaryPath,
48
35
  stat,
49
36
  };
50
- });
37
+ };
51
38
  exports.build = build;
52
- const cargoBuild = ({ functionName, srcDir, targetDirectory, }) => __awaiter(void 0, void 0, void 0, function* () {
39
+ const cargoBuild = async ({ functionName, srcDir, targetDirectory, }) => {
53
40
  try {
54
- yield (0, shell_1.runCommand)('cargo', ['build', '--target', constants_1.BUILD_TARGET, '--release'], {
41
+ await (0, shell_1.runCommand)('cargo', ['build', '--target', constants_1.BUILD_TARGET, '--release'], {
55
42
  cwd: srcDir,
56
43
  env: {
57
44
  CARGO_TARGET_DIR: targetDirectory,
@@ -59,7 +46,7 @@ const cargoBuild = ({ functionName, srcDir, targetDirectory, }) => __awaiter(voi
59
46
  });
60
47
  }
61
48
  catch (error) {
62
- const hasToolchain = yield checkRustToolchain();
49
+ const hasToolchain = await checkRustToolchain();
63
50
  if (hasToolchain) {
64
51
  console.error(`Could not compile Rust function ${functionName}:\n`);
65
52
  }
@@ -70,38 +57,38 @@ const cargoBuild = ({ functionName, srcDir, targetDirectory, }) => __awaiter(voi
70
57
  error.customErrorInfo = { type: 'functionsBundling', location: { functionName, runtime: runtimeName } };
71
58
  throw error;
72
59
  }
73
- });
74
- const checkRustToolchain = () => __awaiter(void 0, void 0, void 0, function* () {
60
+ };
61
+ const checkRustToolchain = async () => {
75
62
  try {
76
- yield (0, shell_1.runCommand)('cargo', ['-V']);
63
+ await (0, shell_1.runCommand)('cargo', ['-V']);
77
64
  return true;
78
65
  }
79
- catch (_) {
66
+ catch {
80
67
  return false;
81
68
  }
82
- });
69
+ };
83
70
  // Returns the path of the Cargo target directory.
84
- const getTargetDirectory = ({ config, name }) => __awaiter(void 0, void 0, void 0, function* () {
71
+ const getTargetDirectory = async ({ config, name }) => {
85
72
  const { rustTargetDirectory } = config;
86
73
  // If the config includes a `rustTargetDirectory` path, we'll use that.
87
74
  if (rustTargetDirectory) {
88
75
  // We replace the [name] placeholder with the name of the function.
89
76
  const path = rustTargetDirectory.replace(/\[name]/g, name);
90
- yield (0, make_dir_1.default)(path);
77
+ await fs_1.promises.mkdir(path, { recursive: true });
91
78
  return path;
92
79
  }
93
80
  // If the directory hasn't been configured, we'll use a temporary directory.
94
- const { path } = yield tmp_promise_1.default.dir();
81
+ const { path } = await tmp_promise_1.default.dir();
95
82
  return path;
96
- });
83
+ };
97
84
  let toolchainInstallation;
98
85
  // Sets the default toolchain and installs the build target defined in
99
86
  // `BUILD_TARGET`. The Promise is saved to `toolchainInstallation`, so
100
87
  // that we run the command just once for multiple Rust functions.
101
- const installToolchain = () => __awaiter(void 0, void 0, void 0, function* () {
102
- yield (0, shell_1.runCommand)('rustup', ['default', 'stable']);
103
- yield (0, shell_1.runCommand)('rustup', ['target', 'add', constants_1.BUILD_TARGET]);
104
- });
88
+ const installToolchain = async () => {
89
+ await (0, shell_1.runCommand)('rustup', ['default', 'stable']);
90
+ await (0, shell_1.runCommand)('rustup', ['target', 'add', constants_1.BUILD_TARGET]);
91
+ };
105
92
  const installToolchainOnce = () => {
106
93
  if (toolchainInstallation === undefined) {
107
94
  toolchainInstallation = installToolchain();
@@ -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
  const path_1 = require("path");
13
4
  const fs_1 = require("../../utils/fs");
@@ -16,49 +7,48 @@ const zip_binary_1 = require("../../zip_binary");
16
7
  const detect_runtime_1 = require("../detect_runtime");
17
8
  const builder_1 = require("./builder");
18
9
  const constants_1 = require("./constants");
19
- const detectRustFunction = ({ fsCache, path }) => __awaiter(void 0, void 0, void 0, function* () {
20
- const stat = yield (0, fs_1.cachedLstat)(fsCache, path);
10
+ const detectRustFunction = async ({ fsCache, path }) => {
11
+ const stat = await (0, fs_1.cachedLstat)(fsCache, path);
21
12
  if (!stat.isDirectory()) {
22
13
  return;
23
14
  }
24
15
  // @ts-expect-error TODO: The `makeCachedFunction` abstraction is causing the
25
16
  // return value of `readdir` to be incorrectly typed.
26
- const files = (yield (0, fs_1.cachedReaddir)(fsCache, path));
17
+ const files = (await (0, fs_1.cachedReaddir)(fsCache, path));
27
18
  const hasCargoManifest = files.includes(constants_1.MANIFEST_NAME);
28
19
  if (!hasCargoManifest) {
29
20
  return;
30
21
  }
31
22
  const mainFilePath = (0, path_1.join)(path, 'src', 'main.rs');
32
23
  try {
33
- const mainFile = yield (0, fs_1.cachedLstat)(fsCache, mainFilePath);
24
+ const mainFile = await (0, fs_1.cachedLstat)(fsCache, mainFilePath);
34
25
  if (mainFile.isFile()) {
35
26
  return mainFilePath;
36
27
  }
37
28
  }
38
- catch (_) {
29
+ catch {
39
30
  // no-op
40
31
  }
41
- });
42
- const findFunctionsInPaths = function ({ featureFlags, fsCache, paths, }) {
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
- }
56
- })));
57
- return functions.filter(non_nullable_1.nonNullable);
58
- });
59
32
  };
60
- const processBinary = ({ fsCache, path }) => __awaiter(void 0, void 0, void 0, function* () {
61
- const stat = (yield (0, fs_1.cachedLstat)(fsCache, path));
33
+ const findFunctionsInPaths = async function ({ featureFlags, fsCache, paths, }) {
34
+ const functions = await Promise.all(paths.map((path) => findFunctionInPath({ path, featureFlags, fsCache })));
35
+ return functions.filter(non_nullable_1.nonNullable);
36
+ };
37
+ const findFunctionInPath = async function ({ path, featureFlags, fsCache }) {
38
+ const runtime = await (0, detect_runtime_1.detectBinaryRuntime)({ fsCache, path });
39
+ if (runtime === 'rs') {
40
+ return processBinary({ fsCache, path });
41
+ }
42
+ if (featureFlags.buildRustSource !== true) {
43
+ return;
44
+ }
45
+ const rustSourceFile = await detectRustFunction({ fsCache, path });
46
+ if (rustSourceFile) {
47
+ return processSource({ fsCache, mainFile: rustSourceFile, path });
48
+ }
49
+ };
50
+ const processBinary = async ({ fsCache, path }) => {
51
+ const stat = (await (0, fs_1.cachedLstat)(fsCache, path));
62
52
  const filename = (0, path_1.basename)(path);
63
53
  const extension = (0, path_1.extname)(path);
64
54
  const name = (0, path_1.basename)(path, extension);
@@ -71,13 +61,13 @@ const processBinary = ({ fsCache, path }) => __awaiter(void 0, void 0, void 0, f
71
61
  srcPath: path,
72
62
  stat,
73
63
  };
74
- });
75
- const processSource = ({ fsCache, mainFile, path, }) => __awaiter(void 0, void 0, void 0, function* () {
64
+ };
65
+ const processSource = async ({ fsCache, mainFile, path, }) => {
76
66
  // TODO: This `stat` value is not going to be used, but we need it to satisfy
77
67
  // the `FunctionSource` interface. We should revisit whether `stat` should be
78
68
  // part of that interface in the first place, or whether we could compute it
79
69
  // downstream when needed (maybe using the FS cache as an optimisation).
80
- const stat = (yield (0, fs_1.cachedLstat)(fsCache, path));
70
+ const stat = (await (0, fs_1.cachedLstat)(fsCache, path));
81
71
  const filename = (0, path_1.basename)(path);
82
72
  const extension = (0, path_1.extname)(path);
83
73
  const name = (0, path_1.basename)(path, extension);
@@ -90,32 +80,30 @@ const processSource = ({ fsCache, mainFile, path, }) => __awaiter(void 0, void 0
90
80
  srcPath: path,
91
81
  stat,
92
82
  };
93
- });
83
+ };
94
84
  // The name of the binary inside the zip file must always be `bootstrap`
95
85
  // because they include the Lambda runtime, and that's the name that AWS
96
86
  // expects for those kind of functions.
97
- const zipFunction = function ({ config, destFolder, filename, mainFile, runtime, srcDir, srcPath, stat, }) {
98
- return __awaiter(this, void 0, void 0, function* () {
99
- const destPath = (0, path_1.join)(destFolder, `${filename}.zip`);
100
- const isSource = (0, path_1.extname)(mainFile) === '.rs';
101
- const zipOptions = {
102
- destPath,
103
- filename: 'bootstrap',
104
- runtime,
105
- };
106
- // If we're building from source, we first need to build the source and zip
107
- // the resulting binary. Otherwise, we're dealing with a binary so we zip it
108
- // directly.
109
- if (isSource) {
110
- const { path: binaryPath, stat: binaryStat } = yield (0, builder_1.build)({ config, name: filename, srcDir });
111
- yield (0, zip_binary_1.zipBinary)(Object.assign(Object.assign({}, zipOptions), { srcPath: binaryPath, stat: binaryStat }));
112
- }
113
- else {
114
- yield (0, zip_binary_1.zipBinary)(Object.assign(Object.assign({}, zipOptions), { srcPath, stat }));
115
- }
116
- return { config, path: destPath };
117
- });
87
+ const zipFunction = async function ({ config, destFolder, filename, mainFile, runtime, srcDir, srcPath, stat, }) {
88
+ const destPath = (0, path_1.join)(destFolder, `${filename}.zip`);
89
+ const isSource = (0, path_1.extname)(mainFile) === '.rs';
90
+ const zipOptions = {
91
+ destPath,
92
+ filename: 'bootstrap',
93
+ runtime,
94
+ };
95
+ // If we're building from source, we first need to build the source and zip
96
+ // the resulting binary. Otherwise, we're dealing with a binary so we zip it
97
+ // directly.
98
+ if (isSource) {
99
+ const { path: binaryPath, stat: binaryStat } = await (0, builder_1.build)({ config, name: filename, srcDir });
100
+ await (0, zip_binary_1.zipBinary)({ ...zipOptions, srcPath: binaryPath, stat: binaryStat });
101
+ }
102
+ else {
103
+ await (0, zip_binary_1.zipBinary)({ ...zipOptions, srcPath, stat });
104
+ }
105
+ return { config, path: destPath };
118
106
  };
119
- const runtime = { findFunctionsInPaths, name: 'rs', zipFunction };
107
+ const runtime = { findFunctionsInPaths, findFunctionInPath, name: 'rs', zipFunction };
120
108
  exports.default = runtime;
121
109
  //# sourceMappingURL=index.js.map
@@ -1,26 +1,17 @@
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.addArchiveSize = void 0;
4
+ const fs_1 = require("fs");
13
5
  const path_1 = require("path");
14
- const fs_1 = require("./fs");
15
6
  // Returns the input object with an additional `size` property containing the
16
7
  // size of the file at `path` when it is a ZIP archive.
17
- const addArchiveSize = (result) => __awaiter(void 0, void 0, void 0, function* () {
8
+ const addArchiveSize = async (result) => {
18
9
  const { path } = result;
19
10
  if ((0, path_1.extname)(path) !== '.zip') {
20
11
  return result;
21
12
  }
22
- const { size } = yield (0, fs_1.stat)(path);
23
- return Object.assign(Object.assign({}, result), { size });
24
- });
13
+ const { size } = await fs_1.promises.stat(path);
14
+ return { ...result, size };
15
+ };
25
16
  exports.addArchiveSize = addArchiveSize;
26
17
  //# sourceMappingURL=archive_size.js.map
@@ -5,7 +5,12 @@ 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
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 });
8
+ const functionResult = {
9
+ ...archive,
10
+ inSourceConfig: undefined,
11
+ runtime: archive.runtime.name,
12
+ 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,
13
+ };
9
14
  return (0, remove_undefined_1.removeUndefined)(functionResult);
10
15
  };
11
16
  exports.formatZipResult = formatZipResult;
@@ -1,22 +1,18 @@
1
1
  /// <reference types="node" />
2
- import { lstat, readFile, stat, writeFile } from 'fs';
3
- declare const pLstat: typeof lstat.__promisify__;
4
- declare const pReadFile: typeof readFile.__promisify__;
5
- declare const pStat: typeof stat.__promisify__;
6
- declare const pWriteFile: typeof writeFile.__promisify__;
2
+ import { promises as fs } from 'fs';
7
3
  declare type FsCache = Record<string, unknown>;
8
- declare const cachedLstat: (cache: FsCache, path: string, options?: import("fs").StatOptions | undefined) => Promise<import("fs").Stats | import("fs").BigIntStats>;
4
+ declare const cachedLstat: (cache: FsCache, path: string, opts?: import("fs").StatOptions | undefined) => Promise<import("fs").Stats | import("fs").BigIntStats>;
9
5
  declare const cachedReaddir: (cache: FsCache, path: string, options: import("fs").ObjectEncodingOptions & {
10
6
  withFileTypes: true;
11
7
  }) => Promise<import("fs").Dirent[]>;
12
- declare const cachedReadFile: (cache: FsCache, path: string, options?: BufferEncoding | (import("fs").ObjectEncodingOptions & {
13
- flag?: string | undefined;
8
+ declare const cachedReadFile: (cache: FsCache, path: string, options?: BufferEncoding | (import("fs").ObjectEncodingOptions & import("events").Abortable & {
9
+ flag?: import("fs").OpenMode | undefined;
14
10
  }) | null | undefined) => Promise<string | Buffer>;
15
11
  declare const getPathWithExtension: (path: string, extension: string) => string;
16
12
  declare const safeUnlink: (path: string) => Promise<void>;
17
13
  declare const listFunctionsDirectories: (srcFolders: string[]) => Promise<string[]>;
18
14
  declare const listFunctionsDirectory: (srcFolder: string) => Promise<string[]>;
19
15
  declare const resolveFunctionsDirectories: (input: string | string[]) => string[];
20
- declare const mkdirAndWriteFile: typeof pWriteFile;
21
- export { cachedLstat, cachedReaddir, cachedReadFile, pLstat as lstat, getPathWithExtension, listFunctionsDirectories, listFunctionsDirectory, resolveFunctionsDirectories, safeUnlink, pStat as stat, pWriteFile as writeFile, pReadFile as readFile, mkdirAndWriteFile, };
16
+ declare const mkdirAndWriteFile: typeof fs.writeFile;
17
+ export { cachedLstat, cachedReaddir, cachedReadFile, getPathWithExtension, listFunctionsDirectories, listFunctionsDirectory, resolveFunctionsDirectories, safeUnlink, mkdirAndWriteFile, };
22
18
  export type { FsCache };
package/dist/utils/fs.js CHANGED
@@ -1,33 +1,9 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
2
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.mkdirAndWriteFile = exports.readFile = exports.writeFile = exports.stat = exports.safeUnlink = exports.resolveFunctionsDirectories = exports.listFunctionsDirectory = exports.listFunctionsDirectories = exports.getPathWithExtension = exports.lstat = exports.cachedReadFile = exports.cachedReaddir = exports.cachedLstat = void 0;
3
+ exports.mkdirAndWriteFile = exports.safeUnlink = exports.resolveFunctionsDirectories = exports.listFunctionsDirectory = exports.listFunctionsDirectories = exports.getPathWithExtension = exports.cachedReadFile = exports.cachedReaddir = exports.cachedLstat = void 0;
16
4
  const fs_1 = require("fs");
17
5
  const path_1 = require("path");
18
- const util_1 = require("util");
19
- const make_dir_1 = __importDefault(require("make-dir"));
20
6
  const non_nullable_1 = require("./non_nullable");
21
- const pLstat = (0, util_1.promisify)(fs_1.lstat);
22
- exports.lstat = pLstat;
23
- const pReaddir = (0, util_1.promisify)(fs_1.readdir);
24
- const pReadFile = (0, util_1.promisify)(fs_1.readFile);
25
- exports.readFile = pReadFile;
26
- const pStat = (0, util_1.promisify)(fs_1.stat);
27
- exports.stat = pStat;
28
- const pUnlink = (0, util_1.promisify)(fs_1.unlink);
29
- const pWriteFile = (0, util_1.promisify)(fs_1.writeFile);
30
- exports.writeFile = pWriteFile;
31
7
  // This caches multiple FS calls to the same path. It creates a cache key with
32
8
  // the name of the function and the path (e.g. "readdir:/some/directory").
33
9
  //
@@ -42,53 +18,49 @@ const makeCachedFunction = (func) => (cache, path, ...args) => {
42
18
  }
43
19
  return cache[key];
44
20
  };
45
- const cachedLstat = makeCachedFunction(pLstat);
21
+ const cachedLstat = makeCachedFunction(fs_1.promises.lstat);
46
22
  exports.cachedLstat = cachedLstat;
47
- const cachedReaddir = makeCachedFunction(pReaddir);
23
+ const cachedReaddir = makeCachedFunction(fs_1.promises.readdir);
48
24
  exports.cachedReaddir = cachedReaddir;
49
- const cachedReadFile = makeCachedFunction(pReadFile);
25
+ const cachedReadFile = makeCachedFunction(fs_1.promises.readFile);
50
26
  exports.cachedReadFile = cachedReadFile;
51
- const getPathWithExtension = (path, extension) => (0, path_1.format)(Object.assign(Object.assign({}, (0, path_1.parse)(path)), { base: undefined, ext: extension }));
27
+ const getPathWithExtension = (path, extension) => (0, path_1.format)({ ...(0, path_1.parse)(path), base: undefined, ext: extension });
52
28
  exports.getPathWithExtension = getPathWithExtension;
53
- const safeUnlink = (path) => __awaiter(void 0, void 0, void 0, function* () {
29
+ const safeUnlink = async (path) => {
54
30
  try {
55
- yield pUnlink(path);
31
+ await fs_1.promises.unlink(path);
56
32
  }
57
- catch (_) { }
58
- });
33
+ catch { }
34
+ };
59
35
  exports.safeUnlink = safeUnlink;
60
36
  // Takes a list of absolute paths and returns an array containing all the
61
37
  // filenames within those directories, if at least one of the directories
62
38
  // exists. If not, an error is thrown.
63
- const listFunctionsDirectories = function (srcFolders) {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- const filenamesByDirectory = yield Promise.all(srcFolders.map((srcFolder) => __awaiter(this, void 0, void 0, function* () {
66
- try {
67
- const filenames = yield listFunctionsDirectory(srcFolder);
68
- return filenames;
69
- }
70
- catch (error) {
71
- return null;
72
- }
73
- })));
74
- const validDirectories = filenamesByDirectory.filter(non_nullable_1.nonNullable);
75
- if (validDirectories.length === 0) {
76
- throw new Error(`Functions folder does not exist: ${srcFolders.join(', ')}`);
77
- }
78
- return validDirectories.flat();
79
- });
80
- };
81
- exports.listFunctionsDirectories = listFunctionsDirectories;
82
- const listFunctionsDirectory = function (srcFolder) {
83
- return __awaiter(this, void 0, void 0, function* () {
39
+ const listFunctionsDirectories = async function (srcFolders) {
40
+ const filenamesByDirectory = await Promise.all(srcFolders.map(async (srcFolder) => {
84
41
  try {
85
- const filenames = yield pReaddir(srcFolder);
86
- return filenames.map((name) => (0, path_1.join)(srcFolder, name));
42
+ const filenames = await listFunctionsDirectory(srcFolder);
43
+ return filenames;
87
44
  }
88
- catch (error) {
89
- throw new Error(`Functions folder does not exist: ${srcFolder}`);
45
+ catch {
46
+ return null;
90
47
  }
91
- });
48
+ }));
49
+ const validDirectories = filenamesByDirectory.filter(non_nullable_1.nonNullable);
50
+ if (validDirectories.length === 0) {
51
+ throw new Error(`Functions folder does not exist: ${srcFolders.join(', ')}`);
52
+ }
53
+ return validDirectories.flat();
54
+ };
55
+ exports.listFunctionsDirectories = listFunctionsDirectories;
56
+ const listFunctionsDirectory = async function (srcFolder) {
57
+ try {
58
+ const filenames = await fs_1.promises.readdir(srcFolder);
59
+ return filenames.map((name) => (0, path_1.join)(srcFolder, name));
60
+ }
61
+ catch {
62
+ throw new Error(`Functions folder does not exist: ${srcFolder}`);
63
+ }
92
64
  };
93
65
  exports.listFunctionsDirectory = listFunctionsDirectory;
94
66
  const resolveFunctionsDirectories = (input) => {
@@ -97,12 +69,12 @@ const resolveFunctionsDirectories = (input) => {
97
69
  return absoluteDirectories;
98
70
  };
99
71
  exports.resolveFunctionsDirectories = resolveFunctionsDirectories;
100
- const mkdirAndWriteFile = (path, ...params) => __awaiter(void 0, void 0, void 0, function* () {
72
+ const mkdirAndWriteFile = async (path, ...params) => {
101
73
  if (typeof path === 'string') {
102
74
  const directory = (0, path_1.dirname)(path);
103
- yield (0, make_dir_1.default)(directory);
75
+ await fs_1.promises.mkdir(directory, { recursive: true });
104
76
  }
105
- return pWriteFile(path, ...params);
106
- });
77
+ return fs_1.promises.writeFile(path, ...params);
78
+ };
107
79
  exports.mkdirAndWriteFile = mkdirAndWriteFile;
108
80
  //# sourceMappingURL=fs.js.map