piral-cli
Version:
The standard CLI for creating and building a Piral instance or a Pilet.
111 lines • 4.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.installDependencies = installDependencies;
exports.uninstallPackage = uninstallPackage;
exports.installPackage = installPackage;
exports.detectClient = detectClient;
exports.initProject = initProject;
exports.isProject = isProject;
exports.listProjects = listProjects;
const path_1 = require("path");
const log_1 = require("../common/log");
const io_1 = require("../common/io");
const scripts_1 = require("../common/scripts");
const MemoryStream_1 = require("../common/MemoryStream");
const external_1 = require("../external");
// Helpers:
const rushJson = 'rush.json';
async function runRushProcess(args, target, output) {
(0, log_1.log)('generalDebug_0003', 'Starting the Rush process ...');
const cwd = (0, path_1.resolve)(process.cwd(), target);
return await (0, scripts_1.runCommand)('rush', args, cwd, output);
}
async function tryRunRushProcess(args, target, output) {
try {
return await runRushProcess(args, target, output);
}
catch (err) {
(0, log_1.log)('generalInfo_0000', output.value || `rush failed due to ${err}`);
throw err;
}
}
function convert(flags) {
return flags.map((flag) => {
switch (flag) {
case '--save-exact':
// discard as this may lead to problems
return '';
case '--save-dev':
return '--dev';
case '--no-save':
// unfortunately no
return '';
default:
return flag;
}
});
}
// Client interface functions:
async function installDependencies(target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await tryRunRushProcess(['update', ...convert(flags)], target, ms);
(0, log_1.log)('generalDebug_0003', `Rush install dependencies result: ${ms.value}`);
return ms.value;
}
async function uninstallPackage(packageRef, target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await tryRunRushProcess(['remove', packageRef, ...convert(flags)], target, ms);
(0, log_1.log)('generalDebug_0003', `Rush remove package result: ${ms.value}`);
return ms.value;
}
async function installPackage(packageRef, target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await tryRunRushProcess(['add', '--package', packageRef, ...convert(flags)], target, ms);
(0, log_1.log)('generalDebug_0003', `Rush add package result: ${ms.value}`);
return ms.value;
}
async function detectClient(root, stopDir = (0, path_1.resolve)(root, '/')) {
return !!(await (0, io_1.findFile)(root, rushJson, stopDir));
}
async function initProject(packageName, target) {
const rushPath = await (0, io_1.findFile)(target, rushJson);
if (!rushPath) {
throw new Error(`Could not find the "${rushJson}" from "${target}". Sure you want to create the project in the right directory?`);
}
const rushDir = (0, path_1.dirname)(rushPath);
const rushContent = await (0, io_1.readText)(rushDir, rushJson);
const rushData = external_1.jju.parse(rushContent);
const projectFolder = (0, path_1.relative)(rushDir, target);
if (!Array.isArray(rushData.projects)) {
rushData.projects = [];
}
rushData.projects.push({
packageName,
projectFolder,
});
await (0, io_1.writeText)(rushDir, rushJson, external_1.jju.update(rushContent, rushData, {
mode: 'cjson',
indent: 2,
}));
}
async function isProject(root, packageRef) {
const details = await listProjects(root);
if (typeof details === 'object' && Array.isArray(details?.projects)) {
return details?.projects?.some((p) => p.name === packageRef) ?? false;
}
return false;
}
// Functions to exclusively use from rush client:
async function listProjects(target) {
const ms = new MemoryStream_1.MemoryStream();
try {
await runRushProcess(['list', '--json'], target, ms);
(0, log_1.log)('generalDebug_0003', `rush list project result: ${ms.value}`);
return JSON.parse(ms.value);
}
catch (e) {
(0, log_1.log)('generalDebug_0003', `rush list error: ${e}`);
return {};
}
}
//# sourceMappingURL=rush.js.map