Skip to content

Commit

Permalink
chore: Add repos cleanup sub-command and simplify git clone/checkout …
Browse files Browse the repository at this point in the history
…commands
  • Loading branch information
angelmadames committed Apr 1, 2024
1 parent dead7e1 commit da313c5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 25 deletions.
11 changes: 8 additions & 3 deletions src/commands/clean/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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;
};
Expand Down
51 changes: 51 additions & 0 deletions src/commands/clean/repos.ts
Original file line number Diff line number Diff line change
@@ -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();
20 changes: 9 additions & 11 deletions src/commands/git/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
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();
const config = projectConfig();

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,
});
}
Expand Down
25 changes: 14 additions & 11 deletions src/commands/git/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ 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();
const config = projectConfig();

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,
});
}
Expand Down

0 comments on commit da313c5

Please sign in to comment.