Code refactor

This commit is contained in:
Akshaya M
2020-03-22 23:54:24 +05:30
parent 0935db3306
commit db864cee1a
5 changed files with 63 additions and 41 deletions

View File

@@ -1,6 +1,8 @@
import * as os from 'os';
import Constants from '../Constants';
import ScriptBuilder from './ScriptBuilder';
import PowerShellToolRunner from './PowerShellToolRunner';
export default class Utils {
static setPSModulePath(azPSVersion: string = "") {
@@ -23,6 +25,29 @@ export default class Utils {
process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`;
}
static async getLatestModule(moduleName: string): Promise<any> {
let output: string = "";
const options: any = {
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
}
}
};
await PowerShellToolRunner.init();
await PowerShellToolRunner.executePowerShellScriptBlock(new ScriptBuilder()
.getLatestModuleScript(moduleName), options);
const outputJson = JSON.parse(output.trim());
if (!(Constants.Success in outputJson)) {
throw new Error(outputJson[Constants.Error]);
}
const azLatestVersion: string = outputJson[Constants.AzVersion];
if (!Utils.isValidVersion(azLatestVersion)) {
throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`);
}
return azLatestVersion;
}
static isValidVersion(version: string): boolean {
return !!version.match(Constants.versionPattern);
}