@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
163 lines (161 loc) • 7.05 kB
JavaScript
import { LogLevel } from '../diagnostics/log-level';
import { EnvironmentModule } from '../manifest/environment-modules';
import { ManifestLoader } from '../manifest/manifest-loader';
import { PerformanceTracker } from '../performance/performance-tracker';
import { AccessibilityManager } from './accessibility-manager';
import { AssetManager } from './asset-manager';
import { LocalizationManager } from './localization-manager';
import { NativeQ } from './native-q';
import { PowerShell } from './powershell';
/**
* The enum switch to use predefined endpoints.
*/
export var RuntimePowerShellEndpoint;
(function (RuntimePowerShellEndpoint) {
/**
* Not specify.
*/
RuntimePowerShellEndpoint[RuntimePowerShellEndpoint["InlineScript"] = 0] = "InlineScript";
/**
* The default endpoint. Microsoft.PowerShell
*/
RuntimePowerShellEndpoint[RuntimePowerShellEndpoint["Default"] = 1] = "Default";
/**
* The SME endpoint. Microsoft.SME.PowerShell
*/
RuntimePowerShellEndpoint[RuntimePowerShellEndpoint["Sme"] = 2] = "Sme";
})(RuntimePowerShellEndpoint || (RuntimePowerShellEndpoint = {}));
/**
* Class to initialize and the SME environment
* (Localized string cannot be used in this class due to initialization phase when the strings are not ready yet.)
* @dynamic
*/
export class CoreEnvironment {
/**
* The localization manager once the environment has been initialized
*/
static localizationManager;
static accessibilityManager;
static assetManager;
/**
* Initializes the environment with manifest loading mode.
*
* @param manifestOptions the name of manifest loading options.
* @param localizationOptions the options to initialize the Localization Manager
* @param runtimeOptions the optional runtime options.
*/
static initialize(manifestOptions, localizationOptions, runtimeOptions) {
if (!manifestOptions || !manifestOptions.name) {
// no localization.
throw new Error('CoreEnvironment.initialize() - Argument error: manifestOptions.');
}
PerformanceTracker.coreEnvironmentInitStarted();
PerformanceTracker.initializeLighthouseMetricsTrackers();
const InitOptions = {
mode: 1 /* MsftSme.EnvironmentMode.LoadEmbedded */,
moduleName: manifestOptions.name,
moduleVersion: manifestOptions.version,
powerShellModuleName: manifestOptions.powerShellModuleName,
powerShellPrefix: manifestOptions.powerShellPrefix,
isProduction: manifestOptions.isProduction,
sessionId: 'N/A',
logLevel: MsftSme.consoleDebug() === null ? LogLevel.Warning : MsftSme.consoleDebug(),
sessionExpiration: 0,
performanceProfile: false,
developerGuide: false,
experiments: MsftSme.experiments(),
connectivityLevel: '',
isShell: false,
shellVersion: null,
gatewayApiVersion: null,
gatewayPlatform: null
};
if (manifestOptions.name === EnvironmentModule.nameOfShell) {
// shell manifest loading
InitOptions.isShell = true;
InitOptions.mode = 2 /* MsftSme.EnvironmentMode.Load */;
InitOptions.sessionId = MsftSme.newGuid();
}
else if (manifestOptions.isProduction) {
// module on the production using the same site origin from location information
// if not specified by manifestOptions.shellOrigin.
InitOptions.shellOrigin = manifestOptions && manifestOptions.shellOrigin ? manifestOptions.shellOrigin : window.location.origin;
}
else {
// module side-loading manifest. non production environment accept any shell origin.
InitOptions.shellOrigin = '*';
}
const self = MsftSme.self();
self.Init = InitOptions;
// enable websocket stream query only if requested.
if (runtimeOptions && runtimeOptions.websocket) {
self.Init.websocket = true;
}
// enable SSH websocket stream query only if requested.
if (runtimeOptions && runtimeOptions.sshWebsocket) {
self.Init.sshWebsocket = true;
}
// enable websocket stream query only if requested.
if (runtimeOptions && runtimeOptions.powerShellEndpoint) {
if (typeof runtimeOptions.powerShellEndpoint === 'string') {
self.Init.powerShellEndpoint = runtimeOptions.powerShellEndpoint;
}
else {
const type = runtimeOptions.powerShellEndpoint;
switch (type) {
case RuntimePowerShellEndpoint.Default:
self.Init.powerShellEndpoint = PowerShell.defaultPowerShellEndpoint;
break;
case RuntimePowerShellEndpoint.Sme:
self.Init.powerShellEndpoint = PowerShell.smePowerShellEndpoint;
break;
}
}
}
CoreEnvironment.localizationManager = new LocalizationManager(localizationOptions);
if (!runtimeOptions || !runtimeOptions.disableStyleInjection) {
CoreEnvironment.assetManager = new AssetManager(CoreEnvironment.localizationManager, runtimeOptions?.cssV2);
}
const deferred = NativeQ.defer();
PerformanceTracker.manifestLoadStarted();
ManifestLoader.loadManifest()
.then(() => {
PerformanceTracker.localizationStarted();
return CoreEnvironment.localization();
})
.then(() => {
if (!runtimeOptions || !runtimeOptions.disableAccessibility) {
PerformanceTracker.accessibilityManagerStarted();
CoreEnvironment.accessibilityManager = new AccessibilityManager();
}
deferred.resolve();
})
.catch(deferred.reject)
.finally(() => PerformanceTracker.coreEnvironmentInitCompleted());
return deferred.promise;
}
/**
* Validate and load localized strings if the localeId doesn't match with current locale Id.
* @param localeSet the local information set.
*/
static moduleLoadLocale(localeSet) {
if (CoreEnvironment.localizationManager.localeId.id !== localeSet.id) {
CoreEnvironment.localizationManager.saveLocale(localeSet);
return CoreEnvironment.localization();
}
return NativeQ.resolved();
}
/**
* Initialize and load localization data by option settings.
*/
static localization() {
const self = MsftSme.self();
CoreEnvironment.localizationManager.updateDocumentLanguage();
return CoreEnvironment.localizationManager.fetchLocalizedStrings()
.toPromise()
.then(strings => {
self.Resources.strings = strings;
});
}
}
//# sourceMappingURL=core-environment.js.map