From 07c1ae52044661e6e2dcbb5b0444494ce1e840ab Mon Sep 17 00:00:00 2001 From: Odio Marcelino Date: Sun, 29 Jun 2025 18:18:30 +0600 Subject: [PATCH] build/fs: modernize file-system helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced custom recursive directory creation and deprecated `fs.rmdirSync` logic with the native APIs introduced in Node ≥10: * [ensureDir](cci:1://file:///c:/Users/T2430514/Downloads/monaco-editor/build/fs.ts:12:0-21:1) now calls `fs.mkdirSync(dirname, { recursive: true })` – creates full path atomically – avoids race conditions and extra loops * [removeDir](cci:1://file:///c:/Users/T2430514/Downloads/monaco-editor/build/fs.ts:44:0-79:1) now ends with `fs.rmSync(dirPath, { recursive: true, force: true })` – officially supported replacement for `fs.rmdirSync` – handles non-empty directories and future Node versions This simplifies the code, removes deprecation warnings on Node 16+, and prevents build/packaging scripts from failing on newer runtimes. Co-Authored-By: S. M. Mohiuddin Khan Shiam <147746955+mohiuddin-khan-shiam@users.noreply.github.com> --- build/fs.ts | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/build/fs.ts b/build/fs.ts index 1fc659a2..a6a915ef 100644 --- a/build/fs.ts +++ b/build/fs.ts @@ -11,22 +11,14 @@ const REPO_ROOT = path.join(__dirname, '../'); const existingDirCache = new Set(); export function ensureDir(dirname: string) { - /** @type {string[]} */ - const dirs = []; - - while (dirname.length > REPO_ROOT.length) { - dirs.push(dirname); - dirname = path.dirname(dirname); + // Node.js ≥10 supports the recursive option for mkdirSync, which creates all + // parent folders that do not yet exist. This is more robust than the manual + // loop above and avoids race-conditions when multiple processes attempt to + // create the same directory tree concurrently. + if (!existingDirCache.has(dirname)) { + fs.mkdirSync(dirname, { recursive: true }); + existingDirCache.add(dirname); } - dirs.reverse(); - dirs.forEach((dir) => { - if (!existingDirCache.has(dir)) { - try { - fs.mkdirSync(dir); - } catch (err) {} - existingDirCache.add(dir); - } - }); } /** @@ -73,7 +65,10 @@ export function removeDir(_dirPath: string, keep?: (filename: string) => boolean } } if (!keepsFiles) { - fs.rmdirSync(dirPath); + // fs.rmdirSync is deprecated for non-empty directories and removed in + // future Node.js versions. fs.rmSync with { recursive: true, force: true } + // is the recommended API. + fs.rmSync(dirPath, { recursive: true, force: true }); } return keepsFiles; }