Skip to content

Commit 536af57

Browse files
refactor: use native Node.js instead of fs-extra (#13736)
1 parent 3365254 commit 536af57

File tree

17 files changed

+261
-296
lines changed

17 files changed

+261
-296
lines changed

integration/cli-test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { spawnSync } from "node:child_process";
2+
import { existsSync, rmSync } from "node:fs";
23
import * as path from "node:path";
34

45
import { expect, test } from "@playwright/test";
56
import dedent from "dedent";
67
import semver from "semver";
7-
import fse from "fs-extra";
88

99
import { createProject } from "./helpers/vite";
1010

@@ -129,13 +129,13 @@ test.describe("cli", () => {
129129
let entryClientFile = path.join(cwd, "app", "entry.client.tsx");
130130
let entryServerFile = path.join(cwd, "app", "entry.server.tsx");
131131

132-
expect(fse.existsSync(entryServerFile)).toBeFalsy();
133-
expect(fse.existsSync(entryClientFile)).toBeFalsy();
132+
expect(existsSync(entryServerFile)).toBeFalsy();
133+
expect(existsSync(entryClientFile)).toBeFalsy();
134134

135135
run(["reveal"], { cwd });
136136

137-
expect(fse.existsSync(entryServerFile)).toBeTruthy();
138-
expect(fse.existsSync(entryClientFile)).toBeTruthy();
137+
expect(existsSync(entryServerFile)).toBeTruthy();
138+
expect(existsSync(entryClientFile)).toBeTruthy();
139139
});
140140

141141
test("generates specified entries in the app directory", async () => {
@@ -144,31 +144,31 @@ test.describe("cli", () => {
144144
let entryClientFile = path.join(cwd, "app", "entry.client.tsx");
145145
let entryServerFile = path.join(cwd, "app", "entry.server.tsx");
146146

147-
expect(fse.existsSync(entryServerFile)).toBeFalsy();
148-
expect(fse.existsSync(entryClientFile)).toBeFalsy();
147+
expect(existsSync(entryServerFile)).toBeFalsy();
148+
expect(existsSync(entryClientFile)).toBeFalsy();
149149

150150
run(["reveal", "entry.server"], { cwd });
151-
expect(fse.existsSync(entryServerFile)).toBeTruthy();
152-
expect(fse.existsSync(entryClientFile)).toBeFalsy();
153-
fse.removeSync(entryServerFile);
151+
expect(existsSync(entryServerFile)).toBeTruthy();
152+
expect(existsSync(entryClientFile)).toBeFalsy();
153+
rmSync(entryServerFile);
154154

155155
run(["reveal", "entry.client"], { cwd });
156-
expect(fse.existsSync(entryClientFile)).toBeTruthy();
157-
expect(fse.existsSync(entryServerFile)).toBeFalsy();
156+
expect(existsSync(entryClientFile)).toBeTruthy();
157+
expect(existsSync(entryServerFile)).toBeFalsy();
158158
});
159159

160160
test("generates entry.{server,client}.jsx in the app directory with --no-typescript", async () => {
161161
const cwd = await createProject();
162162
let entryClientFile = path.join(cwd, "app", "entry.client.jsx");
163163
let entryServerFile = path.join(cwd, "app", "entry.server.jsx");
164164

165-
expect(fse.existsSync(entryServerFile)).toBeFalsy();
166-
expect(fse.existsSync(entryClientFile)).toBeFalsy();
165+
expect(existsSync(entryServerFile)).toBeFalsy();
166+
expect(existsSync(entryClientFile)).toBeFalsy();
167167

168168
run(["reveal", "--no-typescript"], { cwd });
169169

170-
expect(fse.existsSync(entryServerFile)).toBeTruthy();
171-
expect(fse.existsSync(entryClientFile)).toBeTruthy();
170+
expect(existsSync(entryServerFile)).toBeTruthy();
171+
expect(existsSync(entryClientFile)).toBeTruthy();
172172
});
173173
});
174174
});

integration/helpers/create-fixture.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { existsSync, readFileSync } from "node:fs";
2+
import { cp, mkdir, readFile, writeFile } from "node:fs/promises";
3+
import path from "node:path";
14
import type { Writable } from "node:stream";
25
import { Readable } from "node:stream";
3-
import path from "node:path";
46
import url from "node:url";
5-
import fse from "fs-extra";
67
import express from "express";
78
import getPort from "get-port";
89
import stripIndent from "strip-indent";
@@ -51,7 +52,7 @@ export async function createFixture(init: FixtureInit, mode?: ServerMode) {
5152
).href;
5253

5354
let getBrowserAsset = async (asset: string) => {
54-
return fse.readFile(
55+
return readFile(
5556
path.join(projectDir, "public", asset.replace(/^\//, "")),
5657
"utf8"
5758
);
@@ -64,7 +65,7 @@ export async function createFixture(init: FixtureInit, mode?: ServerMode) {
6465
isSpaMode: init.spaMode,
6566
prerender: init.prerender,
6667
requestDocument() {
67-
let html = fse.readFileSync(
68+
let html = readFileSync(
6869
path.join(projectDir, "build/client/index.html")
6970
);
7071
return new Response(html, {
@@ -102,25 +103,21 @@ export async function createFixture(init: FixtureInit, mode?: ServerMode) {
102103
"client",
103104
"__spa-fallback.html"
104105
);
105-
let html = fse.existsSync(mainPath)
106-
? fse.readFileSync(mainPath)
107-
: fse.readFileSync(fallbackPath);
106+
let html = existsSync(mainPath)
107+
? readFileSync(mainPath)
108+
: readFileSync(fallbackPath);
108109
return new Response(html, {
109110
headers: {
110111
"Content-Type": "text/html",
111112
},
112113
});
113114
},
114115
requestResource(href: string) {
115-
let data = fse.readFileSync(
116-
path.join(projectDir, "build/client", href)
117-
);
116+
let data = readFileSync(path.join(projectDir, "build/client", href));
118117
return new Response(data);
119118
},
120119
async requestSingleFetchData(href: string) {
121-
let data = fse.readFileSync(
122-
path.join(projectDir, "build/client", href)
123-
);
120+
let data = readFileSync(path.join(projectDir, "build/client", href));
124121
let stream = createReadableStreamFromReadable(Readable.from(data));
125122
return {
126123
status: 200,
@@ -280,7 +277,7 @@ export async function createAppFixture(fixture: Fixture, mode?: ServerMode) {
280277
let port = await getPort();
281278
let app = express();
282279
app.use(express.static(path.join(fixture.projectDir, "build/client")));
283-
app.get("*", (_, res, next) =>
280+
app.get("*", (_, res) =>
284281
res.sendFile(path.join(fixture.projectDir, "build/client/index.html"))
285282
);
286283
let server = app.listen(port);
@@ -300,11 +297,11 @@ export async function createAppFixture(fixture: Fixture, mode?: ServerMode) {
300297
let file = req.path.endsWith(".data")
301298
? req.path
302299
: req.path + "/index.html";
303-
if (file.endsWith(".html") && !fse.existsSync(path.join(dir, file))) {
300+
if (file.endsWith(".html") && !existsSync(path.join(dir, file))) {
304301
file = "__spa-fallback.html";
305302
}
306303
let filePath = path.join(dir, file);
307-
if (fse.existsSync(filePath)) {
304+
if (existsSync(filePath)) {
308305
res.sendFile(filePath, next);
309306
} else {
310307
// Avoid a built-in console error from `sendFile` on 404's
@@ -375,8 +372,8 @@ export async function createFixtureProject(
375372
let projectDir = path.join(TMP_DIR, projectName);
376373
let port = init.port ?? (await getPort());
377374

378-
await fse.ensureDir(projectDir);
379-
await fse.copy(integrationTemplateDir, projectDir);
375+
await mkdir(projectDir, { recursive: true });
376+
await cp(integrationTemplateDir, projectDir, { recursive: true });
380377

381378
let hasViteConfig = Object.keys(init.files ?? {}).some((filename) =>
382379
filename.startsWith("vite.config.")
@@ -463,10 +460,10 @@ async function writeTestFiles(
463460
await Promise.all(
464461
Object.keys(files ?? {}).map(async (filename) => {
465462
let filePath = path.join(dir, filename);
466-
await fse.ensureDir(path.dirname(filePath));
463+
await mkdir(path.dirname(filePath), { recursive: true });
467464
let file = files![filename];
468465

469-
await fse.writeFile(filePath, stripIndent(file));
466+
await writeFile(filePath, stripIndent(file));
470467
})
471468
);
472469
}

integration/helpers/vite.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { spawn, spawnSync, type ChildProcess } from "node:child_process";
2-
import path from "pathe";
3-
import fs from "node:fs/promises";
4-
import type { Readable } from "node:stream";
5-
import url from "node:url";
2+
import { cp, mkdir, readFile, writeFile } from "node:fs/promises";
63
import { createRequire } from "node:module";
74
import { platform } from "node:os";
8-
import fse from "fs-extra";
5+
import type { Readable } from "node:stream";
6+
import url from "node:url";
7+
import path from "pathe";
98
import stripIndent from "strip-indent";
109
import waitOn from "wait-on";
1110
import getPort from "get-port";
@@ -211,18 +210,18 @@ export async function createProject(
211210
) {
212211
let projectName = `rr-${Math.random().toString(32).slice(2)}`;
213212
let projectDir = path.join(TMP_DIR, projectName);
214-
await fse.ensureDir(projectDir);
213+
await mkdir(projectDir, { recursive: true });
215214

216215
// base template
217216
let templateDir = path.resolve(__dirname, templateName);
218-
await fse.copy(templateDir, projectDir, { errorOnExist: true });
217+
await cp(templateDir, projectDir, { errorOnExist: true, recursive: true });
219218

220219
// user-defined files
221220
await Promise.all(
222221
Object.entries(files).map(async ([filename, contents]) => {
223222
let filepath = path.join(projectDir, filename);
224-
await fse.ensureDir(path.dirname(filepath));
225-
await fse.writeFile(filepath, stripIndent(contents));
223+
await mkdir(path.dirname(filepath), { recursive: true });
224+
await writeFile(filepath, stripIndent(contents));
226225
})
227226
);
228227

@@ -231,7 +230,7 @@ export async function createProject(
231230

232231
// Avoid "Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env
233232
// being set" in vite-ecosystem-ci which breaks empty stderr assertions. To fix
234-
// this we always ensure that only NO_COLOR is set after spreading process.env.
233+
// this, we always ensure that only NO_COLOR is set after spreading process.env.
235234
const colorEnv = {
236235
FORCE_COLOR: undefined,
237236
NO_COLOR: "1",
@@ -490,11 +489,11 @@ export function createEditor(projectDir: string) {
490489
transform: (contents: string) => string
491490
) {
492491
let filepath = path.join(projectDir, file);
493-
let contents = await fs.readFile(filepath, "utf8");
494-
await fs.writeFile(filepath, transform(contents), "utf8");
492+
let contents = await readFile(filepath, "utf8");
493+
await writeFile(filepath, transform(contents), "utf8");
495494

496495
return async function revert() {
497-
await fs.writeFile(filepath, contents, "utf8");
496+
await writeFile(filepath, contents, "utf8");
498497
};
499498
};
500499
}

integration/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"dedent": "^0.7.0",
2323
"execa": "^5.1.1",
2424
"express": "^4.19.2",
25-
"fs-extra": "^10.0.0",
2625
"get-port": "^5.1.1",
2726
"glob": "8.0.3",
2827
"isbot": "^5.1.11",

integration/typegen-test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { spawnSync } from "node:child_process";
2+
import { mkdirSync, renameSync } from "node:fs";
3+
import { readFile, writeFile } from "node:fs/promises";
24
import * as path from "node:path";
35

46
import { expect, test } from "@playwright/test";
57
import dedent from "dedent";
6-
import fse from "fs-extra";
78

89
import { createProject } from "./helpers/vite";
910

@@ -379,7 +380,8 @@ test.describe("typegen", () => {
379380
}
380381
`,
381382
});
382-
fse.moveSync(path.join(cwd, "app"), path.join(cwd, "src/myapp"));
383+
mkdirSync(path.join(cwd, "src"));
384+
renameSync(path.join(cwd, "app"), path.join(cwd, "src/myapp"));
383385

384386
const proc = typecheck(cwd);
385387
expect(proc.stdout.toString()).toBe("");
@@ -622,9 +624,15 @@ test.describe("typegen", () => {
622624
`,
623625
});
624626

625-
const tsconfig = await fse.readJson(path.join(cwd, "tsconfig.json"));
627+
const tsconfig = JSON.parse(
628+
await readFile(path.join(cwd, "tsconfig.json"), "utf-8")
629+
);
626630
tsconfig.compilerOptions.moduleDetection = "force";
627-
await fse.writeJson(path.join(cwd, "tsconfig.json"), tsconfig);
631+
await writeFile(
632+
path.join(cwd, "tsconfig.json"),
633+
JSON.stringify(tsconfig),
634+
"utf-8"
635+
);
628636

629637
const proc = typecheck(cwd);
630638
expect(proc.stdout.toString()).toBe("");

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
"@testing-library/react": "^16.3.0",
6666
"@testing-library/user-event": "^14.5.2",
6767
"@types/cross-spawn": "^6.0.2",
68-
"@types/fs-extra": "^8.1.2",
6968
"@types/glob": "7.2.0",
7069
"@types/jest": "^29.5.4",
7170
"@types/jsdom": "^21.1.1",
@@ -90,7 +89,6 @@
9089
"eslint-plugin-jsx-a11y": "^6.8.0",
9190
"eslint-plugin-react": "^7.34.1",
9291
"eslint-plugin-react-hooks": "next",
93-
"fs-extra": "^10.1.0",
9492
"history": "^5.3.0",
9593
"isbot": "^5.1.11",
9694
"jest": "^29.6.4",

0 commit comments

Comments
 (0)