piral-cli
Version:
The standard CLI for creating and building a Piral instance or a Pilet.
95 lines • 5.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.upgradePiletDefaults = void 0;
exports.upgradePilet = upgradePilet;
const path_1 = require("path");
const types_1 = require("../types");
const external_1 = require("../external");
const common_1 = require("../common");
exports.upgradePiletDefaults = {
version: undefined,
target: '.',
forceOverwrite: common_1.ForceOverwrite.no,
logLevel: types_1.LogLevels.info,
install: true,
npmClient: undefined,
variables: {},
cert: undefined,
allowSelfSigned: common_1.config.allowSelfSigned,
};
async function upgradePilet(baseDir = process.cwd(), options = {}) {
const { version = exports.upgradePiletDefaults.version, target = exports.upgradePiletDefaults.target, forceOverwrite = exports.upgradePiletDefaults.forceOverwrite, logLevel = exports.upgradePiletDefaults.logLevel, install = exports.upgradePiletDefaults.install, variables = exports.upgradePiletDefaults.variables, npmClient: defaultNpmClient = exports.upgradePiletDefaults.npmClient, cert = exports.upgradePiletDefaults.cert, allowSelfSigned = exports.upgradePiletDefaults.allowSelfSigned, } = options;
(0, common_1.ensure)('baseDir', baseDir, 'string');
(0, common_1.ensure)('variables', variables, 'object');
(0, common_1.ensure)('target', target, 'string');
const fullBase = (0, path_1.resolve)(process.cwd(), baseDir);
const root = (0, path_1.resolve)(fullBase, target);
(0, common_1.setLogLevel)(logLevel);
const valid = await (0, common_1.checkExistingDirectory)(root);
if (!valid) {
(0, common_1.fail)('invalidPiletTarget_0040');
}
const npmClient = await (0, common_1.determineNpmClient)(root, defaultNpmClient);
const ca = await (0, common_1.getCertificate)(cert);
const agent = (0, common_1.getAgent)({ ca, allowSelfSigned });
// in case we run from a user's CLI we want to allow updating
const interactive = (0, external_1.isInteractive)();
const { apps, piletPackage } = await (0, common_1.retrievePiletData)(root, undefined, agent, interactive);
const { devDependencies = {}, dependencies = {}, source } = piletPackage;
if (apps.length === 0) {
(0, common_1.fail)('appInstancesNotGiven_0012');
}
for (const { appPackage } of apps) {
//TODO distinguish if it's a website / remote emulator or an npm package
const sourceName = appPackage.name;
const language = /\.jsx?$/.test(source) ? 'js' : 'ts';
if (!sourceName || typeof sourceName !== 'string') {
(0, common_1.fail)('invalidPiletPackage_0042');
}
const currentVersion = devDependencies[sourceName] ?? dependencies[sourceName];
if (!currentVersion || typeof currentVersion !== 'string') {
(0, common_1.fail)('invalidPiralReference_0043');
}
const monorepoRef = await (0, common_1.isMonorepoPackageRef)(sourceName, npmClient);
const [packageRef] = await (0, common_1.getCurrentPackageDetails)(fullBase, sourceName, currentVersion, version, root);
const originalFiles = await (0, common_1.getFileStats)(root, sourceName);
if (!monorepoRef) {
// only install the latest if the shell does come from remote
(0, common_1.progress)(`Updating npm package to %s ...`, packageRef);
await (0, common_1.installNpmPackage)(npmClient, packageRef, root);
}
const piralInfo = await (0, common_1.readPiralPackage)(root, sourceName);
const isEmulator = (0, common_1.checkAppShellPackage)(piralInfo);
const { preUpgrade, postUpgrade, files } = (0, common_1.getPiletsInfo)(piralInfo);
if (preUpgrade) {
(0, common_1.progress)(`Running preUpgrade script ...`);
(0, common_1.log)('generalDebug_0003', `Run: ${preUpgrade}`);
await (0, common_1.runScript)(preUpgrade, root);
}
(0, common_1.progress)(`Taking care of templating ...`);
const data = (0, common_1.getPiletScaffoldData)(language, root, sourceName, variables);
if (isEmulator) {
// in the emulator case we get the files from the contained tarball
await (0, common_1.copyPiralFiles)(root, sourceName, piralInfo, forceOverwrite, data, originalFiles);
}
else {
// otherwise, we perform the same action as in the emulator creation
// just with a different target; not a created directory, but the root
const packageRoot = (0, common_1.getPiralPath)(root, sourceName);
const notOnceFiles = files.filter((m) => typeof m === 'string' || !m.once);
await (0, common_1.copyScaffoldingFiles)(packageRoot, root, notOnceFiles, piralInfo, data);
}
await (0, common_1.patchPiletPackage)(root, piralInfo, isEmulator, npmClient);
if (install) {
(0, common_1.progress)(`Updating dependencies ...`);
await (0, common_1.installNpmDependencies)(npmClient, root);
}
if (postUpgrade) {
(0, common_1.progress)(`Running postUpgrade script ...`);
(0, common_1.log)('generalDebug_0003', `Run: ${postUpgrade}`);
await (0, common_1.runScript)(postUpgrade, root);
}
}
(0, common_1.logDone)('Pilet upgraded successfully!');
}
//# sourceMappingURL=upgrade-pilet.js.map