liveperson-functions-cli
Version:
LivePerson Functions CLI
76 lines • 3.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InitController = void 0;
// tslint:disable:no-shadowed-variable
const child_process_1 = require("child_process");
const path_1 = require("path");
const errorCodes_1 = require("../shared/errorCodes");
const file_service_1 = require("../service/file.service");
const init_view_1 = require("../view/init.view");
class InitController {
constructor(
/* istanbul ignore next */ { initView = new init_view_1.InitView(), exec = child_process_1.execSync, fileService = new file_service_1.FileService(), cwd = process.cwd(), } = {}) {
this.initView = initView;
this.exec = exec;
this.fileService = fileService;
this.cwd = cwd;
this.update = false;
}
/**
* Creates the default project structure and adds function folder depending on the passed names.
* @param {IInitOptions} - Passed function names
* @returns {Promise<void>} - init view
* @memberof InitController
*/
async init({ functionNames, update = false, } = {}) {
try {
this.update = update;
const packageManager = this.determinePackageManager();
const needDependencyInstallation = this.needDependencyInstallation(packageManager);
await this.initView.showInitialiseTaskList({
packageManager,
needDependencyInstallation,
functionNames,
update: this.update,
});
}
catch (error) {
/* istanbul ignore else */
if (error.name !== 'ListrError') {
this.initView.showErrorMessage(error.message || error.errorMsg);
}
}
}
determinePackageManager() {
const versionRegex = /(?:\d{1,2}.){2}\d{1,2}/;
try {
if (versionRegex.test(this.exec('npm -v', { encoding: 'utf8' }))) {
return 'npm';
}
if (versionRegex.test(this.exec('yarn -v', { encoding: 'utf8' }))) {
return 'yarn';
}
throw new Error('Please make sure you have npm or yarn installed');
}
catch {
const prettyError = {
message: 'Please make sure you have npm or yarn installed',
ref: 'https://yarnpkg.com/getting-started/install',
code: errorCodes_1.CLIErrorCodes.PackageManagerNotFound,
};
this.initView.showErrorMessage(prettyError);
throw new Error('exit');
}
}
needDependencyInstallation(packageManager) {
return (this.update ||
(packageManager === 'npm' &&
!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'bin', 'lp-faas-toolbelt', 'package-lock.json')) &&
!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'bin', 'lp-faas-toolbelt', 'node_modules'))) ||
(packageManager === 'yarn' &&
!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'bin', 'lp-faas-toolbelt', 'yarn.lock')) &&
!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'bin', 'lp-faas-toolbelt', 'node_modules'))));
}
}
exports.InitController = InitController;
//# sourceMappingURL=init.controller.js.map