diff --git a/packages/cli-clean/README.md b/packages/cli-clean/README.md index 2d7364bde..6c32b589f 100644 --- a/packages/cli-clean/README.md +++ b/packages/cli-clean/README.md @@ -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 ` > default: current working directory diff --git a/packages/cli-clean/src/__tests__/clean.test.ts b/packages/cli-clean/src/__tests__/clean.test.ts index 022f805ca..761989199 100644 --- a/packages/cli-clean/src/__tests__/clean.test.ts +++ b/packages/cli-clean/src/__tests__/clean.test.ts @@ -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'); @@ -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, + }); + }, + ); + }); }); diff --git a/packages/cli-clean/src/clean.ts b/packages/cli-clean/src/clean.ts index 943a39525..c01bcf75d 100644 --- a/packages/cli-clean/src/clean.ts +++ b/packages/cli-clean/src/clean.ts @@ -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')), }, ], },