UNPKG

@angular/cli

Version:
139 lines 5.49 kB
"use strict"; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheInfoCommandModule = void 0; const fs = __importStar(require("node:fs/promises")); const node_path_1 = require("node:path"); const command_module_1 = require("../../../command-builder/command-module"); const color_1 = require("../../../utilities/color"); const environment_options_1 = require("../../../utilities/environment-options"); const utilities_1 = require("../utilities"); class CacheInfoCommandModule extends command_module_1.CommandModule { command = 'info'; describe = 'Prints persistent disk cache configuration and statistics in the console.'; longDescriptionPath; scope = command_module_1.CommandScope.In; builder(localYargs) { return localYargs.strict(); } async run() { const cacheConfig = (0, utilities_1.getCacheConfig)(this.context.workspace); const { path, environment, enabled } = cacheConfig; const effectiveStatus = this.effectiveEnabledStatus(cacheConfig); const sizeOnDisk = await this.getSizeOfDirectory(path); const info = [ { label: 'Enabled', value: enabled ? color_1.colors.green('Yes') : color_1.colors.red('No'), }, { label: 'Environment', value: color_1.colors.cyan(environment), }, { label: 'Path', value: color_1.colors.cyan(path), }, { label: 'Size on disk', value: color_1.colors.cyan(sizeOnDisk), }, { label: 'Effective Status', value: (effectiveStatus ? color_1.colors.green('Enabled') : color_1.colors.red('Disabled')) + ' (current machine)', }, ]; const maxLabelLength = Math.max(...info.map((l) => l.label.length)); const output = info .map(({ label, value }) => color_1.colors.bold(label.padEnd(maxLabelLength + 2)) + `: ${value}`) .join('\n'); this.context.logger.info(`\n${color_1.colors.bold('Cache Information')}\n\n${output}\n`); } async getSizeOfDirectory(path) { const directoriesStack = [path]; let size = 0; while (directoriesStack.length) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const dirPath = directoriesStack.pop(); let entries = []; try { entries = await fs.readdir(dirPath); } catch { } for (const entry of entries) { const entryPath = (0, node_path_1.join)(dirPath, entry); const stats = await fs.stat(entryPath); if (stats.isDirectory()) { directoriesStack.push(entryPath); } size += stats.size; } } return this.formatSize(size); } formatSize(size) { if (size <= 0) { return '0 bytes'; } const abbreviations = ['bytes', 'kB', 'MB', 'GB']; const index = Math.floor(Math.log(size) / Math.log(1024)); const roundedSize = size / Math.pow(1024, index); // bytes don't have a fraction const fractionDigits = index === 0 ? 0 : 2; return `${roundedSize.toFixed(fractionDigits)} ${abbreviations[index]}`; } effectiveEnabledStatus(cacheConfig) { const { enabled, environment } = cacheConfig; if (enabled) { switch (environment) { case 'ci': return environment_options_1.isCI; case 'local': return !environment_options_1.isCI; } } return enabled; } } exports.CacheInfoCommandModule = CacheInfoCommandModule; //# sourceMappingURL=cli.js.map