UNPKG

office-addin-dev-settings

Version:

Configure developer settings for Office Add-ins.

342 lines 16.3 kB
#!/usr/bin/env node "use strict"; // copyright (c) Microsoft Corporation. All rights reserved. // licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.disableRefreshAddins = exports.unregisterAllAddIns = exports.unregisterAddIn = exports.toWebViewTypeName = exports.setWebView = exports.setSourceBundleUrl = exports.registerAddIn = exports.isLiveReloadEnabled = exports.isDebuggingEnabled = exports.getWebView = exports.getSourceBundleUrl = exports.getRuntimeLoggingPath = exports.getRegisteredAddIns = exports.getOpenDevTools = exports.getEnabledDebuggingMethods = exports.getDeveloperSettingsRegistryKey = exports.enableRuntimeLogging = exports.enableLiveReload = exports.enableDebugging = exports.disableRuntimeLogging = exports.deleteDeveloperSettingsRegistryKey = exports.clearDevSettings = exports.OutlookSideloadManifestPath = void 0; const tslib_1 = require("tslib"); const office_addin_manifest_1 = require("office-addin-manifest"); const dev_settings_1 = require("./dev-settings"); const office_addin_usage_data_1 = require("office-addin-usage-data"); const registry = tslib_1.__importStar(require("./registry")); const publish_1 = require("./publish"); const path_1 = tslib_1.__importDefault(require("path")); /* global process */ const DeveloperSettingsRegistryKey = `HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Office\\16.0\\Wef\\Developer`; const OpenDevTools = "OpenDevTools"; exports.OutlookSideloadManifestPath = "OutlookSideloadManifestPath"; const RefreshAddins = "RefreshAddins"; const RuntimeLogging = "RuntimeLogging"; const SourceBundleExtension = "SourceBundleExtension"; const SourceBundleHost = "SourceBundleHost"; const SourceBundlePath = "SourceBundlePath"; const SourceBundlePort = "SourceBundlePort"; const TitleId = "TitleId"; const UseDirectDebugger = "UseDirectDebugger"; const UseLiveReload = "UseLiveReload"; const UseProxyDebugger = "UseWebDebugger"; const WebViewSelection = "WebViewSelection"; function clearDevSettings(addinId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return deleteDeveloperSettingsRegistryKey(addinId); }); } exports.clearDevSettings = clearDevSettings; function deleteDeveloperSettingsRegistryKey(addinId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); return registry.deleteKey(key); }); } exports.deleteDeveloperSettingsRegistryKey = deleteDeveloperSettingsRegistryKey; function disableRuntimeLogging() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(RuntimeLogging); return registry.deleteKey(key); }); } exports.disableRuntimeLogging = disableRuntimeLogging; function enableDebugging(addinId, enable = true, method = dev_settings_1.DebuggingMethod.Proxy, openDevTools = false) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); const useDirectDebugger = enable && method === dev_settings_1.DebuggingMethod.Direct; const useProxyDebugger = enable && method === dev_settings_1.DebuggingMethod.Proxy; yield registry.addBooleanValue(key, UseDirectDebugger, useDirectDebugger); yield registry.addBooleanValue(key, UseProxyDebugger, useProxyDebugger); if (enable && openDevTools) { yield registry.addBooleanValue(key, OpenDevTools, true); } else { yield registry.deleteValue(key, OpenDevTools); } }); } exports.enableDebugging = enableDebugging; function enableLiveReload(addinId, enable = true) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); return registry.addBooleanValue(key, UseLiveReload, enable); }); } exports.enableLiveReload = enableLiveReload; function enableRuntimeLogging(path) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(RuntimeLogging); return registry.addStringValue(key, "", path); // empty string for the default value }); } exports.enableRuntimeLogging = enableRuntimeLogging; function getDeveloperSettingsRegistryKey(addinId) { if (!addinId) { throw new office_addin_usage_data_1.ExpectedError("The addIn parameter is required."); } if (typeof addinId !== "string") { throw new office_addin_usage_data_1.ExpectedError("The addIn parameter should be a string."); } return new registry.RegistryKey(`${DeveloperSettingsRegistryKey}\\${addinId}`); } exports.getDeveloperSettingsRegistryKey = getDeveloperSettingsRegistryKey; function getEnabledDebuggingMethods(addinId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); const methods = []; if (isRegistryValueTrue(yield registry.getValue(key, UseDirectDebugger))) { methods.push(dev_settings_1.DebuggingMethod.Direct); } if (isRegistryValueTrue(yield registry.getValue(key, UseProxyDebugger))) { methods.push(dev_settings_1.DebuggingMethod.Proxy); } return methods; }); } exports.getEnabledDebuggingMethods = getEnabledDebuggingMethods; function getOpenDevTools(addinId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); return isRegistryValueTrue(yield registry.getValue(key, OpenDevTools)); }); } exports.getOpenDevTools = getOpenDevTools; function getRegisteredAddIns() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = new registry.RegistryKey(`${DeveloperSettingsRegistryKey}`); const values = yield registry.getValues(key); const filteredValues = values.filter((value) => value.name !== RefreshAddins); // if the registry value name and data are the same, then the manifest path was used as the name return filteredValues.map((value) => new dev_settings_1.RegisteredAddin(value.name !== value.data ? value.name : "", value.data)); }); } exports.getRegisteredAddIns = getRegisteredAddIns; function getRuntimeLoggingPath() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(RuntimeLogging); return registry.getStringValue(key, ""); // empty string for the default value }); } exports.getRuntimeLoggingPath = getRuntimeLoggingPath; function getSourceBundleUrl(addinId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); const components = new dev_settings_1.SourceBundleUrlComponents(yield registry.getStringValue(key, SourceBundleHost), yield registry.getStringValue(key, SourceBundlePort), yield registry.getStringValue(key, SourceBundlePath), yield registry.getStringValue(key, SourceBundleExtension)); return components; }); } exports.getSourceBundleUrl = getSourceBundleUrl; function getWebView(addinId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); const webViewString = yield registry.getStringValue(key, WebViewSelection); return toWebViewType(webViewString); }); } exports.getWebView = getWebView; function isDebuggingEnabled(addinId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); const useDirectDebugger = isRegistryValueTrue(yield registry.getValue(key, UseDirectDebugger)); const useWebDebugger = isRegistryValueTrue(yield registry.getValue(key, UseProxyDebugger)); return useDirectDebugger || useWebDebugger; }); } exports.isDebuggingEnabled = isDebuggingEnabled; function isLiveReloadEnabled(addinId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); const enabled = isRegistryValueTrue(yield registry.getValue(key, UseLiveReload)); return enabled; }); } exports.isLiveReloadEnabled = isLiveReloadEnabled; function isRegistryValueTrue(value) { if (value) { switch (value.type) { case registry.RegistryTypes.REG_DWORD: case registry.RegistryTypes.REG_QWORD: case registry.RegistryTypes.REG_SZ: return parseInt(value.data, undefined) !== 0; } } return false; } function registerAddIn(manifestPath, registration) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath); const appsInManifest = (0, office_addin_manifest_1.getOfficeAppsForManifestHosts)(manifest.hosts); // Register using the service if (manifest.manifestType === office_addin_manifest_1.ManifestType.JSON || appsInManifest.indexOf(office_addin_manifest_1.OfficeApp.Outlook) >= 0) { if (!registration) { let filePath = ""; if (manifest.manifestType === office_addin_manifest_1.ManifestType.JSON) { const targetPath = path_1.default.join(process.env.TEMP, "manifest.zip"); filePath = yield (0, office_addin_manifest_1.exportMetadataPackage)(targetPath, manifestPath); } else if (manifest.manifestType === office_addin_manifest_1.ManifestType.XML) { filePath = manifestPath; } registration = yield (0, publish_1.registerWithTeams)(filePath); yield enableRefreshAddins(); } const key = getDeveloperSettingsRegistryKey(exports.OutlookSideloadManifestPath); yield registry.addStringValue(key, TitleId, registration); } const key = new registry.RegistryKey(`${DeveloperSettingsRegistryKey}`); yield registry.deleteValue(key, manifestPath); // in case the manifest path was previously used as the key return registry.addStringValue(key, manifest.id || "", manifestPath); }); } exports.registerAddIn = registerAddIn; function setSourceBundleUrl(addinId, components) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); if (components.host !== undefined) { if (components.host) { yield registry.addStringValue(key, SourceBundleHost, components.host); } else { yield registry.deleteValue(key, SourceBundleHost); } } if (components.port !== undefined) { if (components.port) { yield registry.addStringValue(key, SourceBundlePort, components.port); } else { yield registry.deleteValue(key, SourceBundlePort); } } if (components.path !== undefined) { if (components.path) { yield registry.addStringValue(key, SourceBundlePath, components.path); } else { yield registry.deleteValue(key, SourceBundlePath); } } if (components.extension !== undefined) { if (components.extension) { yield registry.addStringValue(key, SourceBundleExtension, components.extension); } else { yield registry.deleteValue(key, SourceBundleExtension); } } }); } exports.setSourceBundleUrl = setSourceBundleUrl; function setWebView(addinId, webViewType) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = getDeveloperSettingsRegistryKey(addinId); switch (webViewType) { case undefined: case dev_settings_1.WebViewType.Default: yield registry.deleteValue(key, WebViewSelection); break; case dev_settings_1.WebViewType.IE: case dev_settings_1.WebViewType.Edge: case dev_settings_1.WebViewType.EdgeChromium: { const webViewString = webViewType; yield registry.addStringValue(key, WebViewSelection, webViewString); break; } default: throw new office_addin_usage_data_1.ExpectedError(`The webViewType ${webViewType} is not supported.`); } }); } exports.setWebView = setWebView; function toWebViewType(webViewString) { switch (webViewString ? webViewString.toLowerCase() : undefined) { case "ie": return dev_settings_1.WebViewType.IE; case "edge": return dev_settings_1.WebViewType.Edge; case "edge chromium": return dev_settings_1.WebViewType.EdgeChromium; default: return undefined; } } function toWebViewTypeName(webViewType) { switch (webViewType) { case dev_settings_1.WebViewType.Edge: return "legacy Microsoft Edge (EdgeHTML)"; case dev_settings_1.WebViewType.EdgeChromium: return "Microsoft Edge (Chromium)"; case dev_settings_1.WebViewType.IE: return "Microsoft Internet Explorer"; default: return undefined; } } exports.toWebViewTypeName = toWebViewTypeName; function unregisterAddIn(addinId, manifestPath) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = new registry.RegistryKey(`${DeveloperSettingsRegistryKey}`); if (addinId) { const manifest = yield office_addin_manifest_1.OfficeAddinManifest.readManifestFile(manifestPath); const appsInManifest = (0, office_addin_manifest_1.getOfficeAppsForManifestHosts)(manifest.hosts); // Unregister using the service // TODO: remove if statement when the service supports all hosts if (manifest.manifestType === office_addin_manifest_1.ManifestType.JSON || appsInManifest.indexOf(office_addin_manifest_1.OfficeApp.Outlook) >= 0) { yield unacquire(key, addinId); } yield registry.deleteValue(key, addinId); } // since the key used to be manifest path, delete it too if (manifestPath) { yield registry.deleteValue(key, manifestPath); } }); } exports.unregisterAddIn = unregisterAddIn; function unregisterAllAddIns() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const registeredAddins = yield getRegisteredAddIns(); for (const addin of registeredAddins) { yield unregisterAddIn(addin.id, addin.manifestPath); } }); } exports.unregisterAllAddIns = unregisterAllAddIns; function unacquire(key, id) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const manifest = yield registry.getStringValue(key, id); if (manifest != undefined) { const key = getDeveloperSettingsRegistryKey(exports.OutlookSideloadManifestPath); const registration = yield registry.getStringValue(key, TitleId); const uninstallId = registration != undefined ? registration : id; if (yield (0, publish_1.uninstallWithTeams)(uninstallId)) { if (registration != undefined) { registry.deleteValue(key, TitleId); } yield enableRefreshAddins(); } } }); } function enableRefreshAddins() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = new registry.RegistryKey(`${DeveloperSettingsRegistryKey}`); yield registry.addBooleanValue(key, RefreshAddins, true); }); } function disableRefreshAddins() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const key = new registry.RegistryKey(`${DeveloperSettingsRegistryKey}`); yield registry.deleteValue(key, RefreshAddins); }); } exports.disableRefreshAddins = disableRefreshAddins; //# sourceMappingURL=dev-settings-windows.js.map