Skip to content

Commit 6e10327

Browse files
update createOrAppendToFile to conditionalAppendFileSync
1 parent fbda9f7 commit 6e10327

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import fs from "node:fs";
22

33
/**
4-
* Creates a file with the given text, or appends to it if it already exists and the condition is met.
4+
* Runs fs' `appendFileSync` on a target file, but only a specified condition is met in regards to the file's content.
55
*
6-
* @param filepath The path to the file to create or append to.
7-
* @param text The text to write or append to the file.
8-
* @param condition A function that receives the current file content and returns `true` if the text should be appended.
6+
* @param filepath The path to the file.
7+
* @param text The text to append to the file.
8+
* @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.
99
*/
10-
export function createOrAppendToFile(
10+
export function conditionalAppendFileSync(
1111
filepath: string,
1212
text: string,
1313
condition: (fileContent: string) => boolean
1414
): void {
1515
const fileExists = fs.existsSync(filepath);
1616

17-
if (!fileExists) {
18-
fs.writeFileSync(filepath, text);
19-
return;
20-
}
17+
const fileContent = fileExists ? fs.readFileSync(filepath, "utf8") : undefined;
2118

22-
const fileContent = fs.readFileSync(filepath, "utf8");
23-
if (condition(fileContent)) {
19+
if (fileContent !== undefined && condition(fileContent)) {
2420
fs.appendFileSync(filepath, `\n${text}\n`);
2521
}
2622
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { findPackagerAndRoot } from "@opennextjs/aws/build/helper.js";
66
import logger from "@opennextjs/aws/logger.js";
77
import type yargs from "yargs";
88

9-
import { createOrAppendToFile } from "../build/utils/files.js";
9+
import { conditionalAppendFileSync } from "../build/utils/files.js";
1010
import { getNextConfigPath } from "../utils/next-config.js";
1111
import { createOpenNextConfigFile, getOpenNextConfigPath } from "../utils/open-next-config.js";
1212
import { createWranglerConfigFile, getWranglerConfigPath } from "../utils/wrangler-config.js";
@@ -71,15 +71,14 @@ async function migrateCommand(): Promise<void> {
7171

7272
const devVarsExists = fs.existsSync(".dev.vars");
7373
printStepTitle(`${devVarsExists ? "Updating" : "Creating"} .dev.vars file`);
74-
createOrAppendToFile(
74+
conditionalAppendFileSync(
7575
".dev.vars",
7676
"NEXTJS_ENV=development",
7777
(gitIgnoreContent) => !gitIgnoreContent.includes("NEXTJS_ENV=")
7878
);
7979

80-
8180
printStepTitle(`${fs.existsSync("public/_headers") ? "Updating" : "Creating"} public/_headers file`);
82-
createOrAppendToFile(
81+
conditionalAppendFileSync(
8382
"public/_headers",
8483
[
8584
"",
@@ -127,14 +126,14 @@ async function migrateCommand(): Promise<void> {
127126

128127
const gitIgnoreExists = fs.existsSync(".gitignore");
129128
printStepTitle(`${gitIgnoreExists ? "Updating" : "Creating"} .gitignore file`);
130-
createOrAppendToFile(
129+
conditionalAppendFileSync(
131130
".gitignore",
132131
"# OpenNext\n.open-next",
133132
(gitIgnoreContent) => !gitIgnoreContent.includes(".open-next")
134133
);
135134

136135
printStepTitle("Updating Next.js config");
137-
createOrAppendToFile(
136+
conditionalAppendFileSync(
138137
nextConfigFilePath,
139138
"\nimport('@opennextjs/cloudflare').then(m => m.initOpenNextCloudflareForDev());",
140139
(nextConfigContent) => !nextConfigContent.includes("initOpenNextCloudflareForDev")

0 commit comments

Comments
 (0)