UNPKG

liveperson-functions-cli

Version:
126 lines 5.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultStructureService = void 0; const path_1 = require("path"); const file_service_1 = require("./file.service"); class DefaultStructureService { constructor({ fileService = new file_service_1.FileService(), cwd = process.cwd(), dirname = __dirname, } = {}) { this.fileService = fileService; this.cwd = cwd; this.functionName = ''; this.dirname = dirname; } /** * Copies all files from the bin folder of the cli to the folder of the user. * @param {string} [functionName=''] - creates the necessary files for the init command * @memberof DefaultStructureService */ // eslint-disable-next-line complexity create(functionName, update) { if (update) { this.createDefaultServices(); } else { if (functionName && this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'functions', functionName))) { throw new Error(`Folder with same name already exists (${functionName})`); } else { this.createFunctionsFolder(functionName); } /* istanbul ignore else */ if (!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'README.md'))) { this.createReadme(); } /* istanbul ignore else */ if (!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'bin'))) { this.createDefaultServices(); } /* istanbul ignore else */ if (!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, '.vscode'))) { this.copyVsCodeSettings(); } /* istanbul ignore else */ if (!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, '.idea'))) { this.copyIntellijSettings(); } /* istanbul ignore else */ if (!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, '.gitignore'))) { this.copyGitIgnore(); } /* istanbul ignore else */ if (!this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'functions', 'settings.json'))) { this.copySettings(); } /* istanbul ignore else */ if (this.fileService && functionName) { this.renameFunction(); } } } /** * Creates a folder function with a provided name. * @param {string} [functionName] - function name * @memberof DefaultStructureService */ createFunctionsFolder(functionName, takeRoot = false) { const path = takeRoot ? this.fileService.getRoot() : this.cwd; if (functionName) { this.functionName = functionName; this.fileService.copy((0, path_1.join)(this.dirname, '..', '..', 'bin', 'example', 'functions', 'exampleFunction'), (0, path_1.join)(path, 'functions', functionName)); } else { this.copySettings(); } } createReadme() { this.fileService.copy((0, path_1.join)(this.dirname, '..', '..', 'bin', 'example', 'README.md'), (0, path_1.join)(this.cwd, 'README.md')); } createDefaultServices() { this.fileService.copy((0, path_1.join)(this.dirname, '..', '..', 'bin', 'example', 'bin'), (0, path_1.join)(this.cwd, 'bin')); } copyIntellijSettings() { // will be changed to .idea because '.' folder/files are ignored by npm publish this.fileService.copy((0, path_1.join)(this.dirname, '..', '..', 'bin', 'example', 'idea'), (0, path_1.join)(this.cwd, '.idea')); } copyVsCodeSettings() { // will be changed to .vscode because '.' folder/files are ignored by npm publish this.fileService.copy((0, path_1.join)(this.dirname, '..', '..', 'bin', 'example', 'vscode'), (0, path_1.join)(this.cwd, '.vscode')); } copyGitIgnore() { // will be changed to .ignore because '.' folder/files are ignored by npm publish this.fileService.copy((0, path_1.join)(this.dirname, '..', '..', 'bin', 'example', 'gitignore'), (0, path_1.join)(this.cwd, '.gitignore')); } copySettings() { this.fileService.copy((0, path_1.join)(this.dirname, '..', '..', 'bin', 'example', 'functions', 'settings.json'), (0, path_1.join)(this.cwd, 'functions', 'settings.json')); } renameFunction() { const path = (0, path_1.join)(this.cwd, 'functions', this.functionName, 'config.json'); const file = this.fileService.read((0, path_1.join)(this.cwd, 'functions', this.functionName, 'config.json')); file.name = this.functionName; file.description = `${this.functionName} description`; this.fileService.write(path, file); } createFunction(functionConfig) { const { name } = functionConfig; if (name && this.fileService.directoryOrFileExists((0, path_1.join)(this.cwd, 'functions', name))) { throw new Error(`Folder with same name already exists (${name})`); } this.createFunctionsFolder(name); this.adjustDefaultConfig(functionConfig); } adjustDefaultConfig({ name, event, description, }) { if (!name) { throw new Error('Name is required'); } const path = (0, path_1.join)(this.cwd, 'functions', name, 'config.json'); const file = this.fileService.read((0, path_1.join)(this.cwd, 'functions', name, 'config.json')); file.name = name; file.description = description || `${this.functionName} description`; file.event = event || 'No Event'; this.fileService.write(path, file); } } exports.DefaultStructureService = DefaultStructureService; //# sourceMappingURL=defaultStructure.service.js.map