@semo/cli
Version:
A command line tools dispatcher
122 lines • 5.75 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.builder = exports.aliases = exports.desc = exports.command = exports.disabled = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const core_1 = require("@semo/core");
const rimraf_1 = require("rimraf");
const shelljs_1 = __importDefault(require("shelljs"));
const inquirer_1 = __importDefault(require("inquirer"));
exports.disabled = false; // Set to true to disable this command temporarily
// export const plugin = '' // Set this for importing plugin config
exports.command = 'cleanup [type]';
exports.desc = 'Cleanup internal caches.';
exports.aliases = 'clean';
// export const middleware = (argv) => {}
const builder = function (yargs) {
yargs.option('yes', { describe: 'Confirm to cleanup.', alias: 'y' });
// yargs.commandDir('cleanup')
};
exports.builder = builder;
const handler = function (argv) {
return __awaiter(this, void 0, void 0, function* () {
const appConfig = core_1.Utils.getApplicationConfig();
const scriptName = argv.scriptName || 'semo';
let coreCleanupSteps, coreCleanupStepKeys;
let cleanupSteps = {};
if (process.env.HOME) {
coreCleanupSteps = {
cache: path_1.default.resolve(process.env.HOME, '.' + scriptName, 'cache'),
'home-plugin-cache': path_1.default.resolve(process.env.HOME, '.' + scriptName, 'home-plugin-cache'),
'run-plugin-cache': path_1.default.resolve(process.env.HOME, '.' + scriptName, 'run-plugin-cache'),
'repl-package-cache': path_1.default.resolve(process.env.HOME, '.' + scriptName, 'repl-package-cache'),
'repl-history': path_1.default.resolve(process.env.HOME, '.' + scriptName, `.${scriptName}_repl_history`),
'shell-history': path_1.default.resolve(process.env.HOME, '.' + scriptName, `.${scriptName}_shell_history`),
};
Object.keys(coreCleanupSteps).forEach(key => {
if (fs_extra_1.default.existsSync(coreCleanupSteps[key])) {
cleanupSteps[key] = coreCleanupSteps[key];
}
});
coreCleanupStepKeys = Object.keys(coreCleanupSteps).push('all');
}
// Limit only application can hook cleanup
const hookAppCleanup = yield core_1.Utils.invokeHook(`${scriptName}:cleanup`, { include: ['application'] });
Object.keys(hookAppCleanup).forEach(key => {
let cachePath = hookAppCleanup[key];
if (cachePath.indexOf(appConfig.applicationDir) > -1 &&
coreCleanupStepKeys.indexOf(key) === -1) {
cleanupSteps[key] = hookAppCleanup[key];
}
});
if (!argv.type) {
const choices = Object.keys(cleanupSteps)
.map(key => {
return {
name: key +
' ' +
shelljs_1.default
.exec(`du -sh ${cleanupSteps[key]}`, { silent: true })
.split('\t')[0],
value: key,
};
})
.concat([{ name: '** CLEANUP ALL ABOVE **', value: 'all' }]);
const answers = yield inquirer_1.default.prompt([
{
type: 'list',
name: 'selected',
message: `Please choose a cache type to cleanup(remove):`,
choices: choices,
},
]);
argv.type = answers.selected;
}
else {
let availableTypes = Object.keys(cleanupSteps).concat(['all']);
if (availableTypes.indexOf(argv.type) === -1) {
core_1.Utils.error('Invalid cleanup type.');
return;
}
}
if (!argv.yes) {
let confirm = yield inquirer_1.default.prompt([
{
type: 'confirm',
name: 'confirmed',
message: core_1.Utils.warn(`Confirm cleanup ${argv.type}? The operation can not be reversed!`),
default: false,
},
]);
argv.yes = confirm.confirmed;
}
if (argv.yes) {
for (let key of Object.keys(cleanupSteps)) {
if (argv.type === key || argv.type === 'all') {
yield rimraf_1.rimraf.sync(cleanupSteps[key], {
glob: false,
});
core_1.Utils.success(`${key} has been cleanup!`);
}
}
}
else {
core_1.Utils.info('Nothing has been cleanup!');
}
return false;
});
};
exports.handler = handler;
//# sourceMappingURL=cleanup.js.map