Skip to content

Commit 6c94a4a

Browse files
Apply various bugfixes/improvements to the migrate command (#1122)
1 parent fea645f commit 6c94a4a

File tree

7 files changed

+842
-952
lines changed

7 files changed

+842
-952
lines changed

.changeset/puny-coins-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
In `migrate` command, avoid adding unnecessary newlines when creating files

.changeset/salty-spoons-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix `migrate` command incorrectly erroring if the target application doesn't have a `public` directory

.changeset/upset-squids-deny.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
Bump `@opennextjs/aws` to 3.9.16
6+
7+
- Fix `migrate` command not updating Next.js config files
8+
- Fixes [#1062](https://github.com/opennextjs/opennextjs-cloudflare/issues/1062), [#1115](https://github.com/opennextjs/opennextjs-cloudflare/issues/1115)
9+
10+
See details at <https://github.com/opennextjs/opennextjs-aws/releases/tag/v3.9.16>

packages/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"dependencies": {
5555
"@ast-grep/napi": "^0.40.5",
5656
"@dotenvx/dotenvx": "catalog:",
57-
"@opennextjs/aws": "3.9.15",
57+
"@opennextjs/aws": "3.9.16",
5858
"cloudflare": "^4.4.1",
5959
"enquirer": "^2.4.1",
6060
"glob": "catalog:",
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "node:fs";
2+
import path from "node:path";
23

34
/**
45
* Appends text to a file
@@ -8,16 +9,29 @@ import fs from "node:fs";
89
*
910
* @param filepath The path to the file.
1011
* @param text The text to append to the file.
11-
* @param condition 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.
12+
* @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.
13+
* @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.
1214
*/
1315
export function conditionalAppendFileSync(
1416
filepath: string,
1517
text: string,
16-
condition: (fileContent: string) => boolean
18+
{
19+
appendIf = () => true,
20+
appendPrefix = "",
21+
}: {
22+
appendIf?: (fileContent: string) => boolean;
23+
appendPrefix?: string;
24+
} = {}
1725
): void {
1826
const fileExists = fs.existsSync(filepath);
27+
const maybeFileContent = fileExists ? fs.readFileSync(filepath, "utf8") : "";
1928

20-
if (!fileExists || condition(fs.readFileSync(filepath, "utf8"))) {
21-
fs.appendFileSync(filepath, text);
29+
if (!fileExists) {
30+
const dir = path.dirname(filepath);
31+
fs.mkdirSync(dir, { recursive: true });
32+
}
33+
34+
if (!fileExists || appendIf(maybeFileContent)) {
35+
fs.appendFileSync(filepath, `${maybeFileContent.length > 0 ? appendPrefix : ""}${text}`);
2236
}
2337
}

packages/cloudflare/src/cli/commands/migrate.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,22 @@ async function migrateCommand(args: { forceInstall: boolean }): Promise<void> {
7070

7171
const devVarsExists = fs.existsSync(".dev.vars");
7272
printStepTitle(`${devVarsExists ? "Updating" : "Creating"} .dev.vars file`);
73-
conditionalAppendFileSync(
74-
".dev.vars",
75-
"\nNEXTJS_ENV=development\n",
76-
(content) => !/\bNEXTJS_ENV\b/.test(content)
77-
);
73+
conditionalAppendFileSync(".dev.vars", "NEXTJS_ENV=development\n", {
74+
appendIf: (content) => !/\bNEXTJS_ENV\b/.test(content),
75+
appendPrefix: "\n",
76+
});
7877

7978
printStepTitle(`${fs.existsSync("public/_headers") ? "Updating" : "Creating"} public/_headers file`);
8079
conditionalAppendFileSync(
8180
"public/_headers",
82-
"\n\n# https://developers.cloudflare.com/workers/static-assets/headers\n" +
81+
"# https://developers.cloudflare.com/workers/static-assets/headers\n" +
8382
"# https://opennext.js.org/cloudflare/caching#static-assets-caching\n" +
8483
"/_next/static/*\n" +
85-
" Cache-Control: public,max-age=31536000,immutable\n\n",
86-
(content) => !/^\/_next\/static\/*\b/.test(content)
84+
" Cache-Control: public,max-age=31536000,immutable\n",
85+
{
86+
appendIf: (content) => !/^\/_next\/static\/*\b/.test(content),
87+
appendPrefix: "\n\n",
88+
}
8789
);
8890

8991
printStepTitle("Updating package.json scripts");
@@ -121,18 +123,24 @@ async function migrateCommand(args: { forceInstall: boolean }): Promise<void> {
121123

122124
const gitIgnoreExists = fs.existsSync(".gitignore");
123125
printStepTitle(`${gitIgnoreExists ? "Updating" : "Creating"} .gitignore file`);
124-
conditionalAppendFileSync(
125-
".gitignore",
126-
"\n# OpenNext\n.open-next\n",
127-
(content) => !content.includes(".open-next")
128-
);
126+
conditionalAppendFileSync(".gitignore", "# OpenNext\n.open-next\n", {
127+
appendIf: (content) => !content.includes(".open-next"),
128+
appendPrefix: "\n",
129+
});
129130

130-
printStepTitle("Updating Next.js config");
131-
conditionalAppendFileSync(
132-
findNextConfig({ appPath: projectDir })!,
133-
"\nimport('@opennextjs/cloudflare').then(m => m.initOpenNextCloudflareForDev());\n",
134-
(content) => !content.includes("initOpenNextCloudflareForDev")
135-
);
131+
const nextConfig = findNextConfig({ appPath: projectDir });
132+
133+
if (nextConfig) {
134+
printStepTitle("Updating Next.js config");
135+
conditionalAppendFileSync(
136+
nextConfig.path,
137+
"import('@opennextjs/cloudflare').then(m => m.initOpenNextCloudflareForDev());\n",
138+
{
139+
appendIf: (content) => !content.includes("initOpenNextCloudflareForDev"),
140+
appendPrefix: "\n",
141+
}
142+
);
143+
}
136144

137145
printStepTitle("Checking for edge runtime usage");
138146
try {

0 commit comments

Comments
 (0)