Skip to content

Commit

Permalink
chore: Refactor clean command to have individual cleanup contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Apr 1, 2024
1 parent 3133e4f commit e2a5fd3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
19 changes: 10 additions & 9 deletions src/commands/clean/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@ import sharedOptions from '../../utils/shared-options';

export const cleanDepsCommand = () => {
const command = new Command();
const config = projectConfig();

command
.name('deps')
.summary('Cleanup DEMS-managed repositories dependencies')
.summary('Cleanup repositories dependencies (node_modules).')
.description('Cleans all repos dependencies locally installed.')
.addOption(sharedOptions.force().default(false))
.addOption(sharedOptions.force())
.action(async (options) => {
const config = projectConfig();
log.info(
'Cleaning all local dependencies for configured applications...',
);

if (options.force) {
log.info('User interactivity disabled due to --force flag.');
}
log.warning('User interactivity disabled due to --force flag.');
};

for (const repo in config.paths.repos) {
for (const repo of Object.values(config.paths.repos)) {
await deletePath({
path: `${config.paths.repos[repo]}/node_modules`,
path: `${repo}/node_modules`,
force: options.force,
});
log.success(`Dependencies clean task completed for ${repo}.`);
}

log.success('Dependencies cleanup task completed.');
});

return command;
Expand Down
37 changes: 37 additions & 0 deletions src/commands/clean/dot-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Command } from 'commander';
import { projectConfig } from '../../config/project';
import { deletePath } from '../../utils/file-system';
import log from '../../utils/log';
import sharedOptions from '../../utils/shared-options';

export const cleanDotEnvCommand = () => {
const command = new Command();
command
.name('dot-env')
.summary('Cleanup repositories dot env files (.env).')
.description(
'Removes the dot env files (.env) for all the DEMS-managed repositories configured.',
)
.addOption(sharedOptions.force())
.action(async (options) => {
const config = projectConfig();
log.info('Removing dot env files for managed repositories...');

if (options.force) {
log.warning('User interactivity disabled due to --force flag.');
}

for (const repo of Object.values(config.paths.repos)) {
await deletePath({
path: `${repo}/.env`,
force: options.force,
});
}

log.success('dot env files (.env) removed for managed repositories.');
});

return command;
};

export default cleanDotEnvCommand();
24 changes: 2 additions & 22 deletions src/commands/clean/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Command } from 'commander';
import { projectConfig } from '../../config/project';
import { deletePath } from '../../utils/file-system';
import log from '../../utils/log';
import sharedOptions from '../../utils/shared-options';
import { cleanDepsCommand } from './deps';
import { cleanDotEnvCommand } from './dot-env';

export const cleanCommand = () => {
const command = new Command();
Expand All @@ -17,26 +16,7 @@ export const cleanCommand = () => {
'by DEMS. Resets your local development environment.',
)
.addCommand(cleanDepsCommand())
.addOption(sharedOptions.force().default(false))
.addOption(sharedOptions.reposRoot().default(config.paths.repos_root))
.option('-e, --env-file <path>', '.env file', config.paths.repos_root)
.action(async (options) => {
log.info('Cleaning DEMS-related resources...');
if (options.force) {
log.info('User interactivity disabled due to --force flag.');
}

await deletePath({
path: options.reposRoot,
force: options.force,
});
await deletePath({
path: options.envFile,
force: options.force,
});

log.success('Clean completed for current project.');
});
.addCommand(cleanDotEnvCommand());

return command;
};
Expand Down

0 comments on commit e2a5fd3

Please sign in to comment.