import { getOfficeAppsForManifestHosts , ManifestInfo , OfficeApp , OfficeAddinManifest } from "office-addin-manifest" ; import { DebuggingMethod , RegisteredAddin , SourceBundleUrlComponents , WebViewType } from "./dev-settings" ; import { ExpectedError } from "office-addin-usage-data" ; import * as registry from "./registry" ; const DeveloperSettingsRegistryKey = `HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Office\\16.0\\Wef\\Developer` ; const OpenDevTools = "OpenDevTools" ; export const OutlookSideloadManifestPath = "OutlookSideloadManifestPath" ; const RuntimeLogging = "RuntimeLogging" ; const SourceBundleExtension = "SourceBundleExtension" ; const SourceBundleHost = "SourceBundleHost" ; const SourceBundlePath = "SourceBundlePath" ; const SourceBundlePort = "SourceBundlePort" ; const UseDirectDebugger = "UseDirectDebugger" ; const UseLiveReload = "UseLiveReload" ; const UseProxyDebugger = "UseWebDebugger" ; const WebViewSelection = "WebViewSelection" ; export async function clearDevSettings ( addinId ) { return deleteDeveloperSettingsRegistryKey ( addinId ) ; } export async function deleteDeveloperSettingsRegistryKey ( addinId ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; return registry . deleteKey ( key ) ; } export async function disableRuntimeLogging ( ) { const key = getDeveloperSettingsRegistryKey ( RuntimeLogging ) ; return registry . deleteKey ( key ) ; } export async function enableDebugging ( addinId , enable = true , method = DebuggingMethod . Proxy , openDevTools = false ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; const useDirectDebugger = enable && method === DebuggingMethod . Direct ; const useProxyDebugger = enable && method === DebuggingMethod . Proxy ; await registry . addBooleanValue ( key , UseDirectDebugger , useDirectDebugger ) ; await registry . addBooleanValue ( key , UseProxyDebugger , useProxyDebugger ) ; if ( enable && openDevTools ) { await registry . addBooleanValue ( key , OpenDevTools , true ) ; } else { await registry . deleteValue ( key , OpenDevTools ) ; } } export async function enableLiveReload ( addinId , enable = true ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; return registry . addBooleanValue ( key , UseLiveReload , enable ) ; } async function enableOutlookSideloading ( manifestPath ) { const key = getDeveloperSettingsRegistryKey ( OutlookSideloadManifestPath ) ; return registry . addStringValue ( key , "" , manifestPath ) ; } export async function enableRuntimeLogging ( path ) { const key = getDeveloperSettingsRegistryKey ( RuntimeLogging ) ; return registry . addStringValue ( key , "" , path ) ; } export function getDeveloperSettingsRegistryKey ( addinId ) { if ( ! addinId ) { throw new ExpectedError ( "The addIn parameter is required." ) ; } if ( typeof addinId !== "string" ) { throw new ExpectedError ( "The addIn parameter should be a string." ) ; } return new registry . RegistryKey ( `${ DeveloperSettingsRegistryKey }\\${ addinId }` ) ; } export async function getEnabledDebuggingMethods ( addinId ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; const methods = [ ] ; if ( isRegistryValueTrue ( await registry . getValue ( key , UseDirectDebugger ) ) ) { methods . push ( DebuggingMethod . Direct ) ; } if ( isRegistryValueTrue ( await registry . getValue ( key , UseProxyDebugger ) ) ) { methods . push ( DebuggingMethod . Proxy ) ; } return methods ; } export async function getOpenDevTools ( addinId ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; return isRegistryValueTrue ( await registry . getValue ( key , OpenDevTools ) ) ; } export async function getRegisteredAddIns ( ) { const key = new registry . RegistryKey ( `${ DeveloperSettingsRegistryKey }` ) ; const values = await registry . getValues ( key ) ; return values . map ( ( value ) => new RegisteredAddin ( value . name !== value . data ? value . name : "" , value . data ) ) ; } export async function getRuntimeLoggingPath ( ) { const key = getDeveloperSettingsRegistryKey ( RuntimeLogging ) ; return registry . getStringValue ( key , "" ) ; } export async function getSourceBundleUrl ( addinId ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; const components = new SourceBundleUrlComponents ( await registry . getStringValue ( key , SourceBundleHost ) , await registry . getStringValue ( key , SourceBundlePort ) , await registry . getStringValue ( key , SourceBundlePath ) , await registry . getStringValue ( key , SourceBundleExtension ) ) ; return components ; } export async function getWebView ( addinId ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; const webViewString = await registry . getStringValue ( key , WebViewSelection ) ; return toWebViewType ( webViewString ) ; } export async function isDebuggingEnabled ( addinId ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; const useDirectDebugger = isRegistryValueTrue ( await registry . getValue ( key , UseDirectDebugger ) ) ; const useWebDebugger = isRegistryValueTrue ( await registry . getValue ( key , UseProxyDebugger ) ) ; return useDirectDebugger || useWebDebugger ; } export async function isLiveReloadEnabled ( addinId ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; const enabled = isRegistryValueTrue ( await registry . getValue ( key , UseLiveReload ) ) ; return enabled ; } 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 ; } export async function registerAddIn ( addinId , manifestPath ) { const manifest = await OfficeAddinManifest . readManifestFile ( manifestPath ) ; const appsInManifest = getOfficeAppsForManifestHosts ( manifest . hosts ) ; if ( appsInManifest . indexOf ( OfficeApp . Outlook ) >= 0 ) { enableOutlookSideloading ( manifestPath ) ; } const key = new registry . RegistryKey ( `${ DeveloperSettingsRegistryKey }` ) ; await registry . deleteValue ( key , manifestPath ) ; return registry . addStringValue ( key , addinId , manifestPath ) ; } export async function setSourceBundleUrl ( addinId , components ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; if ( components . host !== undefined ) { if ( components . host ) { await registry . addStringValue ( key , SourceBundleHost , components . host ) ; } else { await registry . deleteValue ( key , SourceBundleHost ) ; } } if ( components . port !== undefined ) { if ( components . port ) { await registry . addStringValue ( key , SourceBundlePort , components . port ) ; } else { await registry . deleteValue ( key , SourceBundlePort ) ; } } if ( components . path !== undefined ) { if ( components . path ) { await registry . addStringValue ( key , SourceBundlePath , components . path ) ; } else { await registry . deleteValue ( key , SourceBundlePath ) ; } } if ( components . extension !== undefined ) { if ( components . extension ) { await registry . addStringValue ( key , SourceBundleExtension , components . extension ) ; } else { await registry . deleteValue ( key , SourceBundleExtension ) ; } } } export async function setWebView ( addinId , webViewType ) { const key = getDeveloperSettingsRegistryKey ( addinId ) ; switch ( webViewType ) { case undefined : case WebViewType . Default : await registry . deleteValue ( key , WebViewSelection ) ; break ; case WebViewType . IE : case WebViewType . Edge : case WebViewType . EdgeChromium : { const webViewString = webViewType as string ; await registry . addStringValue ( key , WebViewSelection , webViewString ) ; break ; } default : throw new ExpectedError ( `The webViewType ${ webViewType } is not supported.` ) ; } } function toWebViewType ( webViewString ? ) { switch ( webViewString ? webViewString . toLowerCase ( ) : undefined ) { case "ie" : return WebViewType . IE ; case "edge" : return WebViewType . Edge ; case "edge chromium" : return WebViewType . EdgeChromium ; default : return undefined ; } } export function toWebViewTypeName ( webViewType ? ) { switch ( webViewType ) { case WebViewType . Edge : return "legacy Microsoft Edge (EdgeHTML)" ; case WebViewType . EdgeChromium : return "Microsoft Edge (Chromium)" ; case WebViewType . IE : return "Microsoft Internet Explorer" ; default : return undefined ; } } export async function unregisterAddIn ( addinId , manifestPath ) { const key = new registry . RegistryKey ( `${ DeveloperSettingsRegistryKey }` ) ; if ( addinId ) { await registry . deleteValue ( key , addinId ) ; } if ( manifestPath ) { await registry . deleteValue ( key , manifestPath ) ; } } export async function unregisterAllAddIns ( ) { const key = new registry . RegistryKey ( `${ DeveloperSettingsRegistryKey }` ) ; const values = await registry . getValues ( key ) ; for ( const value of values ) { await registry . deleteValue ( key , value . name ) ; } }