Skip to content

Commit 18d0ffb

Browse files
authored
Merge pull request #151 from microservices-suite/repo-engineering/deletion_automation
Repo engineering/deletion automation
2 parents f1a00f5 + b2dc743 commit 18d0ffb

File tree

4 files changed

+314
-22
lines changed

4 files changed

+314
-22
lines changed

.suite-cli/cli/cli.js

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ const ora = require('ora')
44
const { Command } = require('commander');
55
const { createPromptModule } = require('inquirer');
66
const { execSync } = require('node:child_process')
7-
const actionHandlers = require('./scripts')
8-
const { logInfo, getExistingComponent, getExistingApps, getNextAvailablePort, scaffoldApp, scaffoldGateways } = require('./scripts/scripts.module');
97
const { cwd } = require('node:process');
10-
const program = new Command()
11-
const prompt = createPromptModule()
8+
const { readFileSync } = require('node:fs');
9+
const path = require('node:path');
10+
const { cwd } = require('node:process');
11+
const { readFileSync } = require('node:fs');
12+
const path = require('node:path');
13+
const actionHandlers = require('./scripts')
14+
const { logInfo, getExistingComponent, getExistingApps, getNextAvailablePort, scaffoldApp, scaffoldGateways, readFileContent } = require('./scripts/scripts.module');
15+
16+
const program = new Command();
17+
const packageJsonPath = path.join(__dirname, 'package.json');
18+
const packageJSON = JSON.parse(readFileSync(packageJsonPath, { 'encoding': 'utf8' }));
19+
program.version(packageJSON.version); //get library version set by release script
20+
const prompt = createPromptModule();
1221
program
1322
.command('add')
1423
.description('Adds dependencies at given workspace and updates package.json')
@@ -125,9 +134,9 @@ program
125134
.then(answers => {
126135
let existing_services = []
127136
try {
128-
existing_services = getExistingComponent({ key: 'services', currentDir: cwd() })
137+
existing_services = getExistingComponent({ key: 'services', currentDir: cwd() })
129138
} catch (error) {
130-
139+
131140
}
132141
switch (answers.resource) {
133142
case 'monorepo':
@@ -236,7 +245,7 @@ program
236245
]).then((answers) => actionHandlers.scaffoldNewLibrary({ answers: { ...answers, private: false } }))
237246
break
238247
case 'app':
239-
const existing_apps = getExistingComponent({ key: 'apps', currentDir: cwd() })||[]
248+
const existing_apps = getExistingComponent({ key: 'apps', currentDir: cwd() }) || []
240249
const formatServiceName = (service) => `${service.name}: ${service.port}`;
241250
prompt([
242251
{
@@ -300,7 +309,7 @@ program
300309
break;
301310
case 'gateway':
302311
const apps = getExistingApps({ currentDir: cwd() });
303-
if(!apps) {
312+
if (!apps) {
304313
logInfo({ message: `No apps found in this project. You need to have atleast one app to generate a gateway` })
305314
ora().info(`Run 'suite generate' to create one...`)
306315
process.exit(0);
@@ -356,16 +365,45 @@ program
356365
});
357366
});
358367
program
359-
.command('remove')
360-
.description('Clean remove a monorepo resource plus all the associated files. No residual files remain behind')
361-
.option('service', 'remove service and associated files')
362-
.option('app', 'remove app and associated files')
363-
.option('library', 'remove library and associated files')
364-
.option('microservice', 'remove microservice and associated files')
365-
.option('gateway', 'remove gateway and associated files')
366-
.action(async (options) => {
367-
console.log({options})
368-
await actionHandlers.dockerPrune({ options })
369-
});
368+
.command('remove <resource> [resource_name]')
369+
.description('Clean remove a monorepo resource or all resources of a specific type with the --all flag.')
370+
.option('-f, --force', 'Force removal without confirmation')
371+
.option('--all', 'Remove all resources of the specified type') // Add --all flag
372+
.action(async (resource, resource_name, options) => {
373+
const spinner = ora();
374+
375+
// Validate --all flag usage
376+
if (!resource_name && !options.all) {
377+
spinner.fail('You must provide either a resource name or the --all flag to remove all resources.');
378+
return;
379+
}
380+
381+
try {
382+
// Confirm the deletion if --force is not provided
383+
if (!options.force) {
384+
const { confirmRemoval } = await prompt([
385+
{
386+
type: 'confirm',
387+
name: 'confirmRemoval',
388+
message: `Are you sure you want to remove ${options.all ? `all ${resource}s` : `${resource} "${resource_name}"`} ?`,
389+
default: false
390+
}
391+
]);
392+
393+
if (!confirmRemoval) {
394+
spinner.info('Operation cancelled.');
395+
return;
396+
}
397+
}
398+
399+
// Call the resource removal handler
400+
await actionHandlers.removeResource({ answers: { resource, resource_name, options } });
401+
402+
} catch (error) {
403+
spinner.fail(`Failed to remove ${resource} ${resource_name || 'resources'}: ${error.message}`);
404+
}
405+
});
406+
407+
370408
program.parse(process.argv);
371409
module.exports = program
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
const { removeResource, logError } = require('../scripts.module')
3+
4+
module.exports = async ({ answers }) => {
5+
try {
6+
await removeResource({ answers });
7+
} catch (error) {
8+
logError({ error })
9+
}
10+
}

.suite-cli/cli/scripts/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ module.exports.releasePackage = require('./commands/releasePackage.cmd');
2323
module.exports.scaffoldNewService = require('./commands/scaffoldNewService.cmd');
2424
module.exports.scaffoldNewLibrary = require('./commands/scaffoldNewLibrary.cmd');
2525
module.exports.test = require('./commands/test.cmd');
26+
module.exports.removeResource = require('./commands/removeResource.cmd');

0 commit comments

Comments
 (0)