liveperson-functions-cli
Version:
LivePerson Functions CLI
108 lines • 5.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PushController = void 0;
const faasFactory_service_1 = require("../service/faasFactory.service");
const file_service_1 = require("../service/file.service");
const push_view_1 = require("../view/push.view");
class PushController {
constructor(
/* istanbul ignore next */ { pushView = new push_view_1.PushView(), fileService = new file_service_1.FileService(), } = {}) {
this.pushView = pushView;
this.fileService = fileService;
}
/**
* Depending on the set flags this method optionally triggers push confirmations prompts
* to the user and accordingly triggers the push process itself. The Method surpresses
* Errors from the Listr Tasklist to keep the output clean.
* @param {IPushConfig} { lambdaFunctions, inputFlags } Input contains the names of the
* lambdas to push and the flags that have been set by the user.
* @returns {Promise<void>}
* @memberof PushController
*/
async push({ lambdaFunctions, inputFlags, }) {
var _a;
try {
if (inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.all) {
lambdaFunctions = this.fileService.getFunctionsDirectories();
}
const localLambdaInformation = this.fileService.collectLocalLambdaInformation(lambdaFunctions);
const localLambdaNames = localLambdaInformation.map((e) => e.name);
const faasService = await faasFactory_service_1.factory.get();
const allLambdas = await faasService.getLambdasByNames(localLambdaNames, true);
const allLambdaBodies = await Promise.all(allLambdas.map(async (lambda) => {
const lambdaConfig = this.fileService.getFunctionConfig(lambda.name);
if (Object.prototype.hasOwnProperty.call(lambda, 'uuid')) {
return this.createUpdateLambdaBody(lambda, lambdaConfig);
}
return this.createNewLambdaBody(lambdaConfig);
}));
if (inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags.yes) {
await this.pushView.showPushProcess({
pushRequestBodies: allLambdaBodies,
noWatch: inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags['no-watch'],
});
}
else {
const answers = await this.pushView.askForConfirmation(allLambdaBodies, faasService.accountId);
const confirmedLambdaBodies = allLambdaBodies.filter((entry) => answers[entry.name]);
if (confirmedLambdaBodies.length === 0) {
return;
}
await this.pushView.showPushProcess({
pushRequestBodies: confirmedLambdaBodies,
noWatch: inputFlags === null || inputFlags === void 0 ? void 0 : inputFlags['no-watch'],
});
}
}
catch (error) {
if (error.name !== 'ListrError') {
this.pushView.showErrorMessage(error.message || ((_a = error.error) === null || _a === void 0 ? void 0 : _a.errorMsg));
}
throw new Error(error);
}
}
async createNewLambdaBody(lambdaConfig) {
const faasService = await faasFactory_service_1.factory.get();
return {
...(lambdaConfig.event &&
lambdaConfig.event !== 'No Event' && { eventId: lambdaConfig.event }),
uuid: '',
version: -1,
name: lambdaConfig.name,
description: lambdaConfig.description,
state: 'Draft',
runtime: await faasService.getRuntime(),
implementation: {
code: this.fileService.read(this.fileService.getPathToFunction(lambdaConfig.name, 'index.js'), false),
dependencies: lambdaConfig.dependencies || [],
environmentVariables: lambdaConfig.environmentVariables[0].key === ''
? []
: lambdaConfig.environmentVariables,
},
};
}
createUpdateLambdaBody(lambda, lambdaConfig) {
return {
uuid: lambda.uuid,
version: lambda.version,
state: lambda.state === 'Draft' ? 'Draft' : 'Modified',
name: lambda.name,
eventId: lambda.eventId,
description: lambdaConfig.description,
runtime: lambda.runtime,
createdBy: lambda.createdBy,
updatedBy: lambda.updatedBy,
createdAt: lambda.createdAt,
updatedAt: lambda.updatedAt,
implementation: {
code: this.fileService.read(this.fileService.getPathToFunction(lambda.name, 'index.js'), false),
dependencies: lambdaConfig.dependencies || [],
environmentVariables: lambdaConfig.environmentVariables[0].key === ''
? []
: lambdaConfig.environmentVariables,
},
};
}
}
exports.PushController = PushController;
//# sourceMappingURL=push.controller.js.map