UNPKG

voxa-cli

Version:
201 lines 9.27 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 colors_1 = __importDefault(require("colors")); const fs_extra_1 = __importDefault(require("fs-extra")); const inquirer_1 = __importDefault(require("inquirer")); const lodash_1 = __importDefault(require("lodash")); const pad_1 = __importDefault(require("pad")); const path_1 = __importDefault(require("path")); const rxjs_1 = require("rxjs"); const InteractionBuilder_1 = require("../InteractionBuilder"); exports.name = "init"; exports.alias = ""; exports.description = "create a new interaction.json"; const ALLOWED_ATTRIBUTES = [ "platforms", "spreadsheets", "dialogflowSpreadsheets", "alexaSpreadsheets", "speechPath", "content", "contentPath", "viewsPath" ]; function action() { return __awaiter(this, void 0, void 0, function* () { const interactionFile = path_1.default.join(process.cwd(), "interaction.json"); let hasAnInteractionFile = false; try { // a path we KNOW is totally bogus and not a module hasAnInteractionFile = require(interactionFile); } catch (e) { if (e.code === "MODULE_NOT_FOUND") { hasAnInteractionFile = false; } } const observe = rxjs_1.Observable.create((obs) => { const override = (answers) => { return answers.interactionFile; }; const onlyCommaAnswer = (input) => { return lodash_1.default.chain(input) .split(",") .map(lodash_1.default.trim) .compact() .value(); }; const noEmptyAnswer = (input) => !lodash_1.default.isEmpty(input); const mustBeSpreadsheetURLOrLength30 = (input) => __awaiter(this, void 0, void 0, function* () { let result = lodash_1.default.chain(input) .split(",") .map(lodash_1.default.trim) .compact() .map((i) => { if (i.includes("docs.google.com/spreadsheets")) { const matched = i.match(/docs\.google\.com\/spreadsheets\/d\/(.*)\/.*/); return matched && lodash_1.default.isString(matched[1]) && matched[1].length > 30; } if (i.includes(".sharepoint.com") && i.length > 30) { return true; } const customPath = i.indexOf("/") === 0 ? i : path_1.default.join(process.cwd(), i); return fs_extra_1.default.pathExists(customPath); }) .value(); result = yield Promise.all(result); result = result.every((r) => r); return noEmptyAnswer(input) && result ? true : "Insert a proper google spreadsheet url, Office 365 sharepoint url, local file or local folder path. e.g. https://docs.google.com/spreadsheets/d/XXXXXXX/edit#gid=0, https://XXXXXX.sharepoint.com/:x:/g/personal/YYYYYY/ZZZZZZZ, /Users/local/file.xlsx or /Users/local/sheets/"; }); const questions = [ { type: "confirm", name: "interactionFile", message: `Do you want to override ${interactionFile}?`, when: () => hasAnInteractionFile, default: true }, { type: "checkbox", name: "platforms", message: "Choose platforms", choices: ["alexa", "dialogflow"], when: override }, { type: "input", name: "spreadsheets", message: "Specify all spreadsheets separated by comma (*)", validate: mustBeSpreadsheetURLOrLength30, filter: onlyCommaAnswer, when: override }, { type: "input", name: "speechPath", message: "Specify folder path to save interaction model and manifest", default: InteractionBuilder_1.DEFAULT_INTERACTION_OPTIONS.speechPath, when: override }, { type: "input", name: "contentPath", message: "Specify folder path to save all downloable content", when: override, default: InteractionBuilder_1.DEFAULT_INTERACTION_OPTIONS.contentPath }, { type: "input", name: "viewsPath", message: "Specify folder path to save views file", default: InteractionBuilder_1.DEFAULT_INTERACTION_OPTIONS.viewsPath, when: override }, { type: "confirm", name: "isCorrect", message: `is Correct?`, when: (answers) => { if (override(answers)) { printVariables(lodash_1.default.cloneDeep(answers)); } return override(answers); }, default: true } ]; questions.map(q => obs.next(q)); obs.complete(); }); function printVariables(answers) { return lodash_1.default.chain(answers) .toPairs() .filter(item => !!ALLOWED_ATTRIBUTES.find(i => i === item[0])) .map(item => { const key = item[0]; const value = item[1]; console.log(pad_1.default(colors_1.default.blue(key), 20), colors_1.default.grey(value)); return item; }) .compact() .fromPairs() .value(); } function executePrompt() { return __awaiter(this, void 0, void 0, function* () { return inquirer_1.default.prompt(observe).then((answers) => { if (answers.interactionFile && !answers.isCorrect) { return executePrompt(); } if (hasAnInteractionFile && !answers.interactionFile) { answers = hasAnInteractionFile; } if (!answers.interactionFile && !answers.isCorrect) { printVariables(answers); } answers = lodash_1.default.pick(answers, ALLOWED_ATTRIBUTES); return fs_extra_1.default.outputFile(path_1.default.join(process.cwd(), "interaction.json"), JSON.stringify(answers, null, 2), { flag: "w" }); }); }); } yield executePrompt(); }); } exports.action = action; //# sourceMappingURL=init.js.map