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

@@ -34,31 +34,11 @@ class ServicePrincipalLogin {
initialize() { initialize() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
Utils_1.default.setPSModulePath(); Utils_1.default.setPSModulePath();
const script = new ScriptBuilder_1.default().getLatestModuleScript(Constants_1.default.moduleName); const azLatestVersion = yield Utils_1.default.getLatestModule(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}`);
}
core.debug(`Az Module version used: ${azLatestVersion}`); core.debug(`Az Module version used: ${azLatestVersion}`);
Utils_1.default.setPSModulePath(`${Constants_1.default.prefix}${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() { login() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let output = ""; let output = "";

View File

@@ -1,4 +1,13 @@
"use strict"; "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) { var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod; if (mod && mod.__esModule) return mod;
var result = {}; var result = {};
@@ -12,6 +21,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const os = __importStar(require("os")); const os = __importStar(require("os"));
const Constants_1 = __importDefault(require("../Constants")); const Constants_1 = __importDefault(require("../Constants"));
const ScriptBuilder_1 = __importDefault(require("./ScriptBuilder"));
const PowerShellToolRunner_1 = __importDefault(require("./PowerShellToolRunner"));
class Utils { class Utils {
static setPSModulePath(azPSVersion = "") { static setPSModulePath(azPSVersion = "") {
let modulePath = ""; let modulePath = "";
@@ -32,6 +43,30 @@ class Utils {
} }
process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`; 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) { static isValidVersion(version) {
return !!version.match(Constants_1.default.versionPattern); return !!version.match(Constants_1.default.versionPattern);
} }

View File

@@ -23,30 +23,11 @@ export class ServicePrincipalLogin implements IAzurePowerShellSession {
async initialize() { async initialize() {
Utils.setPSModulePath(); Utils.setPSModulePath();
const script: string = new ScriptBuilder().getLatestModuleScript(Constants.moduleName); const azLatestVersion: string = await Utils.getLatestModule(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}`);
}
core.debug(`Az Module version used: ${azLatestVersion}`); core.debug(`Az Module version used: ${azLatestVersion}`);
Utils.setPSModulePath(`${Constants.prefix}${azLatestVersion}`); Utils.setPSModulePath(`${Constants.prefix}${azLatestVersion}`);
} }
private async getLatestModule(script: string): Promise<any> {
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() { async login() {
let output: string = ""; let output: string = "";
const options: any = { const options: any = {

View File

@@ -17,4 +17,5 @@ export default class PowerShellToolRunner {
static async executePowerShellCommand(command: string, options: any = {}) { static async executePowerShellCommand(command: string, options: any = {}) {
await exec.exec(`"${PowerShellToolRunner.psPath}" -Command "${command}"`, [], options); await exec.exec(`"${PowerShellToolRunner.psPath}" -Command "${command}"`, [], options);
} }
} }

View File

@@ -1,6 +1,8 @@
import * as os from 'os'; import * as os from 'os';
import Constants from '../Constants'; import Constants from '../Constants';
import ScriptBuilder from './ScriptBuilder';
import PowerShellToolRunner from './PowerShellToolRunner';
export default class Utils { export default class Utils {
static setPSModulePath(azPSVersion: string = "") { static setPSModulePath(azPSVersion: string = "") {
@@ -23,6 +25,29 @@ export default class Utils {
process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`; 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 { static isValidVersion(version: string): boolean {
return !!version.match(Constants.versionPattern); return !!version.match(Constants.versionPattern);
} }