@vercel/build-utils 7.1.0 → 7.2.0

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.
@@ -1,468 +1,530 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
4
11
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.installDependencies = exports.getScriptName = exports.runPipInstall = exports.runBundleInstall = exports.runPackageJsonScript = exports.runCustomInstallCommand = exports.getEnvForPackageManager = exports.runNpmInstall = exports.walkParentDirs = exports.scanParentDirs = exports.getNodeVersion = exports.getSpawnOptions = exports.runShellScript = exports.getNodeBinPaths = exports.getNodeBinPath = exports.traverseUpDirectories = exports.execCommand = exports.spawnCommand = exports.spawnAsync = void 0;
7
- const assert_1 = __importDefault(require("assert"));
8
- const fs_extra_1 = __importDefault(require("fs-extra"));
9
- const path_1 = __importDefault(require("path"));
10
- const async_sema_1 = __importDefault(require("async-sema"));
11
- const cross_spawn_1 = __importDefault(require("cross-spawn"));
12
- const semver_1 = require("semver");
13
- const util_1 = require("util");
14
- const debug_1 = __importDefault(require("../debug"));
15
- const errors_1 = require("../errors");
16
- const node_version_1 = require("./node-version");
17
- const read_config_file_1 = require("./read-config-file");
18
- const clone_env_1 = require("../clone-env");
19
- // Only allow one `runNpmInstall()` invocation to run concurrently
20
- const runNpmInstallSema = new async_sema_1.default(1);
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var run_user_scripts_exports = {};
30
+ __export(run_user_scripts_exports, {
31
+ execCommand: () => execCommand,
32
+ getEnvForPackageManager: () => getEnvForPackageManager,
33
+ getNodeBinPath: () => getNodeBinPath,
34
+ getNodeBinPaths: () => getNodeBinPaths,
35
+ getNodeVersion: () => getNodeVersion,
36
+ getScriptName: () => getScriptName,
37
+ getSpawnOptions: () => getSpawnOptions,
38
+ installDependencies: () => installDependencies,
39
+ runBundleInstall: () => runBundleInstall,
40
+ runCustomInstallCommand: () => runCustomInstallCommand,
41
+ runNpmInstall: () => runNpmInstall,
42
+ runPackageJsonScript: () => runPackageJsonScript,
43
+ runPipInstall: () => runPipInstall,
44
+ runShellScript: () => runShellScript,
45
+ scanParentDirs: () => scanParentDirs,
46
+ spawnAsync: () => spawnAsync,
47
+ spawnCommand: () => spawnCommand,
48
+ traverseUpDirectories: () => traverseUpDirectories,
49
+ walkParentDirs: () => walkParentDirs
50
+ });
51
+ module.exports = __toCommonJS(run_user_scripts_exports);
52
+ var import_assert = __toESM(require("assert"));
53
+ var import_fs_extra = __toESM(require("fs-extra"));
54
+ var import_path = __toESM(require("path"));
55
+ var import_async_sema = __toESM(require("async-sema"));
56
+ var import_cross_spawn = __toESM(require("cross-spawn"));
57
+ var import_semver = require("semver");
58
+ var import_util = require("util");
59
+ var import_debug = __toESM(require("../debug"));
60
+ var import_errors = require("../errors");
61
+ var import_node_version = require("./node-version");
62
+ var import_read_config_file = require("./read-config-file");
63
+ var import_clone_env = require("../clone-env");
64
+ const runNpmInstallSema = new import_async_sema.default(1);
21
65
  function spawnAsync(command, args, opts = {}) {
22
- return new Promise((resolve, reject) => {
23
- const stderrLogs = [];
24
- opts = { stdio: 'inherit', ...opts };
25
- const child = (0, cross_spawn_1.default)(command, args, opts);
26
- if (opts.stdio === 'pipe' && child.stderr) {
27
- child.stderr.on('data', data => stderrLogs.push(data));
28
- }
29
- child.on('error', reject);
30
- child.on('close', (code, signal) => {
31
- if (code === 0 || opts.ignoreNon0Exit) {
32
- return resolve();
33
- }
34
- const cmd = opts.prettyCommand
35
- ? `Command "${opts.prettyCommand}"`
36
- : 'Command';
37
- reject(new errors_1.NowBuildError({
38
- code: `BUILD_UTILS_SPAWN_${code || signal}`,
39
- message: opts.stdio === 'inherit'
40
- ? `${cmd} exited with ${code || signal}`
41
- : stderrLogs.map(line => line.toString()).join(''),
42
- }));
43
- });
66
+ return new Promise((resolve, reject) => {
67
+ const stderrLogs = [];
68
+ opts = { stdio: "inherit", ...opts };
69
+ const child = (0, import_cross_spawn.default)(command, args, opts);
70
+ if (opts.stdio === "pipe" && child.stderr) {
71
+ child.stderr.on("data", (data) => stderrLogs.push(data));
72
+ }
73
+ child.on("error", reject);
74
+ child.on("close", (code, signal) => {
75
+ if (code === 0 || opts.ignoreNon0Exit) {
76
+ return resolve();
77
+ }
78
+ const cmd = opts.prettyCommand ? `Command "${opts.prettyCommand}"` : "Command";
79
+ reject(
80
+ new import_errors.NowBuildError({
81
+ code: `BUILD_UTILS_SPAWN_${code || signal}`,
82
+ message: opts.stdio === "inherit" ? `${cmd} exited with ${code || signal}` : stderrLogs.map((line) => line.toString()).join("")
83
+ })
84
+ );
44
85
  });
86
+ });
45
87
  }
46
- exports.spawnAsync = spawnAsync;
47
88
  function spawnCommand(command, options = {}) {
48
- const opts = { ...options, prettyCommand: command };
49
- if (process.platform === 'win32') {
50
- return (0, cross_spawn_1.default)('cmd.exe', ['/C', command], opts);
51
- }
52
- return (0, cross_spawn_1.default)('sh', ['-c', command], opts);
89
+ const opts = { ...options, prettyCommand: command };
90
+ if (process.platform === "win32") {
91
+ return (0, import_cross_spawn.default)("cmd.exe", ["/C", command], opts);
92
+ }
93
+ return (0, import_cross_spawn.default)("sh", ["-c", command], opts);
53
94
  }
54
- exports.spawnCommand = spawnCommand;
55
95
  async function execCommand(command, options = {}) {
56
- const opts = { ...options, prettyCommand: command };
57
- if (process.platform === 'win32') {
58
- await spawnAsync('cmd.exe', ['/C', command], opts);
59
- }
60
- else {
61
- await spawnAsync('sh', ['-c', command], opts);
62
- }
63
- return true;
96
+ const opts = { ...options, prettyCommand: command };
97
+ if (process.platform === "win32") {
98
+ await spawnAsync("cmd.exe", ["/C", command], opts);
99
+ } else {
100
+ await spawnAsync("sh", ["-c", command], opts);
101
+ }
102
+ return true;
64
103
  }
65
- exports.execCommand = execCommand;
66
- function* traverseUpDirectories({ start, base, }) {
67
- let current = path_1.default.normalize(start);
68
- const normalizedRoot = base ? path_1.default.normalize(base) : undefined;
69
- while (current) {
70
- yield current;
71
- if (current === normalizedRoot)
72
- break;
73
- // Go up one directory
74
- const next = path_1.default.join(current, '..');
75
- current = next === current ? undefined : next;
76
- }
104
+ function* traverseUpDirectories({
105
+ start,
106
+ base
107
+ }) {
108
+ let current = import_path.default.normalize(start);
109
+ const normalizedRoot = base ? import_path.default.normalize(base) : void 0;
110
+ while (current) {
111
+ yield current;
112
+ if (current === normalizedRoot)
113
+ break;
114
+ const next = import_path.default.join(current, "..");
115
+ current = next === current ? void 0 : next;
116
+ }
77
117
  }
78
- exports.traverseUpDirectories = traverseUpDirectories;
79
- /**
80
- * @deprecated Use `getNodeBinPaths()` instead.
81
- */
82
- async function getNodeBinPath({ cwd, }) {
83
- const { lockfilePath } = await scanParentDirs(cwd);
84
- const dir = path_1.default.dirname(lockfilePath || cwd);
85
- return path_1.default.join(dir, 'node_modules', '.bin');
118
+ async function getNodeBinPath({
119
+ cwd
120
+ }) {
121
+ const { lockfilePath } = await scanParentDirs(cwd);
122
+ const dir = import_path.default.dirname(lockfilePath || cwd);
123
+ return import_path.default.join(dir, "node_modules", ".bin");
86
124
  }
87
- exports.getNodeBinPath = getNodeBinPath;
88
- function getNodeBinPaths({ start, base, }) {
89
- return Array.from(traverseUpDirectories({ start, base })).map(dir => path_1.default.join(dir, 'node_modules/.bin'));
125
+ function getNodeBinPaths({
126
+ start,
127
+ base
128
+ }) {
129
+ return Array.from(traverseUpDirectories({ start, base })).map(
130
+ (dir) => import_path.default.join(dir, "node_modules/.bin")
131
+ );
90
132
  }
91
- exports.getNodeBinPaths = getNodeBinPaths;
92
133
  async function chmodPlusX(fsPath) {
93
- const s = await fs_extra_1.default.stat(fsPath);
94
- const newMode = s.mode | 64 | 8 | 1; // eslint-disable-line no-bitwise
95
- if (s.mode === newMode)
96
- return;
97
- const base8 = newMode.toString(8).slice(-3);
98
- await fs_extra_1.default.chmod(fsPath, base8);
134
+ const s = await import_fs_extra.default.stat(fsPath);
135
+ const newMode = s.mode | 64 | 8 | 1;
136
+ if (s.mode === newMode)
137
+ return;
138
+ const base8 = newMode.toString(8).slice(-3);
139
+ await import_fs_extra.default.chmod(fsPath, base8);
99
140
  }
100
141
  async function runShellScript(fsPath, args = [], spawnOpts) {
101
- (0, assert_1.default)(path_1.default.isAbsolute(fsPath));
102
- const destPath = path_1.default.dirname(fsPath);
103
- await chmodPlusX(fsPath);
104
- const command = `./${path_1.default.basename(fsPath)}`;
105
- await spawnAsync(command, args, {
106
- ...spawnOpts,
107
- cwd: destPath,
108
- prettyCommand: command,
109
- });
110
- return true;
142
+ (0, import_assert.default)(import_path.default.isAbsolute(fsPath));
143
+ const destPath = import_path.default.dirname(fsPath);
144
+ await chmodPlusX(fsPath);
145
+ const command = `./${import_path.default.basename(fsPath)}`;
146
+ await spawnAsync(command, args, {
147
+ ...spawnOpts,
148
+ cwd: destPath,
149
+ prettyCommand: command
150
+ });
151
+ return true;
111
152
  }
112
- exports.runShellScript = runShellScript;
113
153
  function getSpawnOptions(meta, nodeVersion) {
114
- const opts = {
115
- env: (0, clone_env_1.cloneEnv)(process.env),
116
- };
117
- if (!meta.isDev) {
118
- let found = false;
119
- const oldPath = opts.env.PATH || process.env.PATH || '';
120
- const pathSegments = oldPath.split(path_1.default.delimiter).map(segment => {
121
- if (/^\/node[0-9]+\/bin/.test(segment)) {
122
- found = true;
123
- return `/node${nodeVersion.major}/bin`;
124
- }
125
- return segment;
126
- });
127
- if (!found) {
128
- // If we didn't find & replace, prepend at beginning of PATH
129
- pathSegments.unshift(`/node${nodeVersion.major}/bin`);
130
- }
131
- opts.env.PATH = pathSegments.filter(Boolean).join(path_1.default.delimiter);
154
+ const opts = {
155
+ env: (0, import_clone_env.cloneEnv)(process.env)
156
+ };
157
+ if (!meta.isDev) {
158
+ let found = false;
159
+ const oldPath = opts.env.PATH || process.env.PATH || "";
160
+ const pathSegments = oldPath.split(import_path.default.delimiter).map((segment) => {
161
+ if (/^\/node[0-9]+\/bin/.test(segment)) {
162
+ found = true;
163
+ return `/node${nodeVersion.major}/bin`;
164
+ }
165
+ return segment;
166
+ });
167
+ if (!found) {
168
+ pathSegments.unshift(`/node${nodeVersion.major}/bin`);
132
169
  }
133
- return opts;
170
+ opts.env.PATH = pathSegments.filter(Boolean).join(import_path.default.delimiter);
171
+ }
172
+ return opts;
134
173
  }
135
- exports.getSpawnOptions = getSpawnOptions;
136
174
  async function getNodeVersion(destPath, nodeVersionFallback = process.env.VERCEL_PROJECT_SETTINGS_NODE_VERSION, config = {}, meta = {}) {
137
- const latest = (0, node_version_1.getLatestNodeVersion)();
138
- if (meta.isDev) {
139
- // Use the system-installed version of `node` in PATH for `vercel dev`
140
- return { ...latest, runtime: 'nodejs' };
175
+ const latest = (0, import_node_version.getLatestNodeVersion)();
176
+ if (meta.isDev) {
177
+ return { ...latest, runtime: "nodejs" };
178
+ }
179
+ const { packageJson } = await scanParentDirs(destPath, true);
180
+ let nodeVersion = config.nodeVersion || nodeVersionFallback;
181
+ let isAuto = true;
182
+ if (packageJson?.engines?.node) {
183
+ const { node } = packageJson.engines;
184
+ if (nodeVersion && (0, import_semver.validRange)(node) && !(0, import_semver.intersects)(nodeVersion, node)) {
185
+ console.warn(
186
+ `Warning: Due to "engines": { "node": "${node}" } in your \`package.json\` file, the Node.js Version defined in your Project Settings ("${nodeVersion}") will not apply. Learn More: http://vercel.link/node-version`
187
+ );
188
+ } else if ((0, import_semver.coerce)(node)?.raw === node) {
189
+ console.warn(
190
+ `Warning: Detected "engines": { "node": "${node}" } in your \`package.json\` with major.minor.patch, but only major Node.js Version can be selected. Learn More: http://vercel.link/node-version`
191
+ );
192
+ } else if ((0, import_semver.validRange)(node) && (0, import_semver.intersects)(`${latest.major + 1}.x`, node)) {
193
+ console.warn(
194
+ `Warning: Detected "engines": { "node": "${node}" } in your \`package.json\` that will automatically upgrade when a new major Node.js Version is released. Learn More: http://vercel.link/node-version`
195
+ );
141
196
  }
142
- const { packageJson } = await scanParentDirs(destPath, true);
143
- let nodeVersion = config.nodeVersion || nodeVersionFallback;
144
- let isAuto = true;
145
- if (packageJson?.engines?.node) {
146
- const { node } = packageJson.engines;
147
- if (nodeVersion && (0, semver_1.validRange)(node) && !(0, semver_1.intersects)(nodeVersion, node)) {
148
- console.warn(`Warning: Due to "engines": { "node": "${node}" } in your \`package.json\` file, the Node.js Version defined in your Project Settings ("${nodeVersion}") will not apply. Learn More: http://vercel.link/node-version`);
149
- }
150
- else if ((0, semver_1.coerce)(node)?.raw === node) {
151
- console.warn(`Warning: Detected "engines": { "node": "${node}" } in your \`package.json\` with major.minor.patch, but only major Node.js Version can be selected. Learn More: http://vercel.link/node-version`);
152
- }
153
- else if ((0, semver_1.validRange)(node) && (0, semver_1.intersects)(`${latest.major + 1}.x`, node)) {
154
- console.warn(`Warning: Detected "engines": { "node": "${node}" } in your \`package.json\` that will automatically upgrade when a new major Node.js Version is released. Learn More: http://vercel.link/node-version`);
155
- }
156
- nodeVersion = node;
157
- isAuto = false;
158
- }
159
- return (0, node_version_1.getSupportedNodeVersion)(nodeVersion, isAuto);
197
+ nodeVersion = node;
198
+ isAuto = false;
199
+ }
200
+ return (0, import_node_version.getSupportedNodeVersion)(nodeVersion, isAuto);
160
201
  }
161
- exports.getNodeVersion = getNodeVersion;
162
202
  async function scanParentDirs(destPath, readPackageJson = false) {
163
- (0, assert_1.default)(path_1.default.isAbsolute(destPath));
164
- const pkgJsonPath = await walkParentDirs({
165
- base: '/',
166
- start: destPath,
167
- filename: 'package.json',
168
- });
169
- const packageJson = readPackageJson && pkgJsonPath
170
- ? JSON.parse(await fs_extra_1.default.readFile(pkgJsonPath, 'utf8'))
171
- : undefined;
172
- const [yarnLockPath, npmLockPath, pnpmLockPath] = await walkParentDirsMulti({
173
- base: '/',
174
- start: destPath,
175
- filenames: ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml'],
176
- });
177
- let lockfilePath;
178
- let lockfileVersion;
179
- let cliType = 'yarn';
180
- const [hasYarnLock, packageLockJson, pnpmLockYaml] = await Promise.all([
181
- Boolean(yarnLockPath),
182
- npmLockPath
183
- ? (0, read_config_file_1.readConfigFile)(npmLockPath)
184
- : null,
185
- pnpmLockPath
186
- ? (0, read_config_file_1.readConfigFile)(pnpmLockPath)
187
- : null,
188
- ]);
189
- // Priority order is Yarn > pnpm > npm
190
- if (hasYarnLock) {
191
- cliType = 'yarn';
192
- lockfilePath = yarnLockPath;
193
- }
194
- else if (pnpmLockYaml) {
195
- cliType = 'pnpm';
196
- lockfilePath = pnpmLockPath;
197
- lockfileVersion = Number(pnpmLockYaml.lockfileVersion);
198
- }
199
- else if (packageLockJson) {
200
- cliType = 'npm';
201
- lockfilePath = npmLockPath;
202
- lockfileVersion = packageLockJson.lockfileVersion;
203
- }
204
- const packageJsonPath = pkgJsonPath || undefined;
205
- return {
206
- cliType,
207
- packageJson,
208
- lockfilePath,
209
- lockfileVersion,
210
- packageJsonPath,
211
- };
203
+ (0, import_assert.default)(import_path.default.isAbsolute(destPath));
204
+ const pkgJsonPath = await walkParentDirs({
205
+ base: "/",
206
+ start: destPath,
207
+ filename: "package.json"
208
+ });
209
+ const packageJson = readPackageJson && pkgJsonPath ? JSON.parse(await import_fs_extra.default.readFile(pkgJsonPath, "utf8")) : void 0;
210
+ const [yarnLockPath, npmLockPath, pnpmLockPath, bunLockPath] = await walkParentDirsMulti({
211
+ base: "/",
212
+ start: destPath,
213
+ filenames: [
214
+ "yarn.lock",
215
+ "package-lock.json",
216
+ "pnpm-lock.yaml",
217
+ "bun.lockb"
218
+ ]
219
+ });
220
+ let lockfilePath;
221
+ let lockfileVersion;
222
+ let cliType = "yarn";
223
+ const [hasYarnLock, packageLockJson, pnpmLockYaml, bunLockBin] = await Promise.all([
224
+ Boolean(yarnLockPath),
225
+ npmLockPath ? (0, import_read_config_file.readConfigFile)(npmLockPath) : null,
226
+ pnpmLockPath ? (0, import_read_config_file.readConfigFile)(pnpmLockPath) : null,
227
+ bunLockPath ? import_fs_extra.default.readFile(bunLockPath, "utf8") : null
228
+ ]);
229
+ if (hasYarnLock) {
230
+ cliType = "yarn";
231
+ lockfilePath = yarnLockPath;
232
+ } else if (pnpmLockYaml) {
233
+ cliType = "pnpm";
234
+ lockfilePath = pnpmLockPath;
235
+ lockfileVersion = Number(pnpmLockYaml.lockfileVersion);
236
+ } else if (packageLockJson) {
237
+ cliType = "npm";
238
+ lockfilePath = npmLockPath;
239
+ lockfileVersion = packageLockJson.lockfileVersion;
240
+ } else if (bunLockBin) {
241
+ cliType = "bun";
242
+ lockfilePath = bunLockPath;
243
+ lockfileVersion = 0;
244
+ }
245
+ const packageJsonPath = pkgJsonPath || void 0;
246
+ return {
247
+ cliType,
248
+ packageJson,
249
+ lockfilePath,
250
+ lockfileVersion,
251
+ packageJsonPath
252
+ };
212
253
  }
213
- exports.scanParentDirs = scanParentDirs;
214
- async function walkParentDirs({ base, start, filename, }) {
215
- (0, assert_1.default)(path_1.default.isAbsolute(base), 'Expected "base" to be absolute path');
216
- (0, assert_1.default)(path_1.default.isAbsolute(start), 'Expected "start" to be absolute path');
217
- for (const dir of traverseUpDirectories({ start, base })) {
218
- const fullPath = path_1.default.join(dir, filename);
219
- // eslint-disable-next-line no-await-in-loop
220
- if (await fs_extra_1.default.pathExists(fullPath)) {
221
- return fullPath;
222
- }
254
+ async function walkParentDirs({
255
+ base,
256
+ start,
257
+ filename
258
+ }) {
259
+ (0, import_assert.default)(import_path.default.isAbsolute(base), 'Expected "base" to be absolute path');
260
+ (0, import_assert.default)(import_path.default.isAbsolute(start), 'Expected "start" to be absolute path');
261
+ for (const dir of traverseUpDirectories({ start, base })) {
262
+ const fullPath = import_path.default.join(dir, filename);
263
+ if (await import_fs_extra.default.pathExists(fullPath)) {
264
+ return fullPath;
223
265
  }
224
- return null;
266
+ }
267
+ return null;
225
268
  }
226
- exports.walkParentDirs = walkParentDirs;
227
- async function walkParentDirsMulti({ base, start, filenames, }) {
228
- for (const dir of traverseUpDirectories({ start, base })) {
229
- const fullPaths = filenames.map(f => path_1.default.join(dir, f));
230
- const existResults = await Promise.all(fullPaths.map(f => fs_extra_1.default.pathExists(f)));
231
- const foundOneOrMore = existResults.some(b => b);
232
- if (foundOneOrMore) {
233
- return fullPaths.map((f, i) => (existResults[i] ? f : undefined));
234
- }
269
+ async function walkParentDirsMulti({
270
+ base,
271
+ start,
272
+ filenames
273
+ }) {
274
+ for (const dir of traverseUpDirectories({ start, base })) {
275
+ const fullPaths = filenames.map((f) => import_path.default.join(dir, f));
276
+ const existResults = await Promise.all(
277
+ fullPaths.map((f) => import_fs_extra.default.pathExists(f))
278
+ );
279
+ const foundOneOrMore = existResults.some((b) => b);
280
+ if (foundOneOrMore) {
281
+ return fullPaths.map((f, i) => existResults[i] ? f : void 0);
235
282
  }
236
- return [];
283
+ }
284
+ return [];
237
285
  }
238
286
  function isSet(v) {
239
- return v?.constructor?.name === 'Set';
287
+ return v?.constructor?.name === "Set";
240
288
  }
241
289
  async function runNpmInstall(destPath, args = [], spawnOpts, meta, nodeVersion) {
242
- if (meta?.isDev) {
243
- (0, debug_1.default)('Skipping dependency installation because dev mode is enabled');
244
- return false;
245
- }
246
- (0, assert_1.default)(path_1.default.isAbsolute(destPath));
247
- try {
248
- await runNpmInstallSema.acquire();
249
- const { cliType, packageJsonPath, lockfileVersion } = await scanParentDirs(destPath);
250
- // Only allow `runNpmInstall()` to run once per `package.json`
251
- // when doing a default install (no additional args)
252
- if (meta && packageJsonPath && args.length === 0) {
253
- if (!isSet(meta.runNpmInstallSet)) {
254
- meta.runNpmInstallSet = new Set();
255
- }
256
- if (isSet(meta.runNpmInstallSet)) {
257
- if (meta.runNpmInstallSet.has(packageJsonPath)) {
258
- return false;
259
- }
260
- else {
261
- meta.runNpmInstallSet.add(packageJsonPath);
262
- }
263
- }
290
+ if (meta?.isDev) {
291
+ (0, import_debug.default)("Skipping dependency installation because dev mode is enabled");
292
+ return false;
293
+ }
294
+ (0, import_assert.default)(import_path.default.isAbsolute(destPath));
295
+ try {
296
+ await runNpmInstallSema.acquire();
297
+ const { cliType, packageJsonPath, lockfileVersion } = await scanParentDirs(
298
+ destPath
299
+ );
300
+ if (meta && packageJsonPath && args.length === 0) {
301
+ if (!isSet(meta.runNpmInstallSet)) {
302
+ meta.runNpmInstallSet = /* @__PURE__ */ new Set();
303
+ }
304
+ if (isSet(meta.runNpmInstallSet)) {
305
+ if (meta.runNpmInstallSet.has(packageJsonPath)) {
306
+ return false;
307
+ } else {
308
+ meta.runNpmInstallSet.add(packageJsonPath);
264
309
  }
265
- const installTime = Date.now();
266
- console.log('Installing dependencies...');
267
- (0, debug_1.default)(`Installing to ${destPath}`);
268
- const opts = { cwd: destPath, ...spawnOpts };
269
- const env = (0, clone_env_1.cloneEnv)(opts.env || process.env);
270
- delete env.NODE_ENV;
271
- opts.env = getEnvForPackageManager({
272
- cliType,
273
- lockfileVersion,
274
- nodeVersion,
275
- env,
276
- });
277
- let commandArgs;
278
- const isPotentiallyBrokenNpm = cliType === 'npm' &&
279
- (nodeVersion?.major === 16 ||
280
- opts.env.PATH?.includes('/node16/bin-npm7')) &&
281
- !args.includes('--legacy-peer-deps') &&
282
- spawnOpts?.env?.ENABLE_EXPERIMENTAL_COREPACK !== '1';
283
- if (cliType === 'npm') {
284
- opts.prettyCommand = 'npm install';
285
- commandArgs = args
286
- .filter(a => a !== '--prefer-offline')
287
- .concat(['install', '--no-audit', '--unsafe-perm']);
288
- if (isPotentiallyBrokenNpm &&
289
- spawnOpts?.env?.VERCEL_NPM_LEGACY_PEER_DEPS === '1') {
290
- // Starting in npm@8.6.0, if you ran `npm install --legacy-peer-deps`,
291
- // and then later ran `npm install`, it would fail. So the only way
292
- // to safely upgrade npm from npm@8.5.0 is to set this flag. The docs
293
- // say this flag is not recommended so its is behind a feature flag
294
- // so we can remove it in node@18, which can introduce breaking changes.
295
- // See https://docs.npmjs.com/cli/v8/using-npm/config#legacy-peer-deps
296
- commandArgs.push('--legacy-peer-deps');
297
- }
298
- }
299
- else if (cliType === 'pnpm') {
300
- // PNPM's install command is similar to NPM's but without the audit nonsense
301
- // @see options https://pnpm.io/cli/install
302
- opts.prettyCommand = 'pnpm install';
303
- commandArgs = args
304
- .filter(a => a !== '--prefer-offline')
305
- .concat(['install', '--unsafe-perm']);
306
- }
307
- else {
308
- opts.prettyCommand = 'yarn install';
309
- commandArgs = ['install', ...args];
310
- }
311
- if (process.env.NPM_ONLY_PRODUCTION) {
312
- commandArgs.push('--production');
313
- }
314
- try {
315
- await spawnAsync(cliType, commandArgs, opts);
316
- }
317
- catch (err) {
318
- const potentialErrorPath = path_1.default.join(process.env.HOME || '/', '.npm', 'eresolve-report.txt');
319
- if (isPotentiallyBrokenNpm &&
320
- !commandArgs.includes('--legacy-peer-deps') &&
321
- fs_extra_1.default.existsSync(potentialErrorPath)) {
322
- console.warn('Warning: Retrying "Install Command" with `--legacy-peer-deps` which may accept a potentially broken dependency and slow install time.');
323
- commandArgs.push('--legacy-peer-deps');
324
- await spawnAsync(cliType, commandArgs, opts);
325
- }
326
- else {
327
- throw err;
328
- }
329
- }
330
- (0, debug_1.default)(`Install complete [${Date.now() - installTime}ms]`);
331
- return true;
310
+ }
332
311
  }
333
- finally {
334
- runNpmInstallSema.release();
312
+ const installTime = Date.now();
313
+ console.log("Installing dependencies...");
314
+ (0, import_debug.default)(`Installing to ${destPath}`);
315
+ const opts = { cwd: destPath, ...spawnOpts };
316
+ const env = (0, import_clone_env.cloneEnv)(opts.env || process.env);
317
+ delete env.NODE_ENV;
318
+ opts.env = getEnvForPackageManager({
319
+ cliType,
320
+ lockfileVersion,
321
+ nodeVersion,
322
+ env
323
+ });
324
+ let commandArgs;
325
+ const isPotentiallyBrokenNpm = cliType === "npm" && (nodeVersion?.major === 16 || opts.env.PATH?.includes("/node16/bin-npm7")) && !args.includes("--legacy-peer-deps") && spawnOpts?.env?.ENABLE_EXPERIMENTAL_COREPACK !== "1";
326
+ if (cliType === "npm") {
327
+ opts.prettyCommand = "npm install";
328
+ commandArgs = args.filter((a) => a !== "--prefer-offline").concat(["install", "--no-audit", "--unsafe-perm"]);
329
+ if (isPotentiallyBrokenNpm && spawnOpts?.env?.VERCEL_NPM_LEGACY_PEER_DEPS === "1") {
330
+ commandArgs.push("--legacy-peer-deps");
331
+ }
332
+ } else if (cliType === "pnpm") {
333
+ opts.prettyCommand = "pnpm install";
334
+ commandArgs = args.filter((a) => a !== "--prefer-offline").concat(["install", "--unsafe-perm"]);
335
+ } else if (cliType === "bun") {
336
+ opts.prettyCommand = "bun install";
337
+ commandArgs = ["install", ...args];
338
+ } else {
339
+ opts.prettyCommand = "yarn install";
340
+ commandArgs = ["install", ...args];
335
341
  }
342
+ if (process.env.NPM_ONLY_PRODUCTION) {
343
+ commandArgs.push("--production");
344
+ }
345
+ try {
346
+ await spawnAsync(cliType, commandArgs, opts);
347
+ } catch (err) {
348
+ const potentialErrorPath = import_path.default.join(
349
+ process.env.HOME || "/",
350
+ ".npm",
351
+ "eresolve-report.txt"
352
+ );
353
+ if (isPotentiallyBrokenNpm && !commandArgs.includes("--legacy-peer-deps") && import_fs_extra.default.existsSync(potentialErrorPath)) {
354
+ console.warn(
355
+ 'Warning: Retrying "Install Command" with `--legacy-peer-deps` which may accept a potentially broken dependency and slow install time.'
356
+ );
357
+ commandArgs.push("--legacy-peer-deps");
358
+ await spawnAsync(cliType, commandArgs, opts);
359
+ } else {
360
+ throw err;
361
+ }
362
+ }
363
+ (0, import_debug.default)(`Install complete [${Date.now() - installTime}ms]`);
364
+ return true;
365
+ } finally {
366
+ runNpmInstallSema.release();
367
+ }
336
368
  }
337
- exports.runNpmInstall = runNpmInstall;
338
- function getEnvForPackageManager({ cliType, lockfileVersion, nodeVersion, env, }) {
339
- const newEnv = { ...env };
340
- const oldPath = env.PATH + '';
341
- const npm7 = '/node16/bin-npm7';
342
- const pnpm7 = '/pnpm7/node_modules/.bin';
343
- const pnpm8 = '/pnpm8/node_modules/.bin';
344
- const corepackEnabled = env.ENABLE_EXPERIMENTAL_COREPACK === '1';
345
- if (cliType === 'npm') {
346
- if (typeof lockfileVersion === 'number' &&
347
- lockfileVersion >= 2 &&
348
- (nodeVersion?.major || 0) < 16 &&
349
- !oldPath.includes(npm7) &&
350
- !corepackEnabled) {
351
- // Ensure that npm 7 is at the beginning of the `$PATH`
352
- newEnv.PATH = `${npm7}${path_1.default.delimiter}${oldPath}`;
353
- console.log('Detected `package-lock.json` generated by npm 7+...');
354
- }
369
+ function getEnvForPackageManager({
370
+ cliType,
371
+ lockfileVersion,
372
+ nodeVersion,
373
+ env
374
+ }) {
375
+ const newEnv = { ...env };
376
+ const oldPath = env.PATH + "";
377
+ const npm7 = "/node16/bin-npm7";
378
+ const pnpm7 = "/pnpm7/node_modules/.bin";
379
+ const pnpm8 = "/pnpm8/node_modules/.bin";
380
+ const bun1 = "/bun1";
381
+ const corepackEnabled = env.ENABLE_EXPERIMENTAL_COREPACK === "1";
382
+ if (cliType === "npm") {
383
+ if (typeof lockfileVersion === "number" && lockfileVersion >= 2 && (nodeVersion?.major || 0) < 16 && !oldPath.includes(npm7) && !corepackEnabled) {
384
+ newEnv.PATH = `${npm7}${import_path.default.delimiter}${oldPath}`;
385
+ console.log("Detected `package-lock.json` generated by npm 7+");
355
386
  }
356
- else if (cliType === 'pnpm') {
357
- if (typeof lockfileVersion === 'number' &&
358
- lockfileVersion === 5.4 &&
359
- !oldPath.includes(pnpm7) &&
360
- !corepackEnabled) {
361
- // Ensure that pnpm 7 is at the beginning of the `$PATH`
362
- newEnv.PATH = `${pnpm7}${path_1.default.delimiter}${oldPath}`;
363
- console.log(`Detected \`pnpm-lock.yaml\` version ${lockfileVersion} generated by pnpm 7...`);
364
- }
365
- else if (typeof lockfileVersion === 'number' &&
366
- lockfileVersion >= 6 &&
367
- !oldPath.includes(pnpm8) &&
368
- !corepackEnabled) {
369
- // Ensure that pnpm 8 is at the beginning of the `$PATH`
370
- newEnv.PATH = `${pnpm8}${path_1.default.delimiter}${oldPath}`;
371
- console.log(`Detected \`pnpm-lock.yaml\` version ${lockfileVersion} generated by pnpm 8...`);
372
- }
387
+ } else if (cliType === "pnpm") {
388
+ if (typeof lockfileVersion === "number" && lockfileVersion === 5.4 && !oldPath.includes(pnpm7) && !corepackEnabled) {
389
+ newEnv.PATH = `${pnpm7}${import_path.default.delimiter}${oldPath}`;
390
+ console.log(
391
+ `Detected \`pnpm-lock.yaml\` version ${lockfileVersion} generated by pnpm 7`
392
+ );
393
+ } else if (typeof lockfileVersion === "number" && lockfileVersion >= 6 && !oldPath.includes(pnpm8) && !corepackEnabled) {
394
+ newEnv.PATH = `${pnpm8}${import_path.default.delimiter}${oldPath}`;
395
+ console.log(
396
+ `Detected \`pnpm-lock.yaml\` version ${lockfileVersion} generated by pnpm 8`
397
+ );
373
398
  }
374
- else {
375
- // Yarn v2 PnP mode may be activated, so force "node-modules" linker style
376
- if (!env.YARN_NODE_LINKER) {
377
- newEnv.YARN_NODE_LINKER = 'node-modules';
378
- }
399
+ } else if (cliType === "bun") {
400
+ if (!oldPath.includes(bun1) && !corepackEnabled) {
401
+ newEnv.PATH = `${bun1}${import_path.default.delimiter}${oldPath}`;
402
+ console.log("Detected `bun.lockb` generated by Bun");
403
+ console.warn(
404
+ "Warning: Bun is used as a package manager at build time only, not at runtime with Functions"
405
+ );
406
+ }
407
+ } else {
408
+ if (!env.YARN_NODE_LINKER) {
409
+ newEnv.YARN_NODE_LINKER = "node-modules";
379
410
  }
380
- return newEnv;
411
+ }
412
+ return newEnv;
381
413
  }
382
- exports.getEnvForPackageManager = getEnvForPackageManager;
383
- async function runCustomInstallCommand({ destPath, installCommand, nodeVersion, spawnOpts, }) {
384
- console.log(`Running "install" command: \`${installCommand}\`...`);
385
- const { cliType, lockfileVersion } = await scanParentDirs(destPath);
386
- const env = getEnvForPackageManager({
387
- cliType,
388
- lockfileVersion,
389
- nodeVersion,
390
- env: spawnOpts?.env || {},
391
- });
392
- (0, debug_1.default)(`Running with $PATH:`, env?.PATH || '');
393
- await execCommand(installCommand, {
394
- ...spawnOpts,
395
- env,
396
- cwd: destPath,
397
- });
414
+ async function runCustomInstallCommand({
415
+ destPath,
416
+ installCommand,
417
+ nodeVersion,
418
+ spawnOpts
419
+ }) {
420
+ console.log(`Running "install" command: \`${installCommand}\`...`);
421
+ const { cliType, lockfileVersion } = await scanParentDirs(destPath);
422
+ const env = getEnvForPackageManager({
423
+ cliType,
424
+ lockfileVersion,
425
+ nodeVersion,
426
+ env: spawnOpts?.env || {}
427
+ });
428
+ (0, import_debug.default)(`Running with $PATH:`, env?.PATH || "");
429
+ await execCommand(installCommand, {
430
+ ...spawnOpts,
431
+ env,
432
+ cwd: destPath
433
+ });
398
434
  }
399
- exports.runCustomInstallCommand = runCustomInstallCommand;
400
435
  async function runPackageJsonScript(destPath, scriptNames, spawnOpts) {
401
- (0, assert_1.default)(path_1.default.isAbsolute(destPath));
402
- const { packageJson, cliType, lockfileVersion } = await scanParentDirs(destPath, true);
403
- const scriptName = getScriptName(packageJson, typeof scriptNames === 'string' ? [scriptNames] : scriptNames);
404
- if (!scriptName)
405
- return false;
406
- (0, debug_1.default)('Running user script...');
407
- const runScriptTime = Date.now();
408
- const opts = {
409
- cwd: destPath,
410
- ...spawnOpts,
411
- env: getEnvForPackageManager({
412
- cliType,
413
- lockfileVersion,
414
- nodeVersion: undefined,
415
- env: (0, clone_env_1.cloneEnv)(process.env, spawnOpts?.env),
416
- }),
417
- };
418
- if (cliType === 'npm') {
419
- opts.prettyCommand = `npm run ${scriptName}`;
420
- }
421
- else if (cliType === 'pnpm') {
422
- opts.prettyCommand = `pnpm run ${scriptName}`;
423
- }
424
- else {
425
- opts.prettyCommand = `yarn run ${scriptName}`;
426
- }
427
- console.log(`Running "${opts.prettyCommand}"`);
428
- await spawnAsync(cliType, ['run', scriptName], opts);
429
- (0, debug_1.default)(`Script complete [${Date.now() - runScriptTime}ms]`);
430
- return true;
436
+ (0, import_assert.default)(import_path.default.isAbsolute(destPath));
437
+ const { packageJson, cliType, lockfileVersion } = await scanParentDirs(
438
+ destPath,
439
+ true
440
+ );
441
+ const scriptName = getScriptName(
442
+ packageJson,
443
+ typeof scriptNames === "string" ? [scriptNames] : scriptNames
444
+ );
445
+ if (!scriptName)
446
+ return false;
447
+ (0, import_debug.default)("Running user script...");
448
+ const runScriptTime = Date.now();
449
+ const opts = {
450
+ cwd: destPath,
451
+ ...spawnOpts,
452
+ env: getEnvForPackageManager({
453
+ cliType,
454
+ lockfileVersion,
455
+ nodeVersion: void 0,
456
+ env: (0, import_clone_env.cloneEnv)(process.env, spawnOpts?.env)
457
+ })
458
+ };
459
+ if (cliType === "npm") {
460
+ opts.prettyCommand = `npm run ${scriptName}`;
461
+ } else if (cliType === "pnpm") {
462
+ opts.prettyCommand = `pnpm run ${scriptName}`;
463
+ } else if (cliType === "bun") {
464
+ opts.prettyCommand = `bun run ${scriptName}`;
465
+ } else {
466
+ opts.prettyCommand = `yarn run ${scriptName}`;
467
+ }
468
+ console.log(`Running "${opts.prettyCommand}"`);
469
+ await spawnAsync(cliType, ["run", scriptName], opts);
470
+ (0, import_debug.default)(`Script complete [${Date.now() - runScriptTime}ms]`);
471
+ return true;
431
472
  }
432
- exports.runPackageJsonScript = runPackageJsonScript;
433
473
  async function runBundleInstall(destPath, args = [], spawnOpts, meta) {
434
- if (meta && meta.isDev) {
435
- (0, debug_1.default)('Skipping dependency installation because dev mode is enabled');
436
- return;
437
- }
438
- (0, assert_1.default)(path_1.default.isAbsolute(destPath));
439
- const opts = { ...spawnOpts, cwd: destPath, prettyCommand: 'bundle install' };
440
- await spawnAsync('bundle', args.concat(['install']), opts);
474
+ if (meta && meta.isDev) {
475
+ (0, import_debug.default)("Skipping dependency installation because dev mode is enabled");
476
+ return;
477
+ }
478
+ (0, import_assert.default)(import_path.default.isAbsolute(destPath));
479
+ const opts = { ...spawnOpts, cwd: destPath, prettyCommand: "bundle install" };
480
+ await spawnAsync("bundle", args.concat(["install"]), opts);
441
481
  }
442
- exports.runBundleInstall = runBundleInstall;
443
482
  async function runPipInstall(destPath, args = [], spawnOpts, meta) {
444
- if (meta && meta.isDev) {
445
- (0, debug_1.default)('Skipping dependency installation because dev mode is enabled');
446
- return;
447
- }
448
- (0, assert_1.default)(path_1.default.isAbsolute(destPath));
449
- const opts = { ...spawnOpts, cwd: destPath, prettyCommand: 'pip3 install' };
450
- await spawnAsync('pip3', ['install', '--disable-pip-version-check', ...args], opts);
483
+ if (meta && meta.isDev) {
484
+ (0, import_debug.default)("Skipping dependency installation because dev mode is enabled");
485
+ return;
486
+ }
487
+ (0, import_assert.default)(import_path.default.isAbsolute(destPath));
488
+ const opts = { ...spawnOpts, cwd: destPath, prettyCommand: "pip3 install" };
489
+ await spawnAsync(
490
+ "pip3",
491
+ ["install", "--disable-pip-version-check", ...args],
492
+ opts
493
+ );
451
494
  }
452
- exports.runPipInstall = runPipInstall;
453
495
  function getScriptName(pkg, possibleNames) {
454
- if (pkg?.scripts) {
455
- for (const name of possibleNames) {
456
- if (name in pkg.scripts) {
457
- return name;
458
- }
459
- }
496
+ if (pkg?.scripts) {
497
+ for (const name of possibleNames) {
498
+ if (name in pkg.scripts) {
499
+ return name;
500
+ }
460
501
  }
461
- return null;
502
+ }
503
+ return null;
462
504
  }
463
- exports.getScriptName = getScriptName;
464
- /**
465
- * @deprecate installDependencies() is deprecated.
466
- * Please use runNpmInstall() instead.
467
- */
468
- exports.installDependencies = (0, util_1.deprecate)(runNpmInstall, 'installDependencies() is deprecated. Please use runNpmInstall() instead.');
505
+ const installDependencies = (0, import_util.deprecate)(
506
+ runNpmInstall,
507
+ "installDependencies() is deprecated. Please use runNpmInstall() instead."
508
+ );
509
+ // Annotate the CommonJS export names for ESM import in node:
510
+ 0 && (module.exports = {
511
+ execCommand,
512
+ getEnvForPackageManager,
513
+ getNodeBinPath,
514
+ getNodeBinPaths,
515
+ getNodeVersion,
516
+ getScriptName,
517
+ getSpawnOptions,
518
+ installDependencies,
519
+ runBundleInstall,
520
+ runCustomInstallCommand,
521
+ runNpmInstall,
522
+ runPackageJsonScript,
523
+ runPipInstall,
524
+ runShellScript,
525
+ scanParentDirs,
526
+ spawnAsync,
527
+ spawnCommand,
528
+ traverseUpDirectories,
529
+ walkParentDirs
530
+ });