From da313c5c64e6fe9754c57ad78c2c435c7ef45fe2 Mon Sep 17 00:00:00 2001 From: "Angel M. Adames" Date: Mon, 1 Apr 2024 19:08:34 -0400 Subject: [PATCH] chore: Add repos cleanup sub-command and simplify git clone/checkout commands --- src/commands/clean/index.ts | 11 +++++--- src/commands/clean/repos.ts | 51 ++++++++++++++++++++++++++++++++++++ src/commands/git/checkout.ts | 20 +++++++------- src/commands/git/clone.ts | 25 ++++++++++-------- 4 files changed, 82 insertions(+), 25 deletions(-) create mode 100644 src/commands/clean/repos.ts diff --git a/src/commands/clean/index.ts b/src/commands/clean/index.ts index 26ef799..7a4629d 100644 --- a/src/commands/clean/index.ts +++ b/src/commands/clean/index.ts @@ -1,7 +1,9 @@ import { Command } from 'commander'; import { projectConfig } from '../../config/project'; +import { noIndent } from '../../utils/string'; import { cleanDepsCommand } from './deps'; import { cleanDotEnvCommand } from './dot-env'; +import { cleanReposCommand } from './repos'; export const cleanCommand = () => { const command = new Command(); @@ -11,11 +13,14 @@ export const cleanCommand = () => { .name('clean') .summary('Cleanup DEMS-managed resources') .description( - 'Cleans all configured resources and services initialized\n' + - 'by DEMS. Resets your local development environment.', + noIndent(` + Cleans all configured resources and services initialized + by DEMS. Resets your local development environment. + `), ) .addCommand(cleanDepsCommand()) - .addCommand(cleanDotEnvCommand()); + .addCommand(cleanDotEnvCommand()) + .addCommand(cleanReposCommand()); return command; }; diff --git a/src/commands/clean/repos.ts b/src/commands/clean/repos.ts new file mode 100644 index 0000000..1478103 --- /dev/null +++ b/src/commands/clean/repos.ts @@ -0,0 +1,51 @@ +import chalk from 'chalk'; +import { Command } from 'commander'; +import cliConfig from '../../config/cli'; +import { projectConfig } from '../../config/project'; +import { deletePath } from '../../utils/file-system'; +import log from '../../utils/log'; +import sharedOptions from '../../utils/shared-options'; +import { noIndent } from '../../utils/string'; + +export const cleanReposCommand = () => { + const command = new Command(); + command + .name('repos') + .summary("Remove the current project's git repositories.") + .description( + noIndent(` + Removes the current project's git repositories cloned in the repository + root path managed by DEMS. This action is irreversible. + `), + ) + .addOption(sharedOptions.force()) + .action(async (options) => { + const config = projectConfig(); + log.info("Removing the current project's git repositories..."); + log.info(`Current project: ${chalk.bold(cliConfig.currentProject)}`); + + if (options.force) { + log.warning('User interactivity disabled due to --force flag.'); + } + + log.warning('The repositories that will be removed are:'); + for (const repo of config.repositories) { + log.warning(` - ${repo}`); + } + + for (const repo of Object.values(config.paths.repos)) { + await deletePath({ + path: repo, + force: options.force, + }); + } + + log.success( + 'All managed git repositories directories have been removed.', + ); + }); + + return command; +}; + +export default cleanReposCommand(); diff --git a/src/commands/git/checkout.ts b/src/commands/git/checkout.ts index 3887f92..762b77b 100644 --- a/src/commands/git/checkout.ts +++ b/src/commands/git/checkout.ts @@ -1,8 +1,8 @@ -import chalk from 'chalk'; import { Command } from 'commander'; import { projectConfig } from '../../config/project'; import git from '../../utils/git'; import sharedOptions from '../../utils/shared-options'; +import { noIndent } from '../../utils/string'; export const gitCheckoutCommand = () => { const command = new Command(); @@ -10,21 +10,19 @@ export const gitCheckoutCommand = () => { command .name('checkout') + .summary('Checkout all repositories to a specific git branch or tag.') .description( - 'Checkout all configured git repositories defined in the config.json\n' + - ', to the specified git ref.', + noIndent(` + Checkout to an specific git ref (branch, tag) to all managed + repositories. The 'git checkout' command will be run in all + repositories defined in the config.json file of the current project. + `), ) .addOption(sharedOptions.gitRef().default(config.git.default_ref)) - .addOption(sharedOptions.reposRoot().default(config.paths.repos_root)) - .addOption(sharedOptions.repos().default(config.repositories)) .action((options) => { - console.log(`Git org > ${chalk.bold(options.gitOrg)}`); - console.log(`Git ref > ${chalk.bold(options.gitRef)}`); - console.log(`Repos path > ${chalk.bold(options.reposRoot)}`); - - for (const repo of options.repos) { + for (const repoPath of Object.values(config.paths.repos)) { git.checkout({ - path: `${options.reposRoot}/${repo}`, + path: repoPath, ref: options.gitRef, }); } diff --git a/src/commands/git/clone.ts b/src/commands/git/clone.ts index e483bdb..a1a000f 100644 --- a/src/commands/git/clone.ts +++ b/src/commands/git/clone.ts @@ -2,7 +2,9 @@ import chalk from 'chalk'; import { Command } from 'commander'; import { projectConfig } from '../../config/project'; import git from '../../utils/git'; +import log from '../../utils/log'; import sharedOptions from '../../utils/shared-options'; +import { noIndent } from '../../utils/string'; export const gitCloneCommand = () => { const command = new Command(); @@ -10,24 +12,25 @@ export const gitCloneCommand = () => { command .name('clone') + .summary('Clones all managed repositories using the specified git ref.') .description( - 'Clones all configured git repositories defined in the config.json\n' + - "file in the root directory. Uses 'git clone' strategy.", + noIndent(` + Clones all managed git repositories. The 'git clone' command will be + run for each repository defined in the config.json file of the current + project. + `), ) .addOption(sharedOptions.gitRef().default(config.git.default_ref)) - .addOption(sharedOptions.reposRoot().default(config.paths.repos_root)) - .addOption(sharedOptions.gitOrg().default(config.git.org_url)) - .addOption(sharedOptions.repos().default(config.repositories)) .action((options) => { - console.log(`Git org > ${chalk.bold(options.gitOrg)}`); - console.log(`Git ref > ${chalk.bold(options.gitRef)}`); - console.log(`Repos path > ${chalk.bold(options.reposRoot)}`); + log.info(`Git Org > ${chalk.bold(config.git.org_url)}`); + log.info(`Git Ref > ${chalk.bold(options.gitRef)}`); + log.info(`Repos Path > ${chalk.bold(config.paths.repos_root)}`); - for (const repo of options.repos) { - const repoUrl = `${options.gitOrg}/${repo}`; + for (const repo of config.repositories) { + const repoUrl = `${config.git.org_url}/${repo}`; git.clone({ ref: options.gitRef, - path: options.reposRoot, + path: config.paths.repos_root, repo: repoUrl, }); }