|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * and the Server Side Public License, v 1; you may not use this file except in |
| 5 | + * compliance with, at your election, the Elastic License or the Server Side |
| 6 | + * Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import dedent from 'dedent'; |
| 10 | +import del from 'del'; |
| 11 | +import ora from 'ora'; |
| 12 | +import { join, relative } from 'path'; |
| 13 | + |
| 14 | +import { getBazelDiskCacheFolder, getBazelRepositoryCacheFolder, runBazel } from '../utils/bazel'; |
| 15 | +import { isDirectory } from '../utils/fs'; |
| 16 | +import { log } from '../utils/log'; |
| 17 | +import { ICommand } from './'; |
| 18 | + |
| 19 | +export const ResetCommand: ICommand = { |
| 20 | + description: |
| 21 | + 'Deletes node_modules and output directories, resets internal and disk caches, and stops Bazel server', |
| 22 | + name: 'reset', |
| 23 | + |
| 24 | + async run(projects) { |
| 25 | + log.warning(dedent` |
| 26 | + In most cases, 'yarn kbn clean' is all that should be needed to recover a consistent state when |
| 27 | + problems arise. If you need to use this command, please let us know, as it should not be necessary. |
| 28 | + `); |
| 29 | + |
| 30 | + const toDelete = []; |
| 31 | + for (const project of projects.values()) { |
| 32 | + if (await isDirectory(project.nodeModulesLocation)) { |
| 33 | + toDelete.push({ |
| 34 | + cwd: project.path, |
| 35 | + pattern: relative(project.path, project.nodeModulesLocation), |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + if (await isDirectory(project.targetLocation)) { |
| 40 | + toDelete.push({ |
| 41 | + cwd: project.path, |
| 42 | + pattern: relative(project.path, project.targetLocation), |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + const { extraPatterns } = project.getCleanConfig(); |
| 47 | + if (extraPatterns) { |
| 48 | + toDelete.push({ |
| 49 | + cwd: project.path, |
| 50 | + pattern: extraPatterns, |
| 51 | + }); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Runs Bazel hard clean |
| 56 | + await runBazel(['clean', '--expunge']); |
| 57 | + log.success('Hard cleaned bazel'); |
| 58 | + |
| 59 | + // Deletes Bazel Cache Folders |
| 60 | + await del([await getBazelDiskCacheFolder(), await getBazelRepositoryCacheFolder()], { |
| 61 | + force: true, |
| 62 | + }); |
| 63 | + log.success('Removed disk caches'); |
| 64 | + |
| 65 | + if (toDelete.length === 0) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * In order to avoid patterns like `/build` in packages from accidentally |
| 71 | + * impacting files outside the package we use `process.chdir()` to change |
| 72 | + * the cwd to the package and execute `del()` without the `force` option |
| 73 | + * so it will check that each file being deleted is within the package. |
| 74 | + * |
| 75 | + * `del()` does support a `cwd` option, but it's only for resolving the |
| 76 | + * patterns and does not impact the cwd check. |
| 77 | + */ |
| 78 | + const originalCwd = process.cwd(); |
| 79 | + try { |
| 80 | + for (const { pattern, cwd } of toDelete) { |
| 81 | + process.chdir(cwd); |
| 82 | + const promise = del(pattern); |
| 83 | + |
| 84 | + if (log.wouldLogLevel('info')) { |
| 85 | + ora.promise(promise, relative(originalCwd, join(cwd, String(pattern)))); |
| 86 | + } |
| 87 | + |
| 88 | + await promise; |
| 89 | + } |
| 90 | + } finally { |
| 91 | + process.chdir(originalCwd); |
| 92 | + } |
| 93 | + }, |
| 94 | +}; |
0 commit comments