Outlook_Addin_LLM/node_modules/office-addin-manifest-converter/lib/utilities.js

143 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

"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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utilities = void 0;
const osfmos_1 = require("../lib/osfmos");
const constants_1 = require("./constants");
const fs = require("fs");
const console_1 = require("console");
class Utilities {
static isNullOrUndefined(value) {
return value === null || value === undefined;
}
static writeFile(path, contents, errorText, successText) {
return new Promise((resolve, reject) => {
fs.writeFile(path, contents, err => {
if (err) {
Utilities.logError(errorText, err);
reject(err);
}
else {
Utilities.log(successText);
resolve();
}
});
});
}
static writeFileSync(path, contents, successText) {
return __awaiter(this, void 0, void 0, function* () {
yield fs.writeFileSync(path, contents);
Utilities.log(successText);
});
}
static ensureFolder(folderPath) {
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
}
static setInputXmlFilePath(inputXmlFilePath) {
Utilities._inputXmlFilePath = inputXmlFilePath;
}
static setShowDebugInfo(showDebugInfo) {
Utilities._showDebugInfo = showDebugInfo;
}
static setVerbosity(verbose) {
Utilities._verboseLogging = verbose;
}
static getVerbosity() {
return Utilities._verboseLogging;
}
static setDownloadImages(downloadImages) {
Utilities._downloadImages = downloadImages;
}
static setWriteImageUrls(writeImageUrls) {
Utilities._writeImageUrls = writeImageUrls;
}
static log(message, ...optionalParams) {
if (Utilities._verboseLogging) {
console.log(constants_1.Constants.LoggingPrefix + message, ...optionalParams);
}
}
static logWarning(message, ...optionalParams) {
if (Utilities._verboseLogging) {
console.error(constants_1.Constants.LoggingPrefixWarning + message, ...optionalParams);
}
}
static logError(message, ...optionalParams) {
if (Utilities._verboseLogging) {
console.error(constants_1.Constants.LoggingPrefixError + message, ...optionalParams);
}
}
static logDebug(message, ...optionalParams) {
if (Utilities._showDebugInfo) {
console.log(constants_1.Constants.LoggingPrefix + message, ...optionalParams);
}
}
static assert(condition, message, ...optionalParams) {
if (Utilities._showDebugInfo) {
(0, console_1.assert)(condition, message, ...optionalParams);
}
}
static createLocaleFileName(locale) {
return locale + constants_1.Constants.JSONFileSuffix;
}
static truncateString(value, maxLength) {
if (value.length <= maxLength) {
return value;
}
const insertLengths = constants_1.Constants.TruncationText.length + constants_1.Constants.EllipsisText.length;
if (maxLength <= insertLengths) {
if (maxLength <= constants_1.Constants.EllipsisText.length) {
return value;
}
return value.substring(0, maxLength - constants_1.Constants.EllipsisText.length) + constants_1.Constants.EllipsisText;
}
return constants_1.Constants.TruncationText + value.substring(0, maxLength - insertLengths) + constants_1.Constants.EllipsisText;
}
static getLimitedString(value, maxLength, message) {
if (value.length > maxLength) {
Utilities.logWarning(message, maxLength);
return Utilities.truncateString(value, maxLength);
}
else {
return value;
}
}
static setUpOSFAppTelemetry() {
if (Utilities.isNullOrUndefined(osfmos_1.OSF["AppTelemetry"])) {
osfmos_1.OSF["AppTelemetry"] = {};
osfmos_1.OSF["AppTelemetry"]["logAppCommonMessage"] = function (message) {
Utilities.log(message);
};
}
}
static promiseAll(promises, resolve, reject, message) {
return __awaiter(this, void 0, void 0, function* () {
let failedPromises = [];
yield Promise.all(promises.map(p => p.catch(e => {
failedPromises.push(e);
})));
if (failedPromises.length > 0) {
reject(new Error(message));
}
else {
resolve();
}
});
}
}
exports.Utilities = Utilities;
Utilities._inputXmlFilePath = "";
Utilities._showDebugInfo = false;
Utilities._verboseLogging = false;
Utilities._downloadImages = false;
Utilities._writeImageUrls = false;
//# sourceMappingURL=utilities.js.map