|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + */ |
| 11 | + |
| 12 | +import type {Task} from './types'; |
| 13 | +import type {Options as ExecaOptions} from 'execa'; |
| 14 | + |
| 15 | +import {isMacOS, isWindows, task} from './utils'; |
| 16 | +import execa from 'execa'; |
| 17 | +import {existsSync, readdirSync, rm} from 'fs'; |
| 18 | +import os from 'os'; |
| 19 | +import path from 'path'; |
| 20 | + |
| 21 | +type CleanTasks = { |
| 22 | + android: (androidSrcDir: ?string) => Task[], |
| 23 | + metro: () => Task[], |
| 24 | + npm: (projectRootDir: string, verifyCache?: boolean) => Task[], |
| 25 | + bun: (projectRootDir: string) => Task[], |
| 26 | + watchman: (projectRootDir: string) => Task[], |
| 27 | + yarn: (projectRootDir: string) => Task[], |
| 28 | + cocoapods?: (projectRootDir: string) => Task[], |
| 29 | +}; |
| 30 | + |
| 31 | +const rmrf = (pathname: string) => { |
| 32 | + if (!existsSync(pathname)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + rm(pathname, {maxRetries: 3, recursive: true, force: true}); |
| 36 | +}; |
| 37 | + |
| 38 | +/** |
| 39 | + * Removes the contents of a directory matching a given pattern, but keeps the directory. |
| 40 | + * @private |
| 41 | + */ |
| 42 | +export function deleteDirectoryContents( |
| 43 | + directory: string, |
| 44 | + filePattern: RegExp, |
| 45 | +): Task['action'] { |
| 46 | + return async function deleteDirectoryContentsAction() { |
| 47 | + const base = path.dirname(directory); |
| 48 | + const files = readdirSync(base).filter((filename: string) => |
| 49 | + filePattern.test(filename), |
| 50 | + ); |
| 51 | + for (const filename of files) { |
| 52 | + rmrf(path.join(base, filename)); |
| 53 | + } |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Removes a directory recursively. |
| 59 | + * @private |
| 60 | + */ |
| 61 | +export function deleteDirectory(directory: string): Task['action'] { |
| 62 | + return async function cleanDirectoryAction() { |
| 63 | + rmrf(directory); |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Deletes the contents of the tmp directory matching a given pattern. |
| 69 | + * @private |
| 70 | + */ |
| 71 | +export function deleteTmpDirectoryContents( |
| 72 | + filepattern: RegExp, |
| 73 | +): ReturnType<typeof deleteDirectoryContents> { |
| 74 | + return deleteDirectoryContents(os.tmpdir(), filepattern); |
| 75 | +} |
| 76 | + |
| 77 | +// The tasks that cleanup various build artefacts. |
| 78 | +export const tasks: CleanTasks = { |
| 79 | + /** |
| 80 | + * Cleans up the Android Gradle cache |
| 81 | + */ |
| 82 | + android: (androidSrcDir: ?string) => [ |
| 83 | + task('🧹 Clean Gradle cache', async function gradle(opts?: ExecaOptions) { |
| 84 | + const gradlew = path.join( |
| 85 | + androidSrcDir ?? 'android', |
| 86 | + isWindows ? 'gradlew.bat' : 'gradlew', |
| 87 | + ); |
| 88 | + |
| 89 | + if (!existsSync(gradlew)) { |
| 90 | + return; |
| 91 | + } |
| 92 | + const script = path.basename(gradlew); |
| 93 | + const cwd = path.dirname(gradlew); |
| 94 | + await execa(isWindows ? script : `./${script}`, ['clean'], { |
| 95 | + cwd, |
| 96 | + ...opts, |
| 97 | + }); |
| 98 | + }), |
| 99 | + ], |
| 100 | + |
| 101 | + /** |
| 102 | + * Agressively cleans up all Metro caches. |
| 103 | + */ |
| 104 | + metro: () => [ |
| 105 | + task('🧹 Clean Metro cache', deleteTmpDirectoryContents(/^metro-.+/)), |
| 106 | + task('🧹 Clean Haste cache', deleteTmpDirectoryContents(/^haste-map-.+/)), |
| 107 | + task( |
| 108 | + '🧹 Clean React Native cache', |
| 109 | + deleteTmpDirectoryContents(/^react-.+/), |
| 110 | + ), |
| 111 | + ], |
| 112 | + |
| 113 | + /** |
| 114 | + * Cleans up the `node_modules` folder and optionally garbage collects the npm cache. |
| 115 | + */ |
| 116 | + npm: (projectRootDir: string, verifyCache = false) => { |
| 117 | + const _tasks = [ |
| 118 | + task( |
| 119 | + '🧹 Clean node_modules', |
| 120 | + deleteDirectory(path.join(projectRootDir, 'node_modules')), |
| 121 | + ), |
| 122 | + ]; |
| 123 | + if (verifyCache) { |
| 124 | + _tasks.push( |
| 125 | + task('🔬 Verify npm cache', (opts?: ExecaOptions) => |
| 126 | + execa('npm', ['cache', 'verify'], {cwd: projectRootDir, ...opts}), |
| 127 | + ), |
| 128 | + ); |
| 129 | + } |
| 130 | + return _tasks; |
| 131 | + }, |
| 132 | + |
| 133 | + /** |
| 134 | + * Cleans up the Bun cache. |
| 135 | + */ |
| 136 | + bun: (projectRootDir: string) => [ |
| 137 | + task('🧹 Clean Bun cache', (opts?: ExecaOptions) => |
| 138 | + execa('bun', ['pm', 'cache', 'rm'], {cwd: projectRootDir, ...opts}), |
| 139 | + ), |
| 140 | + ], |
| 141 | + |
| 142 | + /** |
| 143 | + * Stops Watchman and clears its cache |
| 144 | + */ |
| 145 | + watchman: (projectRootDir: string) => [ |
| 146 | + task('✋ Stop Watchman', (opts?: ExecaOptions) => |
| 147 | + execa(isWindows ? 'tskill' : 'killall', ['watchman'], { |
| 148 | + cwd: projectRootDir, |
| 149 | + ...opts, |
| 150 | + }), |
| 151 | + ), |
| 152 | + task('🧹 Delete Watchman cache', (opts?: ExecaOptions) => |
| 153 | + execa('watchman', ['watch-del-all'], {cwd: projectRootDir, ...opts}), |
| 154 | + ), |
| 155 | + ], |
| 156 | + |
| 157 | + /** |
| 158 | + * Cleans up the Yarn cache |
| 159 | + */ |
| 160 | + yarn: (projectRootDir: string) => [ |
| 161 | + task('🧹 Clean Yarn cache', (opts?: ExecaOptions) => |
| 162 | + execa('yarn', ['cache', 'clean'], {cwd: projectRootDir, ...opts}), |
| 163 | + ), |
| 164 | + ], |
| 165 | +}; |
| 166 | + |
| 167 | +if (isMacOS) { |
| 168 | + /** |
| 169 | + * Cleans up the local and global CocoaPods cache |
| 170 | + */ |
| 171 | + tasks.cocoapods = (projectRootDir: string) => [ |
| 172 | + // TODO: add project root |
| 173 | + task( |
| 174 | + '🧹 Clean CocoaPods pod cache', |
| 175 | + function removePodCache(opts?: ExecaOptions) { |
| 176 | + return execa('bundle', ['exec', 'pod', 'deintegrate'], { |
| 177 | + cwd: projectRootDir, |
| 178 | + ...opts, |
| 179 | + }); |
| 180 | + }, |
| 181 | + ), |
| 182 | + ]; |
| 183 | +} |
| 184 | + |
| 185 | +// |
| 186 | +// Internal CLI |
| 187 | +// |
0 commit comments