Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cli-clean/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Cleans caches. Commonly used to ensure build failures are not due to stale cache

Comma-separated flag of caches to clear e.g. `npm,yarn`. If omitted, an interactive prompt will appear. Valid values include `android`, `cocoapods`, `metro`, `npm`, `watchman`, and `yarn`.

On macOS, `cocoapods` removes installed pods from the configured iOS source directory (or `ios` under the project root) and the `.cocoapods` spec cache in your home directory, in addition to running `pod cache clean --all`.

#### `--project-root <string>`

> default: current working directory
Expand Down
59 changes: 59 additions & 0 deletions packages/cli-clean/src/__tests__/clean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import prompts from 'prompts';
import {clean, cleanDir} from '../clean';
import {cleanup, getTempDirectory, writeFiles} from '../../../../jest/helpers';
import fs from 'fs';
import path from 'path';

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

Expand Down Expand Up @@ -77,4 +78,62 @@ describe('clean', () => {

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

describe('CocoaPods paths', () => {
const projectRoot = path.join(DIR, 'project');
const homeDirectory = path.join(DIR, 'home');
let originalCwd: string;

beforeEach(() => {
originalCwd = process.cwd();
writeFiles(DIR, {
'project/package.json': '{}',
'home/.cocoapods/repos/spec.json': '{}',
'working/ios/Pods/keep.txt': 'unrelated project',
'working/~/.cocoapods/keep.txt': 'literal tilde directory',
});
process.chdir(path.join(DIR, 'working'));
jest.spyOn(os, 'platform').mockReturnValue('darwin');
jest.spyOn(os, 'homedir').mockReturnValue(homeDirectory);
});

afterEach(() => {
process.chdir(originalCwd);
jest.restoreAllMocks();
});

it('removes the spec cache from the home directory', async () => {
await clean([], {project: {}} as any, {
include: 'cocoapods',
projectRoot,
});

expect(fs.existsSync(path.join(homeDirectory, '.cocoapods'))).toBe(false);
expect(fs.existsSync('~/.cocoapods/keep.txt')).toBe(true);
});

it.each([undefined, 'native/ios'])(
'removes installed pods from the project with sourceDir %s',
async (sourceDir) => {
const iosDirectory = path.join(projectRoot, sourceDir ?? 'ios');
writeFiles(iosDirectory, {'Pods/cache.txt': 'installed pod'});

await clean(
[],
{
project: {
ios: sourceDir ? {sourceDir: iosDirectory} : null,
},
} as any,
{include: 'cocoapods', projectRoot},
);

expect(fs.existsSync(path.join(iosDirectory, 'Pods'))).toBe(false);
expect(fs.existsSync('ios/Pods/keep.txt')).toBe(true);
expect(execa).toBeCalledWith('pod', ['cache', 'clean', '--all'], {
cwd: projectRoot,
});
},
);
});
});
11 changes: 9 additions & 2 deletions packages/cli-clean/src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,18 @@ export async function clean(
},
{
label: 'Remove installed CocoaPods',
action: () => cleanDir('ios/Pods'),
action: () =>
cleanDir(
path.join(
ctx.project.ios?.sourceDir ??
path.join(projectRoot, 'ios'),
'Pods',
),
),
},
{
label: 'Remove CocoaPods spec cache',
action: () => cleanDir('~/.cocoapods'),
action: () => cleanDir(path.join(os.homedir(), '.cocoapods')),
},
],
},
Expand Down
Loading