From 7e5759f8a2cceb84194c4ab0f0f2c72223da6f1c Mon Sep 17 00:00:00 2001 From: p-sw Date: Thu, 6 Jun 2024 22:09:26 +0900 Subject: [PATCH] feat(cli): add list command This adds a new 'list' command in the CLI package to print all available components in the registry as well as components installed in the project. This command includes additional options to include component file URL and specify a custom path to the config file. --- packages/cli/src/commands/list.ts | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 packages/cli/src/commands/list.ts diff --git a/packages/cli/src/commands/list.ts b/packages/cli/src/commands/list.ts new file mode 100644 index 0000000..520b078 --- /dev/null +++ b/packages/cli/src/commands/list.ts @@ -0,0 +1,51 @@ +import {Command, Flags} from '@oclif/core' +import {getAvailableComponentNames, getRegistry, getComponentURL} from '../helpers/registry.js' +import ora from 'ora' +import treeify from 'treeify' +import {CONFIG_DEFAULT_PATH} from '../const.js' +import {loadConfig, validateConfig} from '../helpers/config.js' +import {getComponentsInstalled} from '../helpers/path.js' + +export default class List extends Command { + static override description = 'Prints all available components in registry and components installed in this project' + + static override examples = ['<%= config.bin %> <%= command.id %>'] + + static override flags = { + url: Flags.boolean({char: 'u', description: 'include component file URL'}), + config: Flags.string({char: 'p', description: 'path to config', default: CONFIG_DEFAULT_PATH}), + } + + public async run(): Promise { + const {flags} = await this.parse(List) + + const registrySpinner = ora('Fetching registry...') + const getInstalledSpinner = ora('Getting installed components...') + + const loadedConfig = await validateConfig(await loadConfig(flags.config)) + + registrySpinner.start() + const registry = await getRegistry() + registrySpinner.succeed(`Fetched ${Object.keys(registry.components).length} components.`) + + const names = await getAvailableComponentNames(registry) + + getInstalledSpinner.start() + const installedNames = await getComponentsInstalled(names, loadedConfig) + getInstalledSpinner.succeed(`Got ${installedNames.length} installed components.`) + + let final: Record = {} + for (const name of names) { + const installed = installedNames.includes(name) ? 'yes' : 'no' + if (flags.url) { + const url = await getComponentURL(registry, name) + final = {...final, [name]: {URL: url, installed}} + } else { + final = {...final, [name]: {installed}} + } + } + + this.log('AVAILABLE COMPONENTS') + this.log(treeify.asTree(final, true, true)) + } +}