piral-cli
Version:
The standard CLI for creating and building a Piral instance or a Pilet.
110 lines • 4.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.installDependencies = installDependencies;
exports.uninstallPackage = uninstallPackage;
exports.installPackage = installPackage;
exports.detectClient = detectClient;
exports.isProject = isProject;
exports.initProject = initProject;
exports.loginUser = loginUser;
exports.unpackPackage = unpackPackage;
exports.createPackage = createPackage;
exports.publishPackage = publishPackage;
exports.findSpecificVersion = findSpecificVersion;
exports.findTarball = findTarball;
exports.listPackage = listPackage;
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");
// Helpers:
function runNpmProcess(args, target, output) {
(0, log_1.log)('generalDebug_0003', 'Starting the npm process ...');
const cwd = (0, path_1.resolve)(process.cwd(), target);
return (0, scripts_1.runCommand)('npm', args, cwd, output);
}
// Client interface functions:
async function installDependencies(target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await runNpmProcess(['install', '--legacy-peer-deps', ...flags], target, ms);
(0, log_1.log)('generalDebug_0003', `npm install dependencies result: ${ms.value}`);
return ms.value;
}
async function uninstallPackage(packageRef, target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await runNpmProcess(['uninstall', packageRef, ...flags], target, ms);
(0, log_1.log)('generalDebug_0003', `npm uninstall package result: ${ms.value}`);
return ms.value;
}
async function installPackage(packageRef, target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await runNpmProcess(['install', packageRef, '--legacy-peer-deps', ...flags], target, ms);
(0, log_1.log)('generalDebug_0003', `npm install package result: ${ms.value}`);
return ms.value;
}
async function detectClient(root, stopDir = (0, path_1.resolve)(root, '/')) {
return !!(await (0, io_1.findFile)(root, 'package-lock.json', stopDir));
}
async function isProject(root, packageRef) {
const details = await listPackage(packageRef, root);
const packageDetails = details?.dependencies?.[packageRef];
if (packageDetails && typeof packageDetails.resolved === 'string') {
return packageDetails.resolved.startsWith('file:');
}
return false;
}
async function initProject(projectName, target) { }
// Functions to exclusively use from npm client:
async function loginUser() {
await runNpmProcess(['login'], '.');
}
async function unpackPackage(packageRef, target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await runNpmProcess(['pack', packageRef, ...flags], target, ms);
(0, log_1.log)('generalDebug_0003', `npm (un)pack result: ${ms.value}`);
return ms.value;
}
async function createPackage(target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await runNpmProcess(['pack', ...flags], target, ms);
(0, log_1.log)('generalDebug_0003', `npm pack result: ${ms.value}`);
return ms.value;
}
async function publishPackage(target = '.', file = '*.tgz', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await runNpmProcess(['publish', file, ...flags], target, ms);
(0, log_1.log)('generalDebug_0003', `npm publish result: ${ms.value}`);
return ms.value;
}
async function findSpecificVersion(packageName, version) {
const ms = new MemoryStream_1.MemoryStream();
try {
await runNpmProcess(['show', packageName, 'version', '--tag', version], '.', ms);
(0, log_1.log)('generalDebug_0003', `npm show result: ${ms.value}`);
return ms.value;
}
catch (ex) {
(0, log_1.log)('generalDebug_0003', `npm show result: ${ex}`);
return '';
}
}
async function findTarball(packageRef, target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
await runNpmProcess(['view', packageRef, 'dist.tarball', ...flags], target, ms);
(0, log_1.log)('generalDebug_0003', `npm view packageRef result: ${ms.value}`);
return ms.value;
}
async function listPackage(packageRef, target = '.', ...flags) {
const ms = new MemoryStream_1.MemoryStream();
try {
await runNpmProcess(['ls', packageRef, '--json', '--depth', '0', ...flags], target, ms);
}
catch (e) {
(0, log_1.log)('generalDebug_0003', `npm ls packageRef error: ${e}`);
return {};
}
(0, log_1.log)('generalDebug_0003', `npm ls packageRef result: ${ms.value}`);
return JSON.parse(ms.value);
}
//# sourceMappingURL=npm.js.map