UNPKG

office-addin-dev-settings

Version:

Configure developer settings for Office Add-ins.

519 lines 23.1 kB
"use strict"; // 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 = tslib_1.__importStar(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 console process */ function appcontainer(manifestPath, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if ((0, appcontainer_1.isAppcontainerSupported)()) { try { if (options.loopback) { try { const askForConfirmation = options.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 (options.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, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { if (options.enable) { yield enableDebugging(manifestPath, options); } else if (options.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, options) { 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(options.debugMethod), options.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, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { if (options.enable) { yield enableLiveReload(manifestPath); } else if (options.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, options /* 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, options /* 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(options /* eslint-disable-line @typescript-eslint/no-unused-vars */) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const registeredAddins = yield devSettings.getRegisteredAddIns(); 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 (_a) { // 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(options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { if (options.enable) { const path = typeof options.enable === "string" ? options.enable : undefined; yield enableRuntimeLogging(path); } else if (options.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, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const app = options.app ? (0, office_addin_manifest_1.parseOfficeApp)(options.app) : undefined; const canPrompt = true; const document = options.document ? options.document : undefined; const appType = (0, appType_1.parseAppType)(type || process.env.npm_package_config_app_platform_to_debug); const registration = options.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, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath); const host = parseStringCommandOption(options.host); const port = parseStringCommandOption(options.port); const path = parseStringCommandOption(options.path); const extension = parseStringCommandOption(options.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, options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { if (options.host !== undefined || options.port !== undefined || options.path !== undefined || options.extension !== undefined) { yield setSourceBundleUrl(manifestPath, options); } 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, options /* 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