Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e53019

Browse files
authored
refactor: make traverseFiles callbacks consistent (#623)
1 parent 9cfe371 commit 6e53019

File tree

3 files changed

+71
-65
lines changed

3 files changed

+71
-65
lines changed

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

Lines changed: 22 additions & 22 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":
106-
const newFilePath =
107-
filepath
108-
.substring(0, filepath.length - ext.length)
109-
.replace(/\.prefetch$/, "") + ".cache";
106+
const newFilePath = absolutePath
107+
.substring(0, absolutePath.length - ext.length)
108+
.replace(/\.prefetch$/, "")
109+
.concat(".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: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -167,51 +167,62 @@ export async function esbuildAsync(
167167
}
168168
}
169169

170-
export function removeFiles(
171-
root: string,
172-
conditionFn: (file: string) => boolean,
173-
searchingDir: string = "",
174-
) {
175-
traverseFiles(
176-
root,
177-
conditionFn,
178-
(filePath) => fs.rmSync(filePath, { force: true }),
179-
searchingDir,
180-
);
181-
}
170+
/**
171+
* Type of the parameter of `traverseFiles` callbacks
172+
*/
173+
export type TraversePath = {
174+
absolutePath: string;
175+
relativePath: string;
176+
};
182177

183178
/**
184179
* Recursively traverse files in a directory and call `callbackFn` when `conditionFn` returns true
180+
*
181+
* The callbacks are passed both the absolute and relative (to root) path to files.
182+
*
185183
* @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
184+
* @param conditionFn - Called to determine if `callbackFn` should be called.
185+
* @param callbackFn - Called when `conditionFn` returns true.
188186
* @param searchingDir - Directory to search (used for recursion)
189187
*/
190188
export function traverseFiles(
191189
root: string,
192-
conditionFn: (file: string) => boolean,
193-
callbackFn: (filePath: string) => void,
190+
conditionFn: (paths: TraversePath) => boolean,
191+
callbackFn: (paths: TraversePath) => void,
194192
searchingDir: string = "",
195193
) {
196194
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-
);
195+
const relativePath = path.join(searchingDir, file);
196+
const absolutePath = path.join(root, relativePath);
197+
198+
if (fs.statSync(absolutePath).isDirectory()) {
199+
traverseFiles(root, conditionFn, callbackFn, relativePath);
206200
return;
207201
}
208202

209-
if (conditionFn(path.join(searchingDir, file))) {
210-
callbackFn(filePath);
203+
if (conditionFn({ absolutePath, relativePath })) {
204+
callbackFn({ absolutePath, relativePath });
211205
}
212206
});
213207
}
214208

209+
/**
210+
* Recursively delete files.
211+
*
212+
* @see `traverseFiles`.
213+
*
214+
* @param root Root directory to search.
215+
* @param conditionFn Predicate used to delete the files.
216+
*/
217+
export function removeFiles(
218+
root: string,
219+
conditionFn: (paths: TraversePath) => boolean,
220+
) {
221+
traverseFiles(root, conditionFn, ({ absolutePath }) =>
222+
fs.rmSync(absolutePath, { force: true }),
223+
);
224+
}
225+
215226
export function getHtmlPages(dotNextPath: string) {
216227
// Get a list of HTML pages
217228
//

0 commit comments

Comments
 (0)