voxa-cli
Version:
The Voxa CLI tools
164 lines • 8.12 kB
JavaScript
;
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 });
/*
* 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-console no-submodule-imports */
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_1 = __importDefault(require("lodash"));
const path_1 = __importDefault(require("path"));
const AlexaSchema_1 = require("./AlexaSchema");
const Spreadsheet_1 = require("./connectors/Spreadsheet");
const DialogflowSchema_1 = require("./DialogflowSchema");
const Drive_1 = require("./Drive");
const fs = Promise.promisifyAll(fs_extra_1.default);
exports.DEFAULT_INTERACTION_OPTIONS = {
speechPath: "speech-assets",
platforms: ["alexa"],
contentPath: "content",
viewsPath: "/",
synonymPath: "synonyms",
assets: [],
assetsPath: "assets"
};
function defaultOptions(interactionOptions) {
const rootPath = interactionOptions.rootPath || "";
const speechPath = interactionOptions.speechPath || exports.DEFAULT_INTERACTION_OPTIONS.speechPath;
const synonymPath = interactionOptions.synonymPath || exports.DEFAULT_INTERACTION_OPTIONS.synonymPath;
const viewsPath = interactionOptions.viewsPath || exports.DEFAULT_INTERACTION_OPTIONS.viewsPath;
const contentPath = interactionOptions.contentPath || exports.DEFAULT_INTERACTION_OPTIONS.contentPath;
const assetsPath = interactionOptions.assetsPath || exports.DEFAULT_INTERACTION_OPTIONS.assetsPath;
const assets = interactionOptions.assets || exports.DEFAULT_INTERACTION_OPTIONS.assets;
const spreadsheets = arrayify(interactionOptions.spreadsheets);
const alexaSpreadsheets = arrayify(interactionOptions.alexaSpreadsheets);
const dialogflowSpreadsheets = arrayify(interactionOptions.dialogflowSpreadsheets);
let platforms = arrayify(interactionOptions.platforms);
const spreadsheetMapping = {
alexa: alexaSpreadsheets,
dialogflow: dialogflowSpreadsheets
};
// If the user didn't declare anything in the platorm key,
// then we should inspect the spreadshseet in alexaSpreadsheets and dialogflowSpreadsheets
// to find out what platform the user wants to use.
platforms = lodash_1.default.chain(spreadsheetMapping)
.toPairs()
.reduce((acc, next) => {
const key = next[0];
const value = next[1];
if (value) {
acc.push(key);
}
return acc;
}, [])
.concat(platforms)
.filter()
.uniq()
.value();
if (lodash_1.default.isEmpty(spreadsheets) && lodash_1.default.isEmpty(platforms)) {
throw Error("Spreadsheet were not specified in the right format");
}
platforms = lodash_1.default.isEmpty(platforms) ? exports.DEFAULT_INTERACTION_OPTIONS.platforms : platforms;
return {
rootPath,
spreadsheets,
alexaSpreadsheets,
dialogflowSpreadsheets,
speechPath,
synonymPath,
viewsPath,
assetsPath,
contentPath,
platforms,
assets
};
}
function arrayify(value) {
return lodash_1.default.isString(value) ? [value] : value;
}
function getSheetsByPlatform(interactionOptions, authKeys) {
return __awaiter(this, void 0, void 0, function* () {
const sheets = yield Spreadsheet_1.transform(interactionOptions, authKeys);
const AVAILABLE_PLATFORM_KEYS = ["alexaSpreadsheets", "dialogflowSpreadsheets"];
const transformByPlatfromPromises = AVAILABLE_PLATFORM_KEYS.map(spreadsheetKey => Spreadsheet_1.transform(interactionOptions, authKeys, spreadsheetKey));
const sheetsByPlatformPromise = yield Promise.all(transformByPlatfromPromises);
const sheetsByPlatform = AVAILABLE_PLATFORM_KEYS.reduce((acc, next, index) => {
const sheetByPlatform = lodash_1.default.chain(sheets)
.cloneDeep()
.concat(sheetsByPlatformPromise[index])
.uniq()
.value();
lodash_1.default.set(acc, next, sheetByPlatform);
return acc;
}, {}); // concat all the sheets from the `spreadsheets` key with platform specific sheets
return sheetsByPlatform;
});
}
exports.buildInteraction = (interactionOptions, authKeys) => __awaiter(void 0, void 0, void 0, function* () {
const definedInteractionOptions = defaultOptions(interactionOptions);
console.time("all");
console.time("timeframe");
const { alexaSpreadsheets, dialogflowSpreadsheets } = yield getSheetsByPlatform(definedInteractionOptions, authKeys);
console.timeEnd("timeframe");
const platforms = definedInteractionOptions.platforms;
const schemas = [];
if (platforms.includes("alexa")) {
const schema = new AlexaSchema_1.AlexaSchema(alexaSpreadsheets, definedInteractionOptions);
schemas.push(schema);
}
if (platforms.includes("dialogflow")) {
const schema = new DialogflowSchema_1.DialogflowSchema(dialogflowSpreadsheets, definedInteractionOptions);
schemas.push(schema);
}
yield Promise.all(schemas.map(schema => fs.remove(path_1.default.join(definedInteractionOptions.rootPath, definedInteractionOptions.speechPath, schema.NAMESPACE))));
const fileContentsProcess = lodash_1.default.chain(schemas)
.reduce((acc, schema, index) => {
if (index === 0) {
// We only want to execute this file once
schema.buildDownloads();
schema.buildViews();
schema.buildViewsMapping();
schema.buildSynonyms();
}
schema.invocations.map((invoc) => {
schema.build(invoc.locale, invoc.environment);
});
acc = acc.concat(schema.fileContent);
return acc;
}, [])
.uniqBy("path")
.map((file) => fs.outputFile(file.path, JSON.stringify(file.content, null, 2), { flag: "w" })).value();
yield Promise.all(fileContentsProcess);
yield Drive_1.downloadDirs(definedInteractionOptions.assets, path_1.default.join(definedInteractionOptions.rootPath, definedInteractionOptions.assetsPath), authKeys);
console.timeEnd("all");
});
//# sourceMappingURL=InteractionBuilder.js.map