From db864cee1a658244033494c4635af8154e50347a Mon Sep 17 00:00:00 2001 From: Akshaya M Date: Sun, 22 Mar 2020 23:54:24 +0530 Subject: [PATCH] Code refactor --- lib/PowerShell/ServicePrincipalLogin.js | 22 +----------- lib/PowerShell/Utilities/Utils.js | 35 +++++++++++++++++++ src/PowerShell/ServicePrincipalLogin.ts | 21 +---------- .../Utilities/PowerShellToolRunner.ts | 1 + src/PowerShell/Utilities/Utils.ts | 25 +++++++++++++ 5 files changed, 63 insertions(+), 41 deletions(-) diff --git a/lib/PowerShell/ServicePrincipalLogin.js b/lib/PowerShell/ServicePrincipalLogin.js index babeb9da..59f5cc58 100644 --- a/lib/PowerShell/ServicePrincipalLogin.js +++ b/lib/PowerShell/ServicePrincipalLogin.js @@ -34,31 +34,11 @@ class ServicePrincipalLogin { initialize() { return __awaiter(this, void 0, void 0, function* () { Utils_1.default.setPSModulePath(); - const script = new ScriptBuilder_1.default().getLatestModuleScript(Constants_1.default.moduleName); - const outputJson = yield this.getLatestModule(script); - const azLatestVersion = outputJson[Constants_1.default.AzVersion]; - if (!(Constants_1.default.Success in outputJson) || !Utils_1.default.isValidVersion(azLatestVersion)) { - throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`); - } + const azLatestVersion = yield Utils_1.default.getLatestModule(Constants_1.default.moduleName); core.debug(`Az Module version used: ${azLatestVersion}`); Utils_1.default.setPSModulePath(`${Constants_1.default.prefix}${azLatestVersion}`); }); } - getLatestModule(script) { - return __awaiter(this, void 0, void 0, function* () { - let output = ""; - const options = { - listeners: { - stdout: (data) => { - output += data.toString(); - } - } - }; - yield PowerShellToolRunner_1.default.init(); - yield PowerShellToolRunner_1.default.executePowerShellScriptBlock(script, options); - return JSON.parse(output.trim()); - }); - } login() { return __awaiter(this, void 0, void 0, function* () { let output = ""; diff --git a/lib/PowerShell/Utilities/Utils.js b/lib/PowerShell/Utilities/Utils.js index 6314f6b2..0bddddb3 100644 --- a/lib/PowerShell/Utilities/Utils.js +++ b/lib/PowerShell/Utilities/Utils.js @@ -1,4 +1,13 @@ "use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; @@ -12,6 +21,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const os = __importStar(require("os")); const Constants_1 = __importDefault(require("../Constants")); +const ScriptBuilder_1 = __importDefault(require("./ScriptBuilder")); +const PowerShellToolRunner_1 = __importDefault(require("./PowerShellToolRunner")); class Utils { static setPSModulePath(azPSVersion = "") { let modulePath = ""; @@ -32,6 +43,30 @@ class Utils { } process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`; } + static getLatestModule(moduleName) { + return __awaiter(this, void 0, void 0, function* () { + let output = ""; + const options = { + listeners: { + stdout: (data) => { + output += data.toString(); + } + } + }; + yield PowerShellToolRunner_1.default.init(); + yield PowerShellToolRunner_1.default.executePowerShellScriptBlock(new ScriptBuilder_1.default() + .getLatestModuleScript(moduleName), options); + const outputJson = JSON.parse(output.trim()); + if (!(Constants_1.default.Success in outputJson)) { + throw new Error(outputJson[Constants_1.default.Error]); + } + const azLatestVersion = outputJson[Constants_1.default.AzVersion]; + if (!Utils.isValidVersion(azLatestVersion)) { + throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`); + } + return azLatestVersion; + }); + } static isValidVersion(version) { return !!version.match(Constants_1.default.versionPattern); } diff --git a/src/PowerShell/ServicePrincipalLogin.ts b/src/PowerShell/ServicePrincipalLogin.ts index 181125a0..1bb27daa 100644 --- a/src/PowerShell/ServicePrincipalLogin.ts +++ b/src/PowerShell/ServicePrincipalLogin.ts @@ -23,30 +23,11 @@ export class ServicePrincipalLogin implements IAzurePowerShellSession { async initialize() { Utils.setPSModulePath(); - const script: string = new ScriptBuilder().getLatestModuleScript(Constants.moduleName); - const outputJson = await this.getLatestModule(script); - const azLatestVersion: string = outputJson[Constants.AzVersion]; - if (!(Constants.Success in outputJson) || !Utils.isValidVersion(azLatestVersion)) { - throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`); - } + const azLatestVersion: string = await Utils.getLatestModule(Constants.moduleName); core.debug(`Az Module version used: ${azLatestVersion}`); Utils.setPSModulePath(`${Constants.prefix}${azLatestVersion}`); } - private async getLatestModule(script: string): Promise { - let output: string = ""; - const options: any = { - listeners: { - stdout: (data: Buffer) => { - output += data.toString(); - } - } - }; - await PowerShellToolRunner.init(); - await PowerShellToolRunner.executePowerShellScriptBlock(script, options); - return JSON.parse(output.trim()); - } - async login() { let output: string = ""; const options: any = { diff --git a/src/PowerShell/Utilities/PowerShellToolRunner.ts b/src/PowerShell/Utilities/PowerShellToolRunner.ts index 648eddbf..2f363f74 100644 --- a/src/PowerShell/Utilities/PowerShellToolRunner.ts +++ b/src/PowerShell/Utilities/PowerShellToolRunner.ts @@ -17,4 +17,5 @@ export default class PowerShellToolRunner { static async executePowerShellCommand(command: string, options: any = {}) { await exec.exec(`"${PowerShellToolRunner.psPath}" -Command "${command}"`, [], options); } + } \ No newline at end of file diff --git a/src/PowerShell/Utilities/Utils.ts b/src/PowerShell/Utilities/Utils.ts index ac1ff55f..c8b4a00c 100644 --- a/src/PowerShell/Utilities/Utils.ts +++ b/src/PowerShell/Utilities/Utils.ts @@ -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 { + 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); }