UNPKG

voxa-cli

Version:
120 lines 5.96 kB
"use strict"; 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. */ const bluebird_1 = __importDefault(require("bluebird")); const google_auth_library_1 = require("google-auth-library"); const googleapis_1 = require("googleapis"); const lodash_1 = __importDefault(require("lodash")); const VoxaSheet_1 = require("../VoxaSheet"); const utils_1 = require("./utils"); const Processor_1 = require("../Processor"); global.Promise = bluebird_1.default; const sheets = googleapis_1.google.sheets("v4"); const readSpreadsheet = Promise.promisify(sheets.spreadsheets.get, { context: sheets }); const readSheetTab = Promise.promisify(sheets.spreadsheets.values.get, { context: sheets }); function initVoxaSheet(spreadsheetsId, spreadsheetResp) { return lodash_1.default.chain(spreadsheetResp) .map((spreadsheet, index) => { const spreadsheetTitle = lodash_1.default.get(spreadsheet, "data.properties.title"); const spreadsheetId = spreadsheetsId[index]; const sheetNames = lodash_1.default.chain(spreadsheet).get("data.sheets", []) .map("properties.title") .value(); return sheetNames.map((sheetTitle) => { const voxaSheet = { spreadsheetId, spreadsheetTitle, sheetTitle, type: "none" }; return voxaSheet; }); }) .flatten() .map(utils_1.findSheetType) .compact() .value(); } function spreadsheetToVoxaSheet(client, spreadsheetsId, spreadsheetResp) { return __awaiter(this, void 0, void 0, function* () { spreadsheetResp = initVoxaSheet(spreadsheetsId, spreadsheetResp); let sheetPromises = spreadsheetResp.map((sheet) => readSheetTab({ auth: client, spreadsheetId: sheet.spreadsheetId, range: `${sheet.sheetTitle}!A1:ZZZ` })); try { sheetPromises = yield Promise.all(sheetPromises); } catch (e) { throw new Error(`Unable to get spreadsheet ${e}`); } return spreadsheetResp.map((sheet, index) => { const reestrictFormat = lodash_1.default.isEmpty(Processor_1.filterSheets([sheet], [VoxaSheet_1.SheetTypes.UTTERANCE])); const data = lodash_1.default.chain(sheetPromises[index]).get("data.values", []) .reduce((acc, next, iindex, arr) => utils_1.rowFormatted(acc, next, iindex, arr, reestrictFormat), []) .drop() .value(); // Apply processor sheet.data = data; return sheet; }); }); } function buildFromGoogleSheets(options, authKeys, spreadsheetKey) { return __awaiter(this, void 0, void 0, function* () { const spreadsheetsId = lodash_1.default.chain(options).get(spreadsheetKey) .map(getGoogleSpreadsheetId) .compact() .value(); let spreadsheetResp = []; if (lodash_1.default.isEmpty(spreadsheetsId) || lodash_1.default.isEmpty(authKeys)) { return []; } const client = google_auth_library_1.auth.fromJSON(authKeys); client.scopes = ["https://www.googleapis.com/auth/spreadsheets.readonly"]; try { spreadsheetResp = yield Promise.all(spreadsheetsId.map((spreadsheetId) => readSpreadsheet({ auth: client, spreadsheetId }))); // tslint:disable-next-line: no-empty } catch (e) { throw new Error(`Unable to read spreadsheets. Make sure user has access. ${e}`); } return spreadsheetToVoxaSheet(client, spreadsheetsId, spreadsheetResp); }); } exports.buildFromGoogleSheets = buildFromGoogleSheets; function getGoogleSpreadsheetId(sheet) { const matched = sheet.match(/docs\.google\.com\/spreadsheets\/d\/(.*)\/.*/); return sheet.includes("docs.google.com/spreadsheets") && matched && lodash_1.default.isString(matched[1]) ? matched[1] : undefined; } //# sourceMappingURL=Google.js.map