voxa-cli
Version:
The Voxa CLI tools
116 lines • 5.39 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.
*/
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_1 = __importDefault(require("lodash"));
const node_xlsx_1 = __importDefault(require("node-xlsx"));
const path_1 = __importDefault(require("path"));
const VoxaSheet_1 = require("../VoxaSheet");
const utils_1 = require("./utils");
const Processor_1 = require("../Processor");
function findLocalFiles(spreadsheet) {
const fsStats = fs_extra_1.default.lstatSync(spreadsheet);
if (fsStats.isDirectory()) {
return fs_extra_1.default
.readdirSync(spreadsheet)
.filter(f => lodash_1.default.endsWith(f, ".xlsx") || lodash_1.default.endsWith(f, ".ods") || lodash_1.default.endsWith(f, ".fods"))
.map(f => path_1.default.join(spreadsheet, f));
}
return [spreadsheet];
}
function readFileCreateWorkbook(f) {
const workbook = node_xlsx_1.default.parse(f);
const spreadsheetTitle = lodash_1.default.last(f.split("/"));
const spreadsheetId = f;
if (!lodash_1.default.isString(spreadsheetTitle)) {
return undefined;
}
return workbook.map(book => ({
spreadsheetId,
spreadsheetTitle,
sheetTitle: book.name,
type: "none",
data: processBookData(book.data).filter(row => row.length)
}));
}
function refactorExcelData(sheet) {
const reestrictFormat = lodash_1.default.isEmpty(Processor_1.filterSheets([sheet], [VoxaSheet_1.SheetTypes.UTTERANCE]));
sheet.data = lodash_1.default.chain(sheet).get("data")
.map((next, index, arr) => {
if (index === 0) {
return next;
}
const head = arr[0];
const extraColumns = head.length - next.length;
if (extraColumns > 0) {
next = lodash_1.default.concat(next, lodash_1.default.fill(Array(extraColumns), undefined));
}
return next;
})
.reduce((acc, next, iindex, arr) => utils_1.rowFormatted(acc, next, iindex, arr, reestrictFormat), [])
.drop()
.value();
return sheet;
}
function buildFromLocalExcel(options, spreadsheetKey) {
return __awaiter(this, void 0, void 0, function* () {
const vsheet = lodash_1.default.chain(options).get(spreadsheetKey)
.map((f) => (f.indexOf("/") === 0 ? f : path_1.default.join(options.rootPath, f)))
.filter((spreadsheet) => fs_extra_1.default.pathExistsSync(spreadsheet))
.map(findLocalFiles)
.flatten()
.map(readFileCreateWorkbook)
.flatten()
.compact()
.map(utils_1.findSheetType)
.filter((sheet) => lodash_1.default.get(sheet, "type") !== "none" && !lodash_1.default.isEmpty(sheet))
.map(refactorExcelData)
.value();
return vsheet;
});
}
exports.buildFromLocalExcel = buildFromLocalExcel;
function processBookData(data) {
// this is all because of a bug in node-xlsx where the first cell of the first tab get's a lot
// of garbage appended to it's content
const titleRow = lodash_1.default.get(data, "[0]", "");
const firstColumnTitle = lodash_1.default.get(titleRow, "[0]", "");
const split = firstColumnTitle.split("\n");
if (split.length > 1) {
titleRow[0] = split[split.length - 1];
}
return data;
}
//# sourceMappingURL=Excel.js.map