@axway/axway-cli-pm
Version:
Package manager for Axway products
113 lines (100 loc) • 2.86 kB
JavaScript
import { createTable, loadConfig } from '@axway/amplify-cli-utils';
import snooplogg from 'snooplogg';
import { listPurgable, uninstallPackage } from '../pm.js';
import { runListr } from '../utils.js';
import 'fs-extra';
import 'npm-package-arg';
import 'libnpmsearch';
import 'pacote';
import 'path';
import 'promise-limit';
import 'semver';
import 'cross-spawn';
import 'which';
import 'events';
import '@axway/amplify-utils';
import 'listr2';
import 'cli-kit';
var purge = {
args: [
{
name: 'package',
desc: 'Name of the package to purge old versions for',
redact: false
}
],
desc: 'Removes all non-active, managed packages',
options: {
'--json': {
callback: ({ ctx, value }) => ctx.jsonMode = value,
desc: 'Outputs the purged packages as JSON'
},
'-y, --yes': {
aliases: [ '--no-prompt' ],
desc: 'Automatic yes to prompts and run non-interactively'
}
},
skipExtensionUpdateCheck: true,
async action({ argv, cli, console, terminal }) {
const { bold, highlight } = snooplogg.styles;
const purgeTable = createTable();
const purgable = await listPurgable(argv.package);
const removedPackages = {};
const tasks = [];
// step 1: determine packages to remove
for (const [ name, versions ] of Object.entries(purgable)) {
for (const pkg of versions) {
tasks.push({
title: `Purging ${highlight(`${name}@${pkg.version}`)}`,
task: async (ctx, task) => {
await uninstallPackage(pkg.path);
task.title = `Purged ${highlight(`${name}@${pkg.version}`)}`;
}
});
purgeTable.push([ ` ${bold(name)}`, pkg.version ]);
if (!removedPackages[name]) {
removedPackages[name] = [];
}
removedPackages[name].push(pkg);
}
}
if (!purgeTable.length) {
if (argv.json) {
console.log(JSON.stringify(removedPackages, null, 2));
} else {
console.log('There are no packages to purge.');
}
return;
}
// step 2: confirm purge
console.log(`The following packages can be purged:\n\n${purgeTable.toString()}\n`);
if (terminal.stdout.isTTY && !argv.yes && !argv.json) {
await new Promise(resolve => {
terminal.once('keypress', str => {
terminal.stderr.cursorTo(0);
terminal.stderr.clearLine();
if (str === 'y' || str === 'Y') {
return resolve();
}
process.exit(0);
});
terminal.stderr.write('Do you want to update? (y/N) ');
});
}
// step 3: run the tasks
try {
await runListr({ console, json: argv.json, tasks });
} catch (err) {
// errors are stored in the results
}
const cfg = await loadConfig();
await cfg.delete('update.notified');
await cfg.save();
if (argv.json) {
console.log(JSON.stringify(removedPackages, null, 2));
}
await cli.emitAction('axway:pm:purge', removedPackages);
}
};
export { purge as default };
//# sourceMappingURL=purge.js.map