Skip to content

Commit 60f631a

Browse files
[infra] Replace fs-extra with node:fs calls (#19148)
1 parent c81102f commit 60f631a

File tree

6 files changed

+34
-30
lines changed

6 files changed

+34
-30
lines changed

docs/scripts/buildServiceWorker.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/* eslint-disable no-console */
22
const path = require('path');
33
const fs = require('fs/promises');
4-
const fse = require('fs-extra');
54

65
async function prepend(file, string) {
76
const data = await fs.readFile(file, 'utf8');
87
await fs.writeFile(file, string + data, 'utf8');
98
}
109

1110
async function run() {
12-
const swDest = path.join(__dirname, '../export/x/sw.js');
11+
const swDestDir = path.join(__dirname, '../export/x');
12+
const swDest = path.join(swDestDir, 'sw.js');
1313
const swSrc = path.join(__dirname, '../src/sw.js');
1414

15-
await fse.copy(swSrc, swDest);
15+
await fs.mkdir(swDestDir, { recursive: true });
16+
await fs.copyFile(swSrc, swDest);
1617
await prepend(
1718
swDest,
1819
`

docs/scripts/formattedTSDemos.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
const ignoreList = ['/pages.ts', 'styling.ts', 'styling.tsx', 'types.ts'];
1313

14-
const fse = require('fs-extra');
1514
const fs = require('fs');
1615
const path = require('path');
1716
const babel = require('@babel/core');
@@ -91,14 +90,16 @@ const previewOverride = {
9190
async function transpileFile(tsxPath, program, ignoreCache = false) {
9291
const jsPath = tsxPath.replace(/\.tsx?$/, '.js');
9392
try {
94-
if (!ignoreCache && (await fse.exists(jsPath))) {
95-
const [jsStat, tsxStat] = await Promise.all([
96-
fs.promises.stat(jsPath),
97-
fs.promises.stat(tsxPath),
98-
]);
99-
if (jsStat.mtimeMs > tsxStat.mtimeMs) {
100-
// JavaScript version is newer, skip transpiling
101-
return TranspileResult.Skipped;
93+
if (!ignoreCache) {
94+
const ignoreNotFound = (err) => (err.code === 'ENOENT' ? null : Promise.reject(err));
95+
const jsStat = await fs.promises.stat(jsPath).catch(ignoreNotFound);
96+
97+
if (jsStat != null) {
98+
const tsxStat = await fs.promises.stat(tsxPath);
99+
if (jsStat.mtimeMs > tsxStat.mtimeMs) {
100+
// JavaScript version is newer, skip transpiling
101+
return TranspileResult.Skipped;
102+
}
102103
}
103104
}
104105

@@ -202,7 +203,7 @@ async function main(argv) {
202203
}
203204

204205
tsxFiles.forEach((filePath) => {
205-
fs.promises.watchFile(filePath, { interval: 500 }, async () => {
206+
fs.watchFile(filePath, { interval: 500 }, async () => {
206207
if ((await transpileFile(filePath, program, true)) === 0) {
207208
console.log('Success - %s', filePath);
208209
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@
142142
"eslint-plugin-mui-x": "workspace:^",
143143
"fast-glob": "catalog:",
144144
"format-util": "^1.0.5",
145-
"fs-extra": "^11.3.1",
146145
"glob-gitignore": "^1.0.15",
147146
"globby": "^14.1.0",
148147
"html-webpack-plugin": "^5.6.3",

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/importDocsStatic.mjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import glob from 'fast-glob';
2-
import fse from 'fs-extra';
2+
import fs from 'node:fs/promises';
33

44
async function run() {
55
const importFiles = [
@@ -15,11 +15,14 @@ async function run() {
1515
const newFiles = glob.sync(`node_modules/@mui/monorepo/${pattern}`);
1616
return acc.concat(newFiles);
1717
}, []);
18-
files.forEach((file) => {
19-
fse.copySync(file, file.replace('node_modules/@mui/monorepo/docs/', 'docs/'));
18+
19+
const copyPromises = files.map(async (file) => {
20+
await fs.copyFile(file, file.replace('node_modules/@mui/monorepo/docs/', 'docs/'));
2021
// eslint-disable-next-line no-console
2122
console.log(`copy ${file}`);
2223
});
24+
25+
await Promise.all(copyPromises);
2326
}
2427

2528
run();

test/regressions/index.test.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as fse from 'fs-extra';
21
import * as path from 'path';
32
import * as childProcess from 'child_process';
43
import { type Browser, chromium } from '@playwright/test';
54
import { major } from '@mui/material/version';
5+
import fs from 'node:fs/promises';
66

77
const isMaterialUIv6 = major === 6;
88
const isMaterialUIv7 = major === 7;
@@ -69,7 +69,7 @@ async function main() {
6969
routes = routes.map((route) => route.replace(baseUrl, ''));
7070

7171
// prepare screenshots
72-
await fse.emptyDir(screenshotDir);
72+
await emptyDir(screenshotDir);
7373

7474
async function navigateToTest(route: string) {
7575
// Use client-side routing which is much faster than full page navigation via page.goto().
@@ -138,7 +138,6 @@ async function main() {
138138
}
139139

140140
const screenshotPath = path.resolve(screenshotDir, `.${route}.png`);
141-
await fse.ensureDir(path.dirname(screenshotPath));
142141

143142
const testcase = await page.waitForSelector(
144143
`[data-testid="testcase"][data-testpath="${route}"]:not([aria-busy="true"])`,
@@ -189,7 +188,6 @@ async function main() {
189188
it('should position the headers matching the columns', async () => {
190189
const route = '/docs-data-grid-virtualization/ColumnVirtualizationGrid';
191190
const screenshotPath = path.resolve(screenshotDir, `.${route}ScrollLeft400px.png`);
192-
await fse.ensureDir(path.dirname(screenshotPath));
193191

194192
await navigateToTest(route);
195193

@@ -216,9 +214,6 @@ async function main() {
216214
it('should position charts axis tooltip 8px away from the pointer', async () => {
217215
const route = '/docs-charts-tooltip/Interaction';
218216
const axisScreenshotPath = path.resolve(screenshotDir, `.${route}AxisTooltip.png`);
219-
const itemScreenshotPath = path.resolve(screenshotDir, `.${route}ItemTooltip.png`);
220-
await fse.ensureDir(path.dirname(axisScreenshotPath));
221-
await fse.ensureDir(path.dirname(itemScreenshotPath));
222217

223218
await navigateToTest(route);
224219

@@ -243,7 +238,6 @@ async function main() {
243238
it('should export a chart as PNG', async () => {
244239
const route = '/docs-charts-export/ExportChartAsImage';
245240
const screenshotPath = path.resolve(screenshotDir, `.${route}PNG.png`);
246-
await fse.ensureDir(path.dirname(screenshotPath));
247241

248242
await navigateToTest(route);
249243

@@ -274,7 +268,6 @@ async function main() {
274268
it('should take a screenshot of the data grid print preview', async () => {
275269
const route = '/docs-data-grid-export/ExportDefaultToolbar';
276270
const screenshotPath = path.resolve(screenshotDir, `.${route}Print.png`);
277-
await fse.ensureDir(path.dirname(screenshotPath));
278271

279272
await navigateToTest(route);
280273

@@ -302,7 +295,6 @@ async function main() {
302295
it('should take a screenshot of the charts print preview', async () => {
303296
const route = '/docs-charts-export/PrintChart';
304297
const screenshotPath = path.resolve(screenshotDir, `.${route}Print.png`);
305-
await fse.ensureDir(path.dirname(screenshotPath));
306298

307299
await navigateToTest(route);
308300

@@ -428,3 +420,14 @@ async function newTestPage(browser: Browser) {
428420

429421
return page;
430422
}
423+
424+
async function emptyDir(dir: string) {
425+
let items;
426+
try {
427+
items = await fs.readdir(dir);
428+
} catch {
429+
return fs.mkdir(dir, { recursive: true });
430+
}
431+
432+
return Promise.all(items.map((item) => fs.rm(path.join(dir, item), { recursive: true })));
433+
}

0 commit comments

Comments
 (0)