Skip to content

Commit 4d79e33

Browse files
authoredJan 19, 2024
fix cleaning metro cache (#2258)
1 parent 0105408 commit 4d79e33

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed
 

‎packages/cli-clean/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
"dependencies": {
1111
"@react-native-community/cli-tools": "13.5.1-alpha.0",
1212
"chalk": "^4.1.2",
13-
"execa": "^5.0.0"
13+
"execa": "^5.0.0",
14+
"glob": "^7.1.3"
1415
},
1516
"files": [
1617
"build",
1718
"!*.d.ts",
1819
"!*.map"
1920
],
2021
"devDependencies": {
21-
"@react-native-community/cli-types": "13.5.1-alpha.0",
22-
"@types/prompts": "^2.4.4"
22+
"@react-native-community/cli-types": "13.5.1-alpha.0",
23+
"@types/prompts": "^2.4.4",
24+
"@types/glob": "^7.1.1"
2325
},
2426
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-clean",
2527
"repository": {

‎packages/cli-clean/src/__tests__/clean.test.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import execa from 'execa';
22
import os from 'os';
33
import prompts from 'prompts';
4-
import {clean} from '../clean';
4+
import {clean, cleanDir} from '../clean';
5+
import {cleanup, getTempDirectory, writeFiles} from '../../../../jest/helpers';
6+
import fs from 'fs';
7+
8+
const DIR = getTempDirectory('temp-cache');
59

610
jest.mock('execa', () => jest.fn());
711
jest.mock('prompts', () => jest.fn());
812

13+
afterEach(() => {
14+
cleanup(DIR);
15+
});
16+
917
describe('clean', () => {
1018
const mockConfig: any = {};
1119

@@ -48,4 +56,25 @@ describe('clean', () => {
4856
expect.anything(),
4957
);
5058
});
59+
60+
it('should remove paths defined with patterns', async () => {
61+
writeFiles(DIR, {
62+
'metro-cache/cache.txt': 'cache file',
63+
'metro-zxcvbnm/cache.txt': 'cache file',
64+
});
65+
66+
await cleanDir(`${DIR}/metro-*`);
67+
68+
expect(fs.readdirSync(DIR)).toEqual([]);
69+
});
70+
71+
it('should remove paths defined without patterns', async () => {
72+
writeFiles(DIR, {
73+
'metro-cache/cache.txt': 'cache file',
74+
});
75+
76+
await cleanDir(`${DIR}/metro-cache`);
77+
78+
expect(fs.readdirSync(DIR)).toEqual([]);
79+
});
5180
});

‎packages/cli-clean/src/clean.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import {getLoader, prompt} from '@react-native-community/cli-tools';
1+
import {getLoader, logger, prompt} from '@react-native-community/cli-tools';
22
import type {Config as CLIConfig} from '@react-native-community/cli-types';
33
import chalk from 'chalk';
44
import execa from 'execa';
55
import {existsSync as fileExists, rm} from 'fs';
66
import os from 'os';
77
import path from 'path';
88
import {promisify} from 'util';
9+
import glob from 'glob';
910

1011
type Args = {
1112
include?: string;
@@ -28,13 +29,37 @@ type CleanGroups = {
2829
const DEFAULT_GROUPS = ['metro', 'watchman'];
2930

3031
const rmAsync = promisify(rm);
32+
const rmAsyncOptions = {maxRetries: 3, recursive: true, force: true};
3133

32-
function cleanDir(directory: string): Promise<void> {
33-
if (!fileExists(directory)) {
34-
return Promise.resolve();
35-
}
34+
function isDirectoryPattern(directory: string): boolean {
35+
return directory.endsWith('*') || directory.endsWith('?');
36+
}
37+
38+
export async function cleanDir(directory: string): Promise<void> {
39+
try {
40+
if (isDirectoryPattern(directory)) {
41+
const directories = await new Promise<string[]>((resolve, reject) => {
42+
glob(directory, {}, (err, foundDirectories) => {
43+
if (err) {
44+
reject(err);
45+
} else {
46+
resolve(foundDirectories);
47+
}
48+
});
49+
});
3650

37-
return rmAsync(directory, {maxRetries: 3, recursive: true, force: true});
51+
for (const dir of directories) {
52+
await rmAsync(dir, rmAsyncOptions);
53+
}
54+
} else {
55+
if (!fileExists(directory)) {
56+
return;
57+
}
58+
await rmAsync(directory, rmAsyncOptions);
59+
}
60+
} catch (error) {
61+
logger.error(`An error occurred while cleaning the directory: ${error}`);
62+
}
3863
}
3964

4065
async function promptForCaches(

0 commit comments

Comments
 (0)
Please sign in to comment.