Skip to content

Commit

Permalink
metro-memory-fs: Support rmSync 'force'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
robhogan authored and facebook-github-bot committed Sep 19, 2024
1 parent ec584b9 commit a792d85
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 13 additions & 0 deletions packages/metro-memory-fs/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
10 changes: 6 additions & 4 deletions packages/metro-memory-fs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a792d85

Please sign in to comment.