Outlook_Addin_LLM/node_modules/office-addin-dev-settings/lib/commands.js

516 lines
22 KiB
JavaScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
Object.defineProperty(exports, "__esModule", { value: true });
exports.webView = exports.unregister = exports.sourceBundleUrl = exports.setSourceBundleUrl = exports.sideload = exports.runtimeLogging = exports.registered = exports.register = exports.m365Account = exports.parseWebViewType = exports.liveReload = exports.isRuntimeLoggingEnabled = exports.isLiveReloadEnabled = exports.isDebuggingEnabled = exports.getSourceBundleUrl = exports.enableRuntimeLogging = exports.enableLiveReload = exports.enableDebugging = exports.disableRuntimeLogging = exports.disableLiveReload = exports.disableDebugging = exports.debugging = exports.clear = exports.appcontainer = void 0;
const tslib_1 = require("tslib");
const office_addin_usage_data_1 = require("office-addin-usage-data");
const office_addin_manifest_1 = require("office-addin-manifest");
const appcontainer_1 = require("./appcontainer");
const appType_1 = require("./appType");
const devSettings = require("./dev-settings");
const sideload_1 = require("./sideload");
const defaults_1 = require("./defaults");
const office_addin_usage_data_2 = require("office-addin-usage-data");
const publish_1 = require("./publish");
/* global process, console */
function appcontainer(manifestPath, command) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if ((0, appcontainer_1.isAppcontainerSupported)()) {
try {
if (command.loopback) {
try {
const askForConfirmation = command.yes !== true;
const allowed = yield (0, appcontainer_1.ensureLoopbackIsEnabled)(manifestPath, askForConfirmation);
console.log(allowed ? "Loopback is allowed." : "Loopback is not allowed.");
}
catch (err) {
throw new Error(`Unable to allow loopback for the appcontainer. \n${err}`);
}
}
else if (command.preventLoopback) {
try {
const name = yield (0, appcontainer_1.getAppcontainerNameFromManifestPath)(manifestPath);
yield (0, appcontainer_1.removeLoopbackExemptionForAppcontainer)(name);
console.log(`Loopback is no longer allowed.`);
}
catch (err) {
throw new Error(`Unable to disallow loopback. \n${err}`);
}
}
else {
try {
const name = yield (0, appcontainer_1.getAppcontainerNameFromManifestPath)(manifestPath);
const allowed = yield (0, appcontainer_1.isLoopbackExemptionForAppcontainer)(name);
console.log(allowed ? "Loopback is allowed." : "Loopback is not allowed.");
}
catch (err) {
throw new Error(`Unable to determine if appcontainer allows loopback. \n${err}`);
}
}
defaults_1.usageDataObject.reportSuccess("appcontainer");
}
catch (err) {
defaults_1.usageDataObject.reportException("appcontainer", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
}
else {
console.log("Appcontainer is not supported.");
}
});
}
exports.appcontainer = appcontainer;
function clear(manifestPath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
yield devSettings.clearDevSettings(manifest.id);
console.log("Developer settings have been cleared.");
defaults_1.usageDataObject.reportSuccess("clear");
}
catch (err) {
defaults_1.usageDataObject.reportException("clear", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.clear = clear;
function debugging(manifestPath, command) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
if (command.enable) {
yield enableDebugging(manifestPath, command);
}
else if (command.disable) {
yield disableDebugging(manifestPath);
}
else {
yield isDebuggingEnabled(manifestPath);
}
defaults_1.usageDataObject.reportSuccess("debugging");
}
catch (err) {
defaults_1.usageDataObject.reportException("debugging", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.debugging = debugging;
function displaySourceBundleUrl(components) {
console.log(`host: ${components.host !== undefined ? `"${components.host}"` : '"localhost" (default)'}`);
console.log(`port: ${components.port !== undefined ? `"${components.port}"` : '"8081" (default)'}`);
console.log(`path: ${components.path !== undefined ? `"${components.path}"` : "(default)"}`);
console.log(`extension: ${components.extension !== undefined ? `"${components.extension}"` : '".bundle" (default)'}`);
console.log();
console.log(`Source bundle url: ${components.url}`);
console.log();
}
function disableDebugging(manifestPath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
yield devSettings.disableDebugging(manifest.id);
console.log("Debugging has been disabled.");
defaults_1.usageDataObject.reportSuccess("disableDebugging()");
}
catch (err) {
defaults_1.usageDataObject.reportException("disableDebugging()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.disableDebugging = disableDebugging;
function disableLiveReload(manifestPath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
yield devSettings.disableLiveReload(manifest.id);
console.log("Live reload has been disabled.");
defaults_1.usageDataObject.reportSuccess("disableLiveReload()");
}
catch (err) {
defaults_1.usageDataObject.reportException("disableLiveReload()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.disableLiveReload = disableLiveReload;
function disableRuntimeLogging() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
yield devSettings.disableRuntimeLogging();
console.log("Runtime logging has been disabled.");
defaults_1.usageDataObject.reportSuccess("disableRuntimeLogging()");
}
catch (err) {
defaults_1.usageDataObject.reportException("disableRuntimeLogging()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.disableRuntimeLogging = disableRuntimeLogging;
function enableDebugging(manifestPath, command) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
yield devSettings.enableDebugging(manifest.id, true, toDebuggingMethod(command.debugMethod), command.openDevTools);
console.log("Debugging has been enabled.");
defaults_1.usageDataObject.reportSuccess("enableDebugging()");
}
catch (err) {
defaults_1.usageDataObject.reportException("enableDebugging()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.enableDebugging = enableDebugging;
function enableLiveReload(manifestPath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
yield devSettings.enableLiveReload(manifest.id);
console.log("Live reload has been enabled.");
defaults_1.usageDataObject.reportSuccess("enableLiveReload()");
}
catch (err) {
defaults_1.usageDataObject.reportException("enableLiveReload()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.enableLiveReload = enableLiveReload;
function enableRuntimeLogging(path) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const logPath = yield devSettings.enableRuntimeLogging(path);
console.log(`Runtime logging has been enabled. File: ${logPath}`);
defaults_1.usageDataObject.reportSuccess("enableRuntimeLogging()");
}
catch (err) {
defaults_1.usageDataObject.reportException("enableRuntimeLogging()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.enableRuntimeLogging = enableRuntimeLogging;
function getSourceBundleUrl(manifestPath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
const components = yield devSettings.getSourceBundleUrl(manifest.id);
displaySourceBundleUrl(components);
defaults_1.usageDataObject.reportSuccess("getSourceBundleUrl()");
}
catch (err) {
defaults_1.usageDataObject.reportException("getSourceBundleUrl()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.getSourceBundleUrl = getSourceBundleUrl;
function isDebuggingEnabled(manifestPath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
const enabled = yield devSettings.isDebuggingEnabled(manifest.id);
console.log(enabled ? "Debugging is enabled." : "Debugging is not enabled.");
defaults_1.usageDataObject.reportSuccess("isDebuggingEnabled()");
}
catch (err) {
defaults_1.usageDataObject.reportException("isDebuggingEnabled()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.isDebuggingEnabled = isDebuggingEnabled;
function isLiveReloadEnabled(manifestPath) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
const enabled = yield devSettings.isLiveReloadEnabled(manifest.id);
console.log(enabled ? "Live reload is enabled." : "Live reload is not enabled.");
defaults_1.usageDataObject.reportSuccess("isLiveReloadEnabled()");
}
catch (err) {
defaults_1.usageDataObject.reportException("isLiveReloadEnabled()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.isLiveReloadEnabled = isLiveReloadEnabled;
function isRuntimeLoggingEnabled() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const path = yield devSettings.getRuntimeLoggingPath();
console.log(path ? `Runtime logging is enabled. File: ${path}` : "Runtime logging is not enabled.");
defaults_1.usageDataObject.reportSuccess("isRuntimeLoggingEnabled()");
}
catch (err) {
defaults_1.usageDataObject.reportException("isRuntimeLoggingEnabled()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.isRuntimeLoggingEnabled = isRuntimeLoggingEnabled;
function liveReload(manifestPath, command) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
if (command.enable) {
yield enableLiveReload(manifestPath);
}
else if (command.disable) {
yield disableLiveReload(manifestPath);
}
else {
yield isLiveReloadEnabled(manifestPath);
}
defaults_1.usageDataObject.reportSuccess("liveReload");
}
catch (err) {
defaults_1.usageDataObject.reportException("liveReload", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.liveReload = liveReload;
function parseStringCommandOption(optionValue) {
return typeof optionValue === "string" ? optionValue : undefined;
}
function parseWebViewType(webViewString) {
switch (webViewString ? webViewString.toLowerCase() : undefined) {
case "ie":
case "ie11":
case "internet explorer":
case "internetexplorer":
return devSettings.WebViewType.IE;
case "edgelegacy":
case "edge-legacy":
case "spartan":
return devSettings.WebViewType.Edge;
case "edge chromium":
case "edgechromium":
case "edge-chromium":
case "anaheim":
case "edge":
return devSettings.WebViewType.EdgeChromium;
case "default":
case "":
case null:
case undefined:
return undefined;
default:
throw new office_addin_usage_data_2.ExpectedError(`Please select a valid web view type instead of '${webViewString}'.`);
}
}
exports.parseWebViewType = parseWebViewType;
function m365Account(operation, command /* eslint-disable-line @typescript-eslint/no-unused-vars */) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
yield (0, publish_1.updateM365Account)(operation);
defaults_1.usageDataObject.reportSuccess("m365Account");
}
catch (err) {
defaults_1.usageDataObject.reportException("m365Account", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.m365Account = m365Account;
function register(manifestPath, command /* eslint-disable-line @typescript-eslint/no-unused-vars */) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
yield devSettings.registerAddIn(manifestPath);
defaults_1.usageDataObject.reportSuccess("register");
}
catch (err) {
defaults_1.usageDataObject.reportException("register", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.register = register;
function registered(command /* eslint-disable-line @typescript-eslint/no-unused-vars */) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const registeredAddins = yield devSettings.getRegisterAddIns();
if (registeredAddins.length > 0) {
for (const addin of registeredAddins) {
let id = addin.id;
if (!id && addin.manifestPath) {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(addin.manifestPath);
id = manifest.id || "";
}
catch (err) {
// ignore errors
}
}
console.log(`${id ? id + " " : ""}${addin.manifestPath}`);
}
}
else {
console.log("No add-ins are registered.");
}
defaults_1.usageDataObject.reportSuccess("registered");
}
catch (err) {
defaults_1.usageDataObject.reportException("registered", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.registered = registered;
function runtimeLogging(command) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
if (command.enable) {
const path = typeof command.enable === "string" ? command.enable : undefined;
yield enableRuntimeLogging(path);
}
else if (command.disable) {
yield disableRuntimeLogging();
}
else {
yield isRuntimeLoggingEnabled();
}
defaults_1.usageDataObject.reportSuccess("runtimeLogging");
}
catch (err) {
defaults_1.usageDataObject.reportException("runtimeLogging", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.runtimeLogging = runtimeLogging;
function sideload(manifestPath, type, command) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const app = command.app ? (0, office_addin_manifest_1.parseOfficeApp)(command.app) : undefined;
const canPrompt = true;
const document = command.document ? command.document : undefined;
const appType = (0, appType_1.parseAppType)(type || process.env.npm_package_config_app_platform_to_debug);
const registration = command.registration;
yield (0, sideload_1.sideloadAddIn)(manifestPath, app, canPrompt, appType, document, registration);
defaults_1.usageDataObject.reportSuccess("sideload");
}
catch (err) {
defaults_1.usageDataObject.reportException("sideload", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.sideload = sideload;
function setSourceBundleUrl(manifestPath, command) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
const host = parseStringCommandOption(command.host);
const port = parseStringCommandOption(command.port);
const path = parseStringCommandOption(command.path);
const extension = parseStringCommandOption(command.extension);
const components = new devSettings.SourceBundleUrlComponents(host, port, path, extension);
validateManifestId(manifest);
yield devSettings.setSourceBundleUrl(manifest.id, components);
console.log("Configured source bundle url.");
displaySourceBundleUrl(yield devSettings.getSourceBundleUrl(manifest.id));
defaults_1.usageDataObject.reportSuccess("setSourceBundleUrl()");
}
catch (err) {
defaults_1.usageDataObject.reportException("setSourceBundleUrl()", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.setSourceBundleUrl = setSourceBundleUrl;
function sourceBundleUrl(manifestPath, command) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
if (command.host !== undefined ||
command.port !== undefined ||
command.path !== undefined ||
command.extension !== undefined) {
yield setSourceBundleUrl(manifestPath, command);
}
else {
yield getSourceBundleUrl(manifestPath);
}
defaults_1.usageDataObject.reportSuccess("sourceBundleUrl");
}
catch (err) {
defaults_1.usageDataObject.reportException("sourceBundleUrl", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.sourceBundleUrl = sourceBundleUrl;
function toDebuggingMethod(text) {
switch (text) {
case "direct":
return devSettings.DebuggingMethod.Direct;
case "proxy":
return devSettings.DebuggingMethod.Proxy;
case "":
case null:
case undefined:
// preferred debug method
return devSettings.DebuggingMethod.Direct;
default:
throw new office_addin_usage_data_2.ExpectedError(`Please provide a valid debug method instead of '${text}'.`);
}
}
function unregister(manifestPath, command /* eslint-disable-line @typescript-eslint/no-unused-vars */) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
if (manifestPath === "all") {
yield devSettings.unregisterAllAddIns();
}
else {
yield devSettings.unregisterAddIn(manifestPath);
}
defaults_1.usageDataObject.reportSuccess("unregister");
}
catch (err) {
defaults_1.usageDataObject.reportException("unregister", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.unregister = unregister;
function validateManifestId(manifest) {
if (!manifest.id) {
throw new office_addin_usage_data_2.ExpectedError(`The manifest file doesn't contain the id of the Office Add-in.`);
}
}
function webView(manifestPath, webViewString) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath);
validateManifestId(manifest);
let webViewType;
if (webViewString === undefined) {
webViewType = yield devSettings.getWebView(manifest.id);
}
else {
webViewType = parseWebViewType(webViewString);
yield devSettings.setWebView(manifest.id, webViewType);
}
const webViewTypeName = devSettings.toWebViewTypeName(webViewType);
console.log(webViewTypeName ? `The web view type is set to ${webViewTypeName}.` : "The web view type has not been set.");
defaults_1.usageDataObject.reportSuccess("webView");
}
catch (err) {
defaults_1.usageDataObject.reportException("webView", err);
(0, office_addin_usage_data_1.logErrorMessage)(err);
}
});
}
exports.webView = webView;
//# sourceMappingURL=commands.js.map