UNPKG

@rnv/engine-core

Version:
181 lines 12.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var path_1 = tslib_1.__importDefault(require("path")); var tar_1 = tslib_1.__importDefault(require("tar")); var core_1 = require("@rnv/core"); var common_1 = require("./common"); var taskOptions_1 = require("../../taskOptions"); var iocane = require('iocane'); var _unzipAndCopy = function (ctx, shouldCleanFolder, destTemp, wsPath, ts, destFolder) { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!shouldCleanFolder) return [3 /*break*/, 2]; return [4 /*yield*/, (0, core_1.cleanFolder)(wsPath)]; case 1: _a.sent(); _a.label = 2; case 2: return [4 /*yield*/, tar_1.default.x({ file: destTemp, cwd: ctx.paths.workspace.dir, })]; case 3: _a.sent(); (0, core_1.removeFilesSync)([destTemp]); if (ctx.files.project.package.name && (0, core_1.fsExistsSync)(ts)) { (0, core_1.copyFileSync)(ts, path_1.default.join(ctx.paths.workspace.dir, ctx.files.project.package.name, 'timestamp')); } (0, core_1.logSuccess)("Files successfully extracted into ".concat(destFolder)); return [2 /*return*/]; } }); }); }; exports.default = (0, core_1.createTask)({ description: 'Decrypt encrypted project files into local `~/<wokspace>/<project>/..`', dependsOn: [core_1.RnvTaskName.configureSoft], fn: function (_a) { var ctx = _a.ctx; return tslib_1.__awaiter(void 0, void 0, void 0, function () { var crypto, sourceRaw, projectName, envVar, source, ts, destFolder, destTemp, shouldCleanFolder, wsPath, isCryptoReset, options, option, confirm_1, key, data, e_1, err; var _b, _c, _d; return tslib_1.__generator(this, function (_e) { switch (_e.label) { case 0: crypto = (_b = ctx.files.project.config) === null || _b === void 0 ? void 0 : _b.crypto; sourceRaw = crypto === null || crypto === void 0 ? void 0 : crypto.path; projectName = (_c = ctx.files.project.config) === null || _c === void 0 ? void 0 : _c.projectName; if (!(!(crypto === null || crypto === void 0 ? void 0 : crypto.isOptional) && sourceRaw)) return [3 /*break*/, 12]; envVar = (0, common_1.getEnvVar)(); if (!projectName || !envVar) return [2 /*return*/]; source = "".concat((0, core_1.getRealPath)(sourceRaw, 'crypto.path')); ts = "".concat(source, ".timestamp"); destFolder = path_1.default.join(ctx.paths.workspace.dir, projectName); destTemp = "".concat(path_1.default.join(ctx.paths.workspace.dir, projectName.replace('/', '-')), ".tgz"); shouldCleanFolder = false; wsPath = path_1.default.join(ctx.paths.workspace.dir, projectName); isCryptoReset = ctx.command === 'crypto' && ctx.program.opts().reset === true; if (!(ctx.program.opts().ci !== true && !isCryptoReset && (0, core_1.fsExistsSync)(destFolder))) return [3 /*break*/, 2]; options = ['Yes - override (recommended)', 'Yes - merge', 'Skip']; return [4 /*yield*/, (0, core_1.inquirerPrompt)({ name: 'option', type: 'list', choices: options, message: "How to decrypt to ".concat((0, core_1.chalk)().bold.white(destFolder), " ?"), })]; case 1: option = (_e.sent()).option; if (option === options[0]) { shouldCleanFolder = true; } else if (option === options[2]) { return [2 /*return*/, true]; } return [3 /*break*/, 3]; case 2: shouldCleanFolder = true; _e.label = 3; case 3: if (!(0, core_1.fsExistsSync)(destTemp)) return [3 /*break*/, 6]; return [4 /*yield*/, (0, core_1.inquirerPrompt)({ type: 'confirm', message: "Found existing decrypted file at ".concat((0, core_1.chalk)().bold.white(destTemp), ". want to use it and skip decrypt ?"), })]; case 4: confirm_1 = (_e.sent()).confirm; if (!confirm_1) return [3 /*break*/, 6]; return [4 /*yield*/, _unzipAndCopy(ctx, shouldCleanFolder, destTemp, wsPath, ts, destFolder)]; case 5: _e.sent(); return [2 /*return*/, true]; case 6: key = ctx.program.opts().key || ctx.process.env[envVar]; if (!key) { return [2 /*return*/, Promise.reject("encrypt: You must pass ".concat((0, core_1.chalk)().bold.white('--key'), " or have env var defined:\n\n").concat((0, common_1.getEnvExportCmd)(envVar, 'REPLACE_WITH_ENV_VARIABLE'), "\n\nMake sure you take into account special characters that might need to be escaped.\n"))]; } if (!(0, core_1.fsExistsSync)(source)) { return [2 /*return*/, Promise.reject("Can't decrypt. ".concat((0, core_1.chalk)().bold.white(source), " is missing!"))]; } data = void 0; _e.label = 7; case 7: _e.trys.push([7, 9, , 10]); return [4 /*yield*/, iocane.createSession().use('cbc').decrypt((0, core_1.fsReadFileSync)(source), key)]; case 8: data = _e.sent(); return [3 /*break*/, 10]; case 9: e_1 = _e.sent(); if (e_1 instanceof Error) { if ((_d = e_1 === null || e_1 === void 0 ? void 0 : e_1.message) === null || _d === void 0 ? void 0 : _d.includes) { if (e_1.message.includes('Signature mismatch')) { err = "You're trying to decode crypto file encoded with previous version of crypto.\nthis change was introduced in \"rnv@0.29.0\"\n\n".concat(e_1, "\n\n ").concat((0, core_1.chalk)().green('SUGGESTION:'), "\n\n ").concat((0, core_1.chalk)().yellow('STEP 1:'), "\n run: ").concat((0, core_1.chalk)().bold.white('rnv crypto encrypt'), " locally at least once and commit the result back to your repository\n\n ").concat((0, core_1.chalk)().yellow('STEP 2:'), "\n you should be able to use: ").concat((0, core_1.chalk)().bold.white('rnv crypto decrypt'), " properly now\n\n ").concat((0, core_1.chalk)().yellow('IF ALL HOPE IS LOST:'), "\n Raise new issue and copy this SUMMARY box output at:\n ").concat((0, core_1.chalk)().bold.white('https://github.com/flexn-io/renative/issues'), "\n and we will try to help!\n\n "); return [2 /*return*/, Promise.reject(err)]; } if (e_1.message.includes('Authentication failed')) { return [2 /*return*/, Promise.reject("It seems like you provided invalid decryption key.\n\n".concat(e_1.stack, "\n\n").concat((0, core_1.chalk)().green('SUGGESTION:'), "\n\n").concat((0, core_1.chalk)().yellow('STEP 1:'), "\ncheck if your ENV VAR is correct: ").concat((0, common_1.getEnvExportCmd)(envVar, '***********'), "\nMake sure you take into account special characters that might need to be escaped\nor if someone did not encrypt ").concat((0, core_1.chalk)().bold.white(source), " with a different key\n\n").concat((0, core_1.chalk)().yellow('STEP 2:'), "\nrun crypto decrypt again\n\n").concat((0, core_1.chalk)().yellow('IF ALL HOPE IS LOST:'), "\nRaise new issue and copy this SUMMARY box output at:\n").concat((0, core_1.chalk)().bold.white('https://github.com/flexn-io/renative/issues'), "\nand we will try to help!\n\n"))]; } } } return [2 /*return*/, Promise.reject(e_1)]; case 10: (0, core_1.fsWriteFileSync)(destTemp, data); // try { // await executeAsync( // c, // `${_getOpenSllPath( // c // )} enc -aes-256-cbc -md md5 -d -in ${source} -out ${destTemp} -k ${key}`, // { privateParams: [key] } // ); // } catch (e) { // const cmd1 = chalk().bold( // `openssl enc -aes-256-cbc -md md5 -d -in ${source} -out ${destTemp} -k $${envVar}` // ); // return Promise.reject(`${e} // ${chalk().green('SUGGESTION:')} // ${chalk().yellow('STEP 1:')} // ${cmd1} // ${chalk().yellow('STEP 2:')} // ${chalk().bold( // 'run your previous command again and choose to skip openssl once asked' // )}`); // } return [4 /*yield*/, _unzipAndCopy(ctx, shouldCleanFolder, destTemp, wsPath, ts, destFolder)]; case 11: // try { // await executeAsync( // c, // `${_getOpenSllPath( // c // )} enc -aes-256-cbc -md md5 -d -in ${source} -out ${destTemp} -k ${key}`, // { privateParams: [key] } // ); // } catch (e) { // const cmd1 = chalk().bold( // `openssl enc -aes-256-cbc -md md5 -d -in ${source} -out ${destTemp} -k $${envVar}` // ); // return Promise.reject(`${e} // ${chalk().green('SUGGESTION:')} // ${chalk().yellow('STEP 1:')} // ${cmd1} // ${chalk().yellow('STEP 2:')} // ${chalk().bold( // 'run your previous command again and choose to skip openssl once asked' // )}`); // } _e.sent(); return [3 /*break*/, 13]; case 12: (0, core_1.logWarning)("You don't have {{ crypto.path }} specificed in ".concat((0, core_1.chalk)().bold.white(ctx.paths.appConfigBase))); return [2 /*return*/, true]; case 13: return [2 /*return*/]; } }); }); }, options: [taskOptions_1.TaskOptions.key], task: core_1.RnvTaskName.cryptoDecrypt, }); //# sourceMappingURL=taskCryptoDecrypt.js.map