@lenne.tech/cli
Version:
lenne.Tech CLI: lt
111 lines (110 loc) • 5.17 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
/**
* Reinitialize npm packages
*/
const NewCommand = {
alias: ['r'],
description: 'Reinstall npm packages',
hidden: false,
name: 'reinit',
run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
// Retrieve the tools we need
const { config, helper, npm, parameters, pm, print: { info, spin, success }, prompt, system, } = toolbox;
// Start timer
const timer = system.startTimer();
// Load configuration
const ltConfig = config.loadConfig();
const configUpdate = (_c = (_b = (_a = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _a === void 0 ? void 0 : _a.npm) === null || _b === void 0 ? void 0 : _b.reinit) === null || _c === void 0 ? void 0 : _c.update;
// Parse CLI arguments
const cliUpdate = parameters.options.update || parameters.options.u;
// Determine noConfirm with priority: CLI > command > global > default
const noConfirm = config.getNoConfirm({
cliValue: parameters.options.noConfirm,
commandConfig: (_e = (_d = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _d === void 0 ? void 0 : _d.npm) === null || _e === void 0 ? void 0 : _e.reinit,
config: ltConfig,
});
// Check
const { data, path } = yield npm.getPackageJson({ showError: true });
if (!path) {
return;
}
// Determine update with priority: CLI > config > interactive
let update;
if (cliUpdate !== undefined) {
// CLI parameter provided
update = cliUpdate === true || cliUpdate === 'true';
}
else if (configUpdate !== undefined) {
// Config value provided
update = configUpdate;
if (update) {
info('Using update setting from lt.config: true');
}
}
else if (noConfirm) {
// noConfirm mode without explicit update setting
update = false;
}
else {
// Interactive mode
update = yield prompt.confirm('Update package.json before reinitialization?');
}
if (update) {
if (!system.which('ncu')) {
const installSpin = spin('Install ncu');
yield system.run(pm.globalInstall('npm-check-updates'));
installSpin.succeed();
}
const updateSpin = spin('Update package.json');
yield system.run(`ncu -u --packageFile ${path}`);
updateSpin.succeed();
}
// Reinitialize
const detectedPm = pm.detect((0, path_1.dirname)(path));
if (data.scripts && data.scripts.reinit) {
const ownReinitSpin = spin('Reinitialize packages');
yield system.run(`cd ${(0, path_1.dirname)(path)} && ${pm.run('reinit', detectedPm)}`);
ownReinitSpin.succeed();
}
else {
const reinitSpin = spin('Reinitialize packages');
if (system.which('rimraf')) {
yield system.run(pm.globalInstall('rimraf'));
}
const lockfile = pm.getLockfileName(detectedPm);
yield system.run(`cd ${(0, path_1.dirname)(path)} && rimraf ${lockfile} && rimraf node_modules && ${pm.cacheClean(detectedPm)} && ${pm.install(detectedPm)}`);
reinitSpin.succeed();
if (data.scripts && data.scripts['test:e2e']) {
const testE2eSpin = spin('Run tests');
yield system.run(`cd ${(0, path_1.dirname)(path)} && ${pm.run('test:e2e', detectedPm)}`);
testE2eSpin.succeed();
}
else if (data.scripts && data.scripts && data.scripts.test) {
const testSpin = spin('Run tests');
yield system.run(`cd ${(0, path_1.dirname)(path)} && ${pm.run('test', detectedPm)}`);
testSpin.succeed();
}
}
// Success info
success(`Reinitialized npm packages in ${helper.msToMinutesAndSeconds(timer())}m.`);
// Exit if not running from menu
if (!toolbox.parameters.options.fromGluegunMenu) {
process.exit();
}
// For tests
return 'npm reinit';
}),
};
exports.default = NewCommand;