-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathutils.ts
More file actions
181 lines (156 loc) · 5.19 KB
/
utils.ts
File metadata and controls
181 lines (156 loc) · 5.19 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { existsSync } from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.js";
import { normalizeOptions } from "@opennextjs/aws/build/helper.js";
import { printHeader, showWarningOnWindows } from "@opennextjs/aws/build/utils.js";
import logger from "@opennextjs/aws/logger.js";
import { unstable_readConfig } from "wrangler";
import type yargs from "yargs";
import type { OpenNextConfig } from "../../api/config.js";
import { createOpenNextConfigIfNotExistent, ensureCloudflareConfig } from "../build/utils/index.js";
export type WithWranglerArgs<T = unknown> = T & {
// Array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
wranglerArgs: string[];
wranglerConfigPath: string | undefined;
env: string | undefined;
};
export const nextAppDir = process.cwd();
/**
* Print headers and warnings for the CLI.
*
* @param command
*/
export function printHeaders(command: string) {
printHeader(`Cloudflare ${command}`);
showWarningOnWindows();
}
/**
* Compile the OpenNext config.
*
* When users do not specify a custom config file (using `--openNextConfigPath`),
* the CLI will offer to create one.
*
* When users specify a custom config file but it doesn't exist, we throw an Error.
*
* @param configPath Optional path to the config file. Absolute or relative to cwd.
* @returns OpenNext config.
*/
export async function compileConfig(configPath: string | undefined) {
if (configPath && !existsSync(configPath)) {
throw new Error(`Custom config file not found at ${configPath}`);
}
if (!configPath) {
configPath = await createOpenNextConfigIfNotExistent(nextAppDir);
}
const { config, buildDir } = await compileOpenNextConfig(configPath, { compileEdge: true });
ensureCloudflareConfig(config);
return { config, buildDir };
}
/**
* Retrieve a compiled OpenNext config, and ensure it is for Cloudflare.
*
* @returns OpenNext config.
*/
export async function retrieveCompiledConfig() {
const configPath = path.join(nextAppDir, ".open-next/.build/open-next.config.edge.mjs");
if (!existsSync(configPath)) {
logger.error("Could not find compiled Open Next config, did you run the build command?");
process.exit(1);
}
const config = await import(configPath).then((mod) => mod.default);
ensureCloudflareConfig(config);
return { config };
}
/**
* Normalize the OpenNext options and set the logging level.
*
* @param config
* @param buildDir Directory to use when building the application
* @returns Normalized options.
*/
export function getNormalizedOptions(config: OpenNextConfig, buildDir = nextAppDir) {
const require = createRequire(import.meta.url);
const openNextDistDir = path.dirname(require.resolve("@opennextjs/aws/index.js"));
const options = normalizeOptions(config, openNextDistDir, buildDir);
logger.setLevel(options.debug ? "debug" : "info");
return {
...options,
config,
};
}
export type BuildOptions = ReturnType<typeof getNormalizedOptions>;
/**
* Read the Wrangler config.
*
* @param args Wrangler environment and config path.
* @returns Wrangler config.
*/
export function readWranglerConfig(args: WithWranglerArgs) {
return unstable_readConfig({ env: args.env, config: args.wranglerConfigPath });
}
/**
* Adds flags for the wrangler config path and environment to the yargs configuration.
*/
export function withWranglerOptions<T extends yargs.Argv>(args: T) {
return args
.option("config", {
type: "string",
alias: "c",
desc: "Path to Wrangler configuration file",
})
.option("configPath", {
type: "string",
desc: "Path to Wrangler configuration file",
deprecated: true,
})
.option("env", {
type: "string",
alias: "e",
desc: "Wrangler environment to use for operations",
});
}
type WranglerInputArgs = {
configPath: string | undefined;
config: string | undefined;
env: string | undefined;
remote?: boolean | undefined;
};
/**
*
* @param args
* @returns An array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
*/
function getWranglerArgs(args: WranglerInputArgs & { _: (string | number)[] }): string[] {
if (args.configPath) {
logger.warn("The `--configPath` flag is deprecated, please use `--config` instead.");
if (args.config) {
logger.error(
"Multiple config flags found. Please use the `--config` flag for your Wrangler config path."
);
process.exit(1);
}
}
return [
...(args.configPath ? ["--config", args.configPath] : []),
...(args.config ? ["--config", args.config] : []),
...(args.env ? ["--env", args.env] : []),
...(args.remote ? ["--remote"] : []),
// Note: the first args in `_` will be the commands.
...args._.slice(args._[0] === "populateCache" ? 2 : 1).map((a) => `${a}`),
];
}
/**
*
* @param args
* @returns The inputted args, and an array of arguments that can be given to wrangler commands, including the `--config` and `--env` args.
*/
export function withWranglerPassthroughArgs<T extends yargs.ArgumentsCamelCase<WranglerInputArgs>>(
args: T
): WithWranglerArgs<T> {
return {
...args,
wranglerConfigPath: args.config ?? args.configPath,
wranglerArgs: getWranglerArgs(args),
};
}