UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

91 lines (89 loc) 3.68 kB
import { map } from 'rxjs/operators'; import { EnvironmentModuleToolState } from '../../manifest/environment-modules'; import { SharedCache } from '../shared-cache'; import { ToolInventory } from './tool-inventory'; /** * Tool Inventory cache class. */ export class ToolInventoryCache extends SharedCache { appContext; static uniqueId = '@msft-sme/shell:toolInventory'; static uniqueVersion = 0; /** * Initializes a new instance of the ToolInventoryCache class. * * @param appContext the app context. * @param options the option of shared cache. */ constructor(appContext, options) { super(ToolInventoryCache.uniqueId, ToolInventoryCache.uniqueVersion, (params) => this.dataInstanceId(params), (instance) => this.dataSerialize(instance), (serialized) => this.dataDeserialize(serialized), (params) => this.dataQuery(params), options); this.appContext = appContext; } /** * Defines how to collect the tool inventory data. * * @param params the server inventory query params. * @return the Observable of ServerInventory data. */ dataQuery(params) { const psSession = this.appContext.powerShell.createSession(params.name, null, params.requestOptions); return this.appContext.powerShell.run(psSession, params.command ? { command: params.command, script: params.script, module: params.module } : params.script) .pipe(map((data) => { const inventory = new ToolInventory(params.name); if (data && data.results && data.results.length > 0) { const result = data.results[0]; inventory.name = params.name; inventory.id = params.id; switch (result.state) { case EnvironmentModuleToolState[EnvironmentModuleToolState.Available]: inventory.state = EnvironmentModuleToolState.Available; break; case EnvironmentModuleToolState[EnvironmentModuleToolState.NotConfigured]: inventory.state = EnvironmentModuleToolState.NotConfigured; break; case EnvironmentModuleToolState[EnvironmentModuleToolState.NotSupported]: default: inventory.state = EnvironmentModuleToolState.NotSupported; break; } inventory.message = result.message; inventory.properties = []; const properties = result.properties; if (properties) { for (const pair of properties) { inventory.properties.push(pair); } } } return inventory; })); } /** * Defines how to identify the cache entry by params. * * @param params the tool inventory query params. * @return the id string. */ dataInstanceId(params) { return `${params.name}:${params.id}`; } /** * Defines how to deserialize the class object from seralized data. * * @param serialized the serialized string; */ dataDeserialize(serialized) { const inventory = JSON.parse(serialized); return new ToolInventory(inventory.name, inventory); } /** * Defines how to serialize the class object to seralized data. * * @param instance the class instance. */ dataSerialize(instance) { // automatically stripped out class related data. return JSON.stringify(instance); } } //# sourceMappingURL=tool-inventory-cache.js.map