voxa-cli
Version:
The Voxa CLI tools
144 lines • 6.58 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 axios_1 = __importDefault(require("axios"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_1 = __importDefault(require("lodash"));
const path_1 = __importDefault(require("path"));
const qs_1 = __importDefault(require("qs"));
const Excel_1 = require("./Excel");
const OFFICE_PATH = ".voxa/office";
function buildFromOffice365(options, spreadsheetKey) {
return __awaiter(this, void 0, void 0, function* () {
const officeSharedIds = lodash_1.default.chain(options).get(spreadsheetKey)
.filter((sheet) => sheet.includes(".sharepoint.com"))
.map(getOfficeShareId)
.compact()
.value();
let auth = {};
const authPath = path_1.default.join(options.rootPath, "azure_secret.json");
try {
auth = require(authPath);
// tslint:disable-next-line: no-empty
}
catch (e) { }
if (lodash_1.default.isEmpty(auth) || lodash_1.default.isEmpty(officeSharedIds)) {
return [];
}
const accessToken = yield getAccessToken(auth);
if (!lodash_1.default.isString(accessToken)) {
return [];
}
yield fs_extra_1.default.remove(path_1.default.join(options.rootPath, OFFICE_PATH));
return downloadExcelFiles(officeSharedIds, accessToken, options, spreadsheetKey);
});
}
exports.buildFromOffice365 = buildFromOffice365;
function getOfficeShareId(sheet) {
// how to encode shareId URL
// https://docs.microsoft.com/en-us/graph/api/shares-get?view=graph-rest-1.0#encoding-sharing-urls
const base64 = lodash_1.default.trimEnd(Buffer.from(sheet).toString("base64"), "=");
const sharedId = `u!${base64.replace("/", "_").replace("+", "-")}`;
return sharedId;
}
function getAccessToken(auth) {
return __awaiter(this, void 0, void 0, function* () {
const { client_secret, client_id, tenant_directory } = auth;
const data = {
client_id,
client_secret,
grant_type: "client_credentials",
resource: "https://graph.microsoft.com"
};
const axiosOptions = {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
data: qs_1.default.stringify(data),
url: `https://login.microsoftonline.com/${tenant_directory}/oauth2/token`
};
let accessToken;
try {
const resp = yield axios_1.default.request(axiosOptions);
accessToken = lodash_1.default.get(resp, "data.access_token");
}
catch (error) {
throw error;
}
return accessToken;
});
}
function downloadExcelFiles(officeSharedIds, accessToken, options, spreadsheetKey) {
return __awaiter(this, void 0, void 0, function* () {
const headers = {
Authorization: `Bearer ${accessToken}`
};
const metadataPromises = lodash_1.default.chain(officeSharedIds)
.map(sharedId => [
axios_1.default.request({
headers,
url: `https://graph.microsoft.com/v1.0/shares/${sharedId}/driveitem`
}),
axios_1.default.request({
responseType: "arraybuffer",
headers,
url: `https://graph.microsoft.com/v1.0/shares/${sharedId}/driveitem/content`
})
])
.flatten()
.value();
yield outputExcelFiles(options, metadataPromises);
const spreadsheets = lodash_1.default.get(options, spreadsheetKey);
spreadsheets.push(OFFICE_PATH);
return Excel_1.buildFromLocalExcel(Object.assign(Object.assign({}, options), { spreadsheets }), spreadsheetKey);
});
}
function outputExcelFiles(options, metadataPromises) {
return __awaiter(this, void 0, void 0, function* () {
const files = yield Promise.all(metadataPromises);
yield Promise.all(lodash_1.default.chain(files)
.chunk(2)
.map(item => {
const name = lodash_1.default.get(item[0], "data.name");
const content = lodash_1.default.get(item[1], "data");
const file = {
content,
path: path_1.default.join(options.rootPath, OFFICE_PATH, name)
};
return fs_extra_1.default.outputFile(file.path, file.content, { flag: "w" });
})
.value());
});
}
//# sourceMappingURL=Office365.js.map