UNPKG

voxa-cli

Version:
195 lines 9.37 kB
"use strict"; /* * Copyright (c) 2018 Rain Agency <contact@rain.agency> * Author: Rain Agency <contact@rain.agency> * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* tslint:disable:no-submodule-imports no-console */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const handlebars_1 = __importDefault(require("handlebars")); const lodash_1 = __importDefault(require("lodash")); const path_1 = __importDefault(require("path")); class VoxaGenerator { constructor(answers) { this.data = { appName: answers.appName, kebabAppName: lodash_1.default.kebabCase(answers.appName), folderName: answers.newDir ? lodash_1.default.kebabCase(answers.appName) : "", canFulfill: answers.canFulfill, language: answers.language, ext: answers.language === "javascript" ? "js" : "ts", author: answers.author, ga: answers.analytics.includes("ga") || answers.analytics.includes("all"), dashbot: answers.analytics.includes("dashbot") || answers.analytics.includes("all"), chatbase: answers.analytics.includes("chatbase") || answers.analytics.includes("all"), voxaCli: answers.voxaCli, saveUserInfo: answers.saveUserInfo, usesAlexa: answers.platform.includes("alexa") || answers.platform.includes("all"), usesGoogleAssistant: answers.platform.includes("google") || answers.platform.includes("all"), usesFacebook: answers.platform.includes("facebook") || answers.platform.includes("all"), usesTelegram: answers.platform.includes("telegram") || answers.platform.includes("all"), accountLinking: answers.accountLinking }; } generateProject() { return __awaiter(this, void 0, void 0, function* () { try { yield this.copyPackageFile(); yield this.copyReadmeFile(); yield this.copyGitIgnoreFile(); if (this.data.voxaCli) { yield this.copyInteractionFile(); } yield this.copyServerless(); yield this.copyServer(); yield this.copySrcFiles(); if (this.data.accountLinking) { yield this.copyWebFiles(); } yield this.copyAllOtherFiles(); return Promise.resolve(true); } catch (error) { console.error(error); return Promise.resolve(false); } }); } getTemplatePath(...args) { let fileDir = [__dirname, "..", "..", "templates", this.data.language, ...args]; if (path_1.default.basename(__filename) === "VoxaGenerator.ts") { fileDir = [__dirname, "..", "templates", this.data.language, ...args]; } return path_1.default.join(...fileDir); } getTemplateFile(...args) { return __awaiter(this, void 0, void 0, function* () { if (path_1.default.basename(__filename) === "VoxaGenerator.ts") { return fs_extra_1.default.readFile(path_1.default.join(__dirname, "..", "templates", this.data.language, ...args), "utf8"); } return fs_extra_1.default.readFile(path_1.default.join(__dirname, "..", "..", "templates", this.data.language, ...args), "utf8"); }); } generateHandlebarTemplateFile(...filePath) { return __awaiter(this, void 0, void 0, function* () { const data = this.data; const content = yield this.getTemplateFile(...filePath); const template = handlebars_1.default.compile(content); const result = template(data); return fs_extra_1.default.outputFile(path_1.default.join(process.cwd(), this.data.folderName, ...filePath), result); }); } copyFileOrFolder(...filePath) { return fs_extra_1.default.copy(this.getTemplatePath(...filePath), path_1.default.join(process.cwd(), this.data.folderName, ...filePath)); } copyPackageFile() { return this.generateHandlebarTemplateFile("package.json"); } copyReadmeFile() { return __awaiter(this, void 0, void 0, function* () { return this.generateHandlebarTemplateFile("README.md"); }); } copyGitIgnoreFile() { return __awaiter(this, void 0, void 0, function* () { yield this.copyFileOrFolder("gitignore.txt"); return fs_extra_1.default.rename(path_1.default.join(process.cwd(), this.data.folderName, "gitignore.txt"), path_1.default.join(process.cwd(), this.data.folderName, ".gitignore")); }); } copyInteractionFile() { return this.copyFileOrFolder("interaction.json"); } copyServerless() { return this.generateHandlebarTemplateFile("serverless.yml"); } copyServer() { return this.generateHandlebarTemplateFile(`server.${this.data.ext}`); } copySrcIndexFile() { return this.generateHandlebarTemplateFile("src", "app", `index.${this.data.ext}`); } copySrcModelFile() { return this.generateHandlebarTemplateFile("src", "app", `model.${this.data.ext}`); } copySrcHandlerFile() { return this.generateHandlebarTemplateFile("src", `handler.${this.data.ext}`); } copySrcConfigFiles() { const configFilePaths = [ ["src", "config", "local.example.json"], ["src", "config", "staging.json"], ["src", "config", "production.json"] ]; return Promise.all(configFilePaths.map(filePath => this.generateHandlebarTemplateFile(...filePath))); } copySrcFiles() { return __awaiter(this, void 0, void 0, function* () { const srcFolderPath = this.getTemplatePath("src"); const destinationPath = path_1.default.join(process.cwd(), this.data.folderName, "src"); yield fs_extra_1.default.copy(srcFolderPath, destinationPath); if (!this.data.saveUserInfo) { yield fs_extra_1.default.remove(path_1.default.join(process.cwd(), this.data.folderName, "src", "services", `User.${this.data.ext}`)); } const promises = [ yield this.copySrcIndexFile(), yield this.copySrcModelFile(), yield this.copySrcHandlerFile(), yield this.copySrcConfigFiles() ]; return Promise.all(promises); }); } copyWebFiles() { return this.copyFileOrFolder("web"); } copyAllOtherFiles() { return __awaiter(this, void 0, void 0, function* () { const rootFiles = yield fs_extra_1.default.readdir(this.getTemplatePath()); const PROCESSED_FILES = [ "README.md", "serverless.yml", "package.json", "src", "interaction.json", "gitignore.txt", `server.${this.data.ext}`, "web" ]; const filteredFiles = rootFiles.filter(file => !PROCESSED_FILES.includes(file)); return filteredFiles.map(file => { fs_extra_1.default.copy(this.getTemplatePath(file), path_1.default.join(process.cwd(), this.data.folderName, file)); }); }); } } exports.default = VoxaGenerator; //# sourceMappingURL=VoxaGenerator.js.map