|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { tags } from '@angular-devkit/core'; |
| 10 | +import { promises as fs } from 'fs'; |
| 11 | +import { join } from 'path'; |
| 12 | +import { Argv } from 'yargs'; |
| 13 | +import { |
| 14 | + CommandModule, |
| 15 | + CommandModuleImplementation, |
| 16 | + CommandScope, |
| 17 | +} from '../../../command-builder/command-module'; |
| 18 | +import { isCI } from '../../../utilities/environment-options'; |
| 19 | +import { getCacheConfig } from '../utilities'; |
| 20 | + |
| 21 | +export class CacheInfoCommandModule extends CommandModule implements CommandModuleImplementation { |
| 22 | + command = 'info'; |
| 23 | + describe = 'Prints persistent disk cache configuration and statistics in the console.'; |
| 24 | + longDescriptionPath?: string | undefined; |
| 25 | + static override scope: CommandScope.In; |
| 26 | + |
| 27 | + builder(localYargs: Argv): Argv { |
| 28 | + return localYargs.strict(); |
| 29 | + } |
| 30 | + |
| 31 | + async run(): Promise<void> { |
| 32 | + const { path, environment, enabled } = getCacheConfig(this.context.workspace); |
| 33 | + |
| 34 | + this.context.logger.info(tags.stripIndents` |
| 35 | + Enabled: ${enabled ? 'yes' : 'no'} |
| 36 | + Environment: ${environment} |
| 37 | + Path: ${path} |
| 38 | + Size on disk: ${await this.getSizeOfDirectory(path)} |
| 39 | + Effective status on current machine: ${this.effectiveEnabledStatus() ? 'enabled' : 'disabled'} |
| 40 | + `); |
| 41 | + } |
| 42 | + |
| 43 | + private async getSizeOfDirectory(path: string): Promise<string> { |
| 44 | + const directoriesStack = [path]; |
| 45 | + let size = 0; |
| 46 | + |
| 47 | + while (directoriesStack.length) { |
| 48 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 49 | + const dirPath = directoriesStack.pop()!; |
| 50 | + let entries: string[] = []; |
| 51 | + |
| 52 | + try { |
| 53 | + entries = await fs.readdir(dirPath); |
| 54 | + } catch {} |
| 55 | + |
| 56 | + for (const entry of entries) { |
| 57 | + const entryPath = join(dirPath, entry); |
| 58 | + const stats = await fs.stat(entryPath); |
| 59 | + |
| 60 | + if (stats.isDirectory()) { |
| 61 | + directoriesStack.push(entryPath); |
| 62 | + } |
| 63 | + |
| 64 | + size += stats.size; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return this.formatSize(size); |
| 69 | + } |
| 70 | + |
| 71 | + private formatSize(size: number): string { |
| 72 | + if (size <= 0) { |
| 73 | + return '0 bytes'; |
| 74 | + } |
| 75 | + |
| 76 | + const abbreviations = ['bytes', 'kB', 'MB', 'GB']; |
| 77 | + const index = Math.floor(Math.log(size) / Math.log(1024)); |
| 78 | + const roundedSize = size / Math.pow(1024, index); |
| 79 | + // bytes don't have a fraction |
| 80 | + const fractionDigits = index === 0 ? 0 : 2; |
| 81 | + |
| 82 | + return `${roundedSize.toFixed(fractionDigits)} ${abbreviations[index]}`; |
| 83 | + } |
| 84 | + |
| 85 | + private effectiveEnabledStatus(): boolean { |
| 86 | + const { enabled, environment } = getCacheConfig(this.context.workspace); |
| 87 | + |
| 88 | + if (enabled) { |
| 89 | + switch (environment) { |
| 90 | + case 'ci': |
| 91 | + return isCI; |
| 92 | + case 'local': |
| 93 | + return !isCI; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return enabled; |
| 98 | + } |
| 99 | +} |
0 commit comments