@opennextjs/cloudflare 1.16.3 → 1.16.4
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.
- package/dist/cli/build/utils/files.d.ts +6 -2
- package/dist/cli/build/utils/files.js +11 -4
- package/dist/cli/commands/build.js +1 -1
- package/dist/cli/commands/deploy.js +1 -1
- package/dist/cli/commands/migrate.js +21 -6
- package/dist/cli/commands/populate-cache.js +2 -2
- package/dist/cli/commands/preview.js +1 -1
- package/dist/cli/commands/upload.js +1 -1
- package/dist/cli/commands/utils.js +2 -2
- package/dist/cli/index.js +18 -1
- package/package.json +2 -2
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @param filepath The path to the file.
|
|
8
8
|
* @param text The text to append to the file.
|
|
9
|
-
* @param
|
|
9
|
+
* @param opts.appendIf A function that receives the current file content and returns `true` if the text should be appended to it, the condition is skipped when the file is being created. Defaults to a function that always returns true.
|
|
10
|
+
* @param opts.appendPrefix A string that will be inserted between the pre-existing file's content and the new text in case the file already existed. Defaults to an empty string.
|
|
10
11
|
*/
|
|
11
|
-
export declare function conditionalAppendFileSync(filepath: string, text: string,
|
|
12
|
+
export declare function conditionalAppendFileSync(filepath: string, text: string, { appendIf, appendPrefix, }?: {
|
|
13
|
+
appendIf?: (fileContent: string) => boolean;
|
|
14
|
+
appendPrefix?: string;
|
|
15
|
+
}): void;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
2
3
|
/**
|
|
3
4
|
* Appends text to a file
|
|
4
5
|
*
|
|
@@ -7,11 +8,17 @@ import fs from "node:fs";
|
|
|
7
8
|
*
|
|
8
9
|
* @param filepath The path to the file.
|
|
9
10
|
* @param text The text to append to the file.
|
|
10
|
-
* @param
|
|
11
|
+
* @param opts.appendIf A function that receives the current file content and returns `true` if the text should be appended to it, the condition is skipped when the file is being created. Defaults to a function that always returns true.
|
|
12
|
+
* @param opts.appendPrefix A string that will be inserted between the pre-existing file's content and the new text in case the file already existed. Defaults to an empty string.
|
|
11
13
|
*/
|
|
12
|
-
export function conditionalAppendFileSync(filepath, text,
|
|
14
|
+
export function conditionalAppendFileSync(filepath, text, { appendIf = () => true, appendPrefix = "", } = {}) {
|
|
13
15
|
const fileExists = fs.existsSync(filepath);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
const maybeFileContent = fileExists ? fs.readFileSync(filepath, "utf8") : "";
|
|
17
|
+
if (!fileExists) {
|
|
18
|
+
const dir = path.dirname(filepath);
|
|
19
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
20
|
+
}
|
|
21
|
+
if (!fileExists || appendIf(maybeFileContent)) {
|
|
22
|
+
fs.appendFileSync(filepath, `${maybeFileContent.length > 0 ? appendPrefix : ""}${text}`);
|
|
16
23
|
}
|
|
17
24
|
}
|
|
@@ -26,7 +26,7 @@ async function buildCommand(args) {
|
|
|
26
26
|
* Consumes 1 positional parameter.
|
|
27
27
|
*/
|
|
28
28
|
export function addBuildCommand(y) {
|
|
29
|
-
return y.command("build", "Build an OpenNext Cloudflare worker", (c) => withWranglerOptions(c)
|
|
29
|
+
return y.command("build [args..]", "Build an OpenNext Cloudflare worker", (c) => withWranglerOptions(c)
|
|
30
30
|
.option("skipNextBuild", {
|
|
31
31
|
type: "boolean",
|
|
32
32
|
alias: ["skipBuild", "s"],
|
|
@@ -46,5 +46,5 @@ export async function deployCommand(args) {
|
|
|
46
46
|
* Consumes 1 positional parameter.
|
|
47
47
|
*/
|
|
48
48
|
export function addDeployCommand(y) {
|
|
49
|
-
return y.command("deploy", "Deploy a built OpenNext app to Cloudflare Workers", (c) => withPopulateCacheOptions(c), (args) => deployCommand(withWranglerPassthroughArgs(args)));
|
|
49
|
+
return y.command("deploy [args..]", "Deploy a built OpenNext app to Cloudflare Workers", (c) => withPopulateCacheOptions(c), (args) => deployCommand(withWranglerPassthroughArgs(args)));
|
|
50
50
|
}
|
|
@@ -49,12 +49,18 @@ async function migrateCommand(args) {
|
|
|
49
49
|
await createOpenNextConfigFile("./");
|
|
50
50
|
const devVarsExists = fs.existsSync(".dev.vars");
|
|
51
51
|
printStepTitle(`${devVarsExists ? "Updating" : "Creating"} .dev.vars file`);
|
|
52
|
-
conditionalAppendFileSync(".dev.vars", "
|
|
52
|
+
conditionalAppendFileSync(".dev.vars", "NEXTJS_ENV=development\n", {
|
|
53
|
+
appendIf: (content) => !/\bNEXTJS_ENV\b/.test(content),
|
|
54
|
+
appendPrefix: "\n",
|
|
55
|
+
});
|
|
53
56
|
printStepTitle(`${fs.existsSync("public/_headers") ? "Updating" : "Creating"} public/_headers file`);
|
|
54
|
-
conditionalAppendFileSync("public/_headers", "
|
|
57
|
+
conditionalAppendFileSync("public/_headers", "# https://developers.cloudflare.com/workers/static-assets/headers\n" +
|
|
55
58
|
"# https://opennext.js.org/cloudflare/caching#static-assets-caching\n" +
|
|
56
59
|
"/_next/static/*\n" +
|
|
57
|
-
" Cache-Control: public,max-age=31536000,immutable\n
|
|
60
|
+
" Cache-Control: public,max-age=31536000,immutable\n", {
|
|
61
|
+
appendIf: (content) => !/^\/_next\/static\/*\b/.test(content),
|
|
62
|
+
appendPrefix: "\n\n",
|
|
63
|
+
});
|
|
58
64
|
printStepTitle("Updating package.json scripts");
|
|
59
65
|
const openNextScripts = {
|
|
60
66
|
preview: "opennextjs-cloudflare build && opennextjs-cloudflare preview",
|
|
@@ -82,9 +88,18 @@ async function migrateCommand(args) {
|
|
|
82
88
|
}
|
|
83
89
|
const gitIgnoreExists = fs.existsSync(".gitignore");
|
|
84
90
|
printStepTitle(`${gitIgnoreExists ? "Updating" : "Creating"} .gitignore file`);
|
|
85
|
-
conditionalAppendFileSync(".gitignore", "
|
|
86
|
-
|
|
87
|
-
|
|
91
|
+
conditionalAppendFileSync(".gitignore", "# OpenNext\n.open-next\n", {
|
|
92
|
+
appendIf: (content) => !content.includes(".open-next"),
|
|
93
|
+
appendPrefix: "\n",
|
|
94
|
+
});
|
|
95
|
+
const nextConfig = findNextConfig({ appPath: projectDir });
|
|
96
|
+
if (nextConfig) {
|
|
97
|
+
printStepTitle("Updating Next.js config");
|
|
98
|
+
conditionalAppendFileSync(nextConfig.path, "import('@opennextjs/cloudflare').then(m => m.initOpenNextCloudflareForDev());\n", {
|
|
99
|
+
appendIf: (content) => !content.includes("initOpenNextCloudflareForDev"),
|
|
100
|
+
appendPrefix: "\n",
|
|
101
|
+
});
|
|
102
|
+
}
|
|
88
103
|
printStepTitle("Checking for edge runtime usage");
|
|
89
104
|
try {
|
|
90
105
|
const extensions = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".mts"];
|
|
@@ -219,8 +219,8 @@ function populateStaticAssetsIncrementalCache(options) {
|
|
|
219
219
|
*/
|
|
220
220
|
export function addPopulateCacheCommand(y) {
|
|
221
221
|
return y.command("populateCache", "Populate the cache for a built Next.js app", (c) => c
|
|
222
|
-
.command("local", "Local dev server cache", (c) => withPopulateCacheOptions(c), (args) => populateCacheCommand("local", withWranglerPassthroughArgs(args)))
|
|
223
|
-
.command("remote", "Remote Cloudflare Worker cache", (c) => withPopulateCacheOptions(c), (args) => populateCacheCommand("remote", withWranglerPassthroughArgs(args)))
|
|
222
|
+
.command("local [args..]", "Local dev server cache", (c) => withPopulateCacheOptions(c), (args) => populateCacheCommand("local", withWranglerPassthroughArgs(args)))
|
|
223
|
+
.command("remote [args..]", "Remote Cloudflare Worker cache", (c) => withPopulateCacheOptions(c), (args) => populateCacheCommand("remote", withWranglerPassthroughArgs(args)))
|
|
224
224
|
.demandCommand(1, 1));
|
|
225
225
|
}
|
|
226
226
|
export function withPopulateCacheOptions(args) {
|
|
@@ -28,7 +28,7 @@ export async function previewCommand(args) {
|
|
|
28
28
|
* Consumes 1 positional parameter.
|
|
29
29
|
*/
|
|
30
30
|
export function addPreviewCommand(y) {
|
|
31
|
-
return y.command("preview", "Preview a built OpenNext app with a Wrangler dev server", (c) => withPopulateCacheOptions(c).option("remote", {
|
|
31
|
+
return y.command("preview [args..]", "Preview a built OpenNext app with a Wrangler dev server", (c) => withPopulateCacheOptions(c).option("remote", {
|
|
32
32
|
type: "boolean",
|
|
33
33
|
alias: "r",
|
|
34
34
|
default: false,
|
|
@@ -40,5 +40,5 @@ export async function uploadCommand(args) {
|
|
|
40
40
|
* Consumes 1 positional parameter.
|
|
41
41
|
*/
|
|
42
42
|
export function addUploadCommand(y) {
|
|
43
|
-
return y.command("upload", "Upload a built OpenNext app to Cloudflare Workers", (c) => withPopulateCacheOptions(c), (args) => uploadCommand(withWranglerPassthroughArgs(args)));
|
|
43
|
+
return y.command("upload [args..]", "Upload a built OpenNext app to Cloudflare Workers", (c) => withPopulateCacheOptions(c), (args) => uploadCommand(withWranglerPassthroughArgs(args)));
|
|
44
44
|
}
|
|
@@ -120,8 +120,8 @@ function getWranglerArgs(args) {
|
|
|
120
120
|
...(args.config ? ["--config", args.config] : []),
|
|
121
121
|
...(args.env ? ["--env", args.env] : []),
|
|
122
122
|
...(args.remote ? ["--remote"] : []),
|
|
123
|
-
// Note: the
|
|
124
|
-
...args.
|
|
123
|
+
// Note: the `args` array contains unrecognised flags.
|
|
124
|
+
...(args.args?.map((a) => `${a}`) ?? []),
|
|
125
125
|
];
|
|
126
126
|
}
|
|
127
127
|
/**
|
package/dist/cli/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import logger from "@opennextjs/aws/logger.js";
|
|
2
3
|
import yargs from "yargs";
|
|
4
|
+
import { getVersion } from "./build/utils/version.js";
|
|
3
5
|
import { addBuildCommand } from "./commands/build.js";
|
|
4
6
|
import { addDeployCommand } from "./commands/deploy.js";
|
|
5
7
|
import { addMigrateCommand } from "./commands/migrate.js";
|
|
@@ -9,7 +11,22 @@ import { addUploadCommand } from "./commands/upload.js";
|
|
|
9
11
|
export function runCommand() {
|
|
10
12
|
const y = yargs(process.argv.slice(2).filter((arg) => arg !== "--"))
|
|
11
13
|
.scriptName("opennextjs-cloudflare")
|
|
12
|
-
.parserConfiguration({ "unknown-options-as-args": true })
|
|
14
|
+
.parserConfiguration({ "unknown-options-as-args": true })
|
|
15
|
+
.strictCommands()
|
|
16
|
+
.help()
|
|
17
|
+
.alias("h", "help")
|
|
18
|
+
.version(getVersion().cloudflare)
|
|
19
|
+
.alias("v", "version")
|
|
20
|
+
.fail((msg, err, yargs) => {
|
|
21
|
+
if (msg) {
|
|
22
|
+
logger.error(`${msg}\n`);
|
|
23
|
+
}
|
|
24
|
+
if (err) {
|
|
25
|
+
throw err;
|
|
26
|
+
}
|
|
27
|
+
yargs.showHelp();
|
|
28
|
+
process.exit(1);
|
|
29
|
+
});
|
|
13
30
|
addBuildCommand(y);
|
|
14
31
|
addPreviewCommand(y);
|
|
15
32
|
addDeployCommand(y);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opennextjs/cloudflare",
|
|
3
3
|
"description": "Cloudflare builder for next apps",
|
|
4
|
-
"version": "1.16.
|
|
4
|
+
"version": "1.16.4",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"opennextjs-cloudflare": "dist/cli/index.js"
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@ast-grep/napi": "^0.40.5",
|
|
46
46
|
"@dotenvx/dotenvx": "1.31.0",
|
|
47
|
-
"@opennextjs/aws": "3.9.
|
|
47
|
+
"@opennextjs/aws": "3.9.16",
|
|
48
48
|
"cloudflare": "^4.4.1",
|
|
49
49
|
"enquirer": "^2.4.1",
|
|
50
50
|
"glob": "^12.0.0",
|