Skip to content

Commit

Permalink
fix cleaning metro cache
Browse files Browse the repository at this point in the history
  • Loading branch information
TMisiukiewicz committed Jan 12, 2024
1 parent 42ce0ed commit a96ceb3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
6 changes: 4 additions & 2 deletions packages/cli-clean/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"dependencies": {
"@react-native-community/cli-tools": "13.4.0",
"chalk": "^4.1.2",
"execa": "^5.0.0"
"execa": "^5.0.0",
"glob": "^7.1.3"
},
"files": [
"build",
Expand All @@ -19,7 +20,8 @@
],
"devDependencies": {
"@react-native-community/cli-types": "13.4.0",
"@types/prompts": "^2.4.4"
"@types/prompts": "^2.4.4",
"@types/glob": "^7.1.1"
},
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-clean",
"repository": {
Expand Down
31 changes: 30 additions & 1 deletion packages/cli-clean/src/__tests__/clean.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import execa from 'execa';
import os from 'os';
import prompts from 'prompts';
import {clean} from '../clean';
import {clean, cleanDir} from '../clean';
import {cleanup, getTempDirectory, writeFiles} from '../../../../jest/helpers';
import fs from 'fs';

const DIR = getTempDirectory('temp-cache');

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

afterEach(() => {
cleanup(DIR);
});

describe('clean', () => {
const mockConfig: any = {};

Expand Down Expand Up @@ -48,4 +56,25 @@ describe('clean', () => {
expect.anything(),
);
});

it('should remove paths defined with patterns', async () => {
writeFiles(DIR, {
'metro-cache/cache.txt': 'cache file',
'metro-zxcvbnm/cache.txt': 'cache file',
});

await cleanDir(`${DIR}/metro-*`);

expect(fs.readdirSync(DIR)).toEqual([]);
});

it('should remove paths defined without patterns', async () => {
writeFiles(DIR, {
'metro-cache/cache.txt': 'cache file',
});

await cleanDir(`${DIR}/metro-cache`);

expect(fs.readdirSync(DIR)).toEqual([]);
});
});
37 changes: 31 additions & 6 deletions packages/cli-clean/src/clean.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {getLoader, prompt} from '@react-native-community/cli-tools';
import {getLoader, logger, prompt} from '@react-native-community/cli-tools';
import type {Config as CLIConfig} from '@react-native-community/cli-types';
import chalk from 'chalk';
import execa from 'execa';
import {existsSync as fileExists, rm} from 'fs';
import os from 'os';
import path from 'path';
import {promisify} from 'util';
import glob from 'glob';

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

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

function cleanDir(directory: string): Promise<void> {
if (!fileExists(directory)) {
return Promise.resolve();
}
function isDirectoryPattern(directory: string): boolean {
return directory.endsWith('*') || directory.endsWith('?');
}

export async function cleanDir(directory: string): Promise<void> {
try {
if (isDirectoryPattern(directory)) {
const directories = await new Promise<string[]>((resolve, reject) => {
glob(directory, {}, (err, foundDirectories) => {
if (err) {
reject(err);
} else {
resolve(foundDirectories);
}
});
});

return rmAsync(directory, {maxRetries: 3, recursive: true, force: true});
for (const dir of directories) {
await rmAsync(dir, rmAsyncOptions);
}
} else {
if (!fileExists(directory)) {
return;
}
await rmAsync(directory, rmAsyncOptions);
}
} catch (error) {
logger.error(`An error occurred while cleaning the directory: ${error}`);
}
}

async function promptForCaches(
Expand Down

0 comments on commit a96ceb3

Please sign in to comment.