From a792d85ffde3c21c3fbf64ac9404ab0afe5ff957 Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Thu, 19 Sep 2024 12:26:25 -0700 Subject: [PATCH] metro-memory-fs: Support rmSync 'force' Summary: The `force` option of `rmSync` should prevent it throwing on non-existent directories. https://nodejs.org/docs/latest-v18.x/api/fs.html#fsrmsyncpath-options ``` - **[Fix]**: `metro-memory-fs`: Respect `force` in `rmSync` ``` Reviewed By: vzaidman Differential Revision: D63030996 fbshipit-source-id: e08e2b98650c8b460ac03fdcb27f874cbed826b6 --- .../metro-memory-fs/src/__tests__/index-test.js | 13 +++++++++++++ packages/metro-memory-fs/src/index.js | 10 ++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/metro-memory-fs/src/__tests__/index-test.js b/packages/metro-memory-fs/src/__tests__/index-test.js index 5c2008afe8..48c0dd0f66 100644 --- a/packages/metro-memory-fs/src/__tests__/index-test.js +++ b/packages/metro-memory-fs/src/__tests__/index-test.js @@ -771,6 +771,19 @@ describe('posix support', () => { fs.rmSync('/foo', {recursive: true}); expect(fs.readdirSync('/')).toEqual([]); }); + + it.each([false, undefined])( + 'throws on non-existent path (force: %s)', + force => { + expect(() => + fs.rmSync('/notexists', force != null ? {force} : {}), + ).toThrow('ENOENT'); + }, + ); + + it('succeeds non-existent path (force: true)', () => { + fs.rmSync('/notexists', {force: true}); + }); }); describe('readlink', () => { diff --git a/packages/metro-memory-fs/src/index.js b/packages/metro-memory-fs/src/index.js index ed50c90b1f..348e27ebea 100644 --- a/packages/metro-memory-fs/src/index.js +++ b/packages/metro-memory-fs/src/index.js @@ -927,16 +927,18 @@ class MemoryFs { }); }; - rmSync: (filePath: FilePath, options?: {recursive?: boolean}) => void = ( + rmSync: ( filePath: FilePath, - options, - ) => { + options?: {recursive?: boolean, force?: boolean}, + ) => void = (filePath: FilePath, options) => { filePath = pathStr(filePath); const {dirNode, node, basename} = this._resolve(filePath, { keepFinalSymlink: true, }); if (node == null) { - throw makeError('ENOENT', filePath, 'no such file or directory'); + if (options?.force !== true) { + throw makeError('ENOENT', filePath, 'no such file or directory'); + } } else if (node.type === 'directory') { if (options && options.recursive) { // NOTE: File watchers won't be informed of recursive deletions