-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathrun-wrangler.ts
More file actions
108 lines (93 loc) · 3.09 KB
/
run-wrangler.ts
File metadata and controls
108 lines (93 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { spawnSync } from "node:child_process";
import { readFileSync } from "node:fs";
import path from "node:path";
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
import { compareSemver } from "@opennextjs/aws/build/helper.js";
import logger from "@opennextjs/aws/logger.js";
export type WranglerTarget = "local" | "remote";
type WranglerOptions = {
target?: WranglerTarget;
environment?: string;
excludeRemoteFlag?: boolean;
logging?: "all" | "error";
};
/**
* Checks the package.json `packageManager` field to determine whether yarn modern is used.
*
* @param options Build options.
* @returns Whether yarn modern is used.
*/
function isYarnModern(options: BuildOptions) {
const packageJson: { packageManager?: string } = JSON.parse(
readFileSync(path.join(options.monorepoRoot, "package.json"), "utf-8")
);
if (!packageJson.packageManager?.startsWith("yarn")) return false;
return version ? compareSemver(version, ">=", "4.0.0") : false;
}
/**
* Prepends CLI flags with `--` so that certain package managers can pass args through to wrangler
* properly.
*
* npm and yarn classic require `--` to be used, while pnpm and bun require that it is not used.
*
* @param options Build options.
* @param args CLI args.
* @returns Arguments with a passthrough flag injected when needed.
*/
function injectPassthroughFlagForArgs(options: BuildOptions, args: string[]) {
if (options.packager !== "npm" && (options.packager !== "yarn" || isYarnModern(options))) {
return args;
}
const flagInArgsIndex = args.findIndex((v) => v.startsWith("--"));
if (flagInArgsIndex !== -1) {
args.splice(flagInArgsIndex, 0, "--");
}
return args;
}
export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: WranglerOptions = {}) {
const result = spawnSync(
options.packager,
[
options.packager === "bun" ? "x" : "exec",
"wrangler",
...injectPassthroughFlagForArgs(
options,
[
...args,
wranglerOpts.environment && `--env ${wranglerOpts.environment}`,
wranglerOpts.target === "remote" && !wranglerOpts.excludeRemoteFlag && "--remote",
wranglerOpts.target === "local" && "--local",
].filter((v): v is string => !!v)
),
],
{
shell: true,
stdio: wranglerOpts.logging === "error" ? ["ignore", "ignore", "inherit"] : "inherit",
}
);
if (result.status !== 0) {
logger.error("Wrangler command failed");
process.exit(1);
}
}
export function isWranglerTarget(v: string | undefined): v is WranglerTarget {
return !!v && ["local", "remote"].includes(v);
}
/**
* Find the value of the environment flag (`--env` / `-e`) used by Wrangler.
*
* @param args - CLI arguments.
* @returns Value of the environment flag.
*/
export function getWranglerEnvironmentFlag(args: string[]) {
for (let i = 0; i <= args.length; i++) {
const arg = args[i];
if (!arg) continue;
if (arg === "--env" || arg === "-e") {
return args[i + 1];
}
if (arg.startsWith("--env=") || arg.startsWith("-e=")) {
return arg.split("=")[1];
}
}
}