Skip to content

Commit 7d16e4e

Browse files
committed
refactor: make callbacks consistent
1 parent 9cfe371 commit 7d16e4e

File tree

3 files changed

+62
-58
lines changed

3 files changed

+62
-58
lines changed

packages/open-next/src/build/createAssets.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
7474
const htmlPages = buildHelper.getHtmlPages(dotNextPath);
7575
buildHelper.removeFiles(
7676
outputPath,
77-
(file) =>
78-
file.endsWith(".js") ||
79-
file.endsWith(".js.nft.json") ||
80-
(file.endsWith(".html") && htmlPages.has(file)),
77+
({ relativePath }) =>
78+
relativePath.endsWith(".js") ||
79+
relativePath.endsWith(".js.nft.json") ||
80+
(relativePath.endsWith(".html") && htmlPages.has(relativePath)),
8181
);
8282

8383
// Merge cache files into a single file
@@ -95,21 +95,21 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
9595
buildHelper.traverseFiles(
9696
outputPath,
9797
() => true,
98-
(filepath) => {
99-
const ext = path.extname(filepath);
98+
({ absolutePath }) => {
99+
const ext = path.extname(absolutePath);
100100
switch (ext) {
101101
case ".meta":
102102
case ".html":
103103
case ".json":
104104
case ".body":
105105
case ".rsc":
106106
const newFilePath =
107-
filepath
108-
.substring(0, filepath.length - ext.length)
107+
absolutePath
108+
.substring(0, absolutePath.length - ext.length)
109109
.replace(/\.prefetch$/, "") + ".cache";
110110

111111
cacheFilesPath[newFilePath] = {
112-
[ext.slice(1)]: filepath,
112+
[ext.slice(1)]: absolutePath,
113113
...cacheFilesPath[newFilePath],
114114
};
115115
break;
@@ -161,9 +161,9 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
161161
// Traverse files inside cache to find all meta files and cache tags associated with them
162162
buildHelper.traverseFiles(
163163
outputPath,
164-
(file) => file.endsWith(".meta"),
165-
(filePath) => {
166-
const fileContent = fs.readFileSync(filePath, "utf8");
164+
({ absolutePath }) => absolutePath.endsWith(".meta"),
165+
({ absolutePath, relativePath }) => {
166+
const fileContent = fs.readFileSync(absolutePath, "utf8");
167167
const fileData = JSON.parse(fileContent);
168168
if (fileData.headers?.["x-next-cache-tags"]) {
169169
fileData.headers["x-next-cache-tags"]
@@ -175,7 +175,7 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
175175
path: {
176176
S: path.posix.join(
177177
buildId,
178-
path.relative(outputPath, filePath).replace(".meta", ""),
178+
relativePath.replace(".meta", ""),
179179
),
180180
},
181181
// We don't care about the revalidation time here, we just need to make sure it's there
@@ -199,17 +199,14 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
199199
buildHelper.traverseFiles(
200200
fetchCachePath,
201201
() => true,
202-
(filepath) => {
203-
const fileContent = fs.readFileSync(filepath, "utf8");
202+
({ absolutePath, relativePath }) => {
203+
const fileContent = fs.readFileSync(absolutePath, "utf8");
204204
const fileData = JSON.parse(fileContent);
205205
fileData?.tags?.forEach((tag: string) => {
206206
metaFiles.push({
207207
tag: { S: path.posix.join(buildId, tag) },
208208
path: {
209-
S: path.posix.join(
210-
buildId,
211-
path.relative(fetchCachePath, filepath),
212-
),
209+
S: path.posix.join(buildId, relativePath),
213210
},
214211
revalidatedAt: { N: "1" },
215212
});
@@ -235,7 +232,10 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
235232
}
236233

237234
// We need to remove files later because we need the metafiles for dynamodb tags cache
238-
buildHelper.removeFiles(outputPath, (file) => !file.endsWith(".cache"));
235+
buildHelper.removeFiles(
236+
outputPath,
237+
({ relativePath }) => !relativePath.endsWith(".cache"),
238+
);
239239

240240
return { useTagCache };
241241
}

packages/open-next/src/build/createServerBundle.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,14 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
6565
const appPath = path.join(serverPath, "app");
6666
buildHelper.traverseFiles(
6767
appPath,
68-
(file) => {
69-
if (file.endsWith("page.js") || file.endsWith("route.js")) {
70-
const route = `app/${file.replace(/\.js$/, "")}`;
71-
if (!foundRoutes.has(route)) {
72-
remainingRoutes.add(route);
73-
}
68+
({ relativePath }) =>
69+
relativePath.endsWith("page.js") || relativePath.endsWith("route.js"),
70+
({ relativePath }) => {
71+
const route = `app/${relativePath.replace(/\.js$/, "")}`;
72+
if (!foundRoutes.has(route)) {
73+
remainingRoutes.add(route);
7474
}
75-
return false;
7675
},
77-
() => {},
7876
);
7977
}
8078

@@ -83,16 +81,13 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
8381
const pagePath = path.join(serverPath, "pages");
8482
buildHelper.traverseFiles(
8583
pagePath,
86-
(file) => {
87-
if (file.endsWith(".js")) {
88-
const route = `pages/${file.replace(/\.js$/, "")}`;
89-
if (!foundRoutes.has(route)) {
90-
remainingRoutes.add(route);
91-
}
84+
({ relativePath }) => relativePath.endsWith(".js"),
85+
({ relativePath }) => {
86+
const route = `pages/${relativePath.replace(/\.js$/, "")}`;
87+
if (!foundRoutes.has(route)) {
88+
remainingRoutes.add(route);
9289
}
93-
return false;
9490
},
95-
() => {},
9691
);
9792
}
9893

packages/open-next/src/build/helper.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,47 +167,56 @@ export async function esbuildAsync(
167167
}
168168
}
169169

170+
/**
171+
* Recursively delete files.
172+
*
173+
* @see `traverseFiles`.
174+
*  
175+
* @param root Root directory to search.
176+
* @param conditionFn Predicate used to delete the files.
177+
*/
170178
export function removeFiles(
171179
root: string,
172-
conditionFn: (file: string) => boolean,
173-
searchingDir: string = "",
180+
conditionFn: (paths: {
181+
absolutePath: string;
182+
relativePath: string;
183+
}) => boolean,
174184
) {
175-
traverseFiles(
176-
root,
177-
conditionFn,
178-
(filePath) => fs.rmSync(filePath, { force: true }),
179-
searchingDir,
185+
traverseFiles(root, conditionFn, ({ absolutePath }) =>
186+
fs.rmSync(absolutePath, { force: true }),
180187
);
181188
}
182189

183190
/**
184191
* Recursively traverse files in a directory and call `callbackFn` when `conditionFn` returns true
192+
*
193+
* The callbacks are passed both the absolute and relative (to root) path to files.
194+
*
185195
* @param root - Root directory to search
186-
* @param conditionFn - Called to determine if `callbackFn` should be called
187-
* @param callbackFn - Called when `conditionFn` returns true
196+
* @param conditionFn - Called to determine if `callbackFn` should be called.
197+
* @param callbackFn - Called when `conditionFn` returns true.
188198
* @param searchingDir - Directory to search (used for recursion)
189199
*/
190200
export function traverseFiles(
191201
root: string,
192-
conditionFn: (file: string) => boolean,
193-
callbackFn: (filePath: string) => void,
202+
conditionFn: (paths: {
203+
absolutePath: string;
204+
relativePath: string;
205+
}) => boolean,
206+
callbackFn: (paths: { absolutePath: string; relativePath: string }) => void,
194207
searchingDir: string = "",
195208
) {
196209
fs.readdirSync(path.join(root, searchingDir)).forEach((file) => {
197-
const filePath = path.join(root, searchingDir, file);
198-
199-
if (fs.statSync(filePath).isDirectory()) {
200-
traverseFiles(
201-
root,
202-
conditionFn,
203-
callbackFn,
204-
path.join(searchingDir, file),
205-
);
210+
const relativePath = path.join(searchingDir, file);
211+
const absolutePath = path.join(root, relativePath);
212+
213+
if (fs.statSync(absolutePath).isDirectory()) {
214+
traverseFiles(root, conditionFn, callbackFn, relativePath);
206215
return;
207216
}
208217

209-
if (conditionFn(path.join(searchingDir, file))) {
210-
callbackFn(filePath);
218+
if (conditionFn({ absolutePath, relativePath })) {
219+
callbackFn({ absolutePath, relativePath });
211220
}
212221
});
213222
}

0 commit comments

Comments
 (0)