From 6d92b9bd5393d09715a60dd0726c5b2382cab757 Mon Sep 17 00:00:00 2001 From: Nikolai Laevskii Date: Fri, 12 May 2023 14:07:46 +0200 Subject: [PATCH] Refactor install dir computation --- dist/index.js | 37 ++++++++++++++++++------------------- src/installer.ts | 36 ++++++++++++------------------------ src/utils.ts | 5 +++++ 3 files changed, 35 insertions(+), 43 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8e20d7f..aa4b7a5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -419,24 +419,15 @@ class DotnetCoreInstaller { exports.DotnetCoreInstaller = DotnetCoreInstaller; _a = DotnetCoreInstaller; (() => { - const installationDirectoryWindows = path_1.default.join(process.env['PROGRAMFILES'] + '', 'dotnet'); - const installationDirectoryLinux = '/usr/share/dotnet'; - const installationDirectoryMac = path_1.default.join(process.env['HOME'] + '', '.dotnet'); - const dotnetInstallDir = process.env['DOTNET_INSTALL_DIR']; - if (dotnetInstallDir) { - process.env['DOTNET_INSTALL_DIR'] = - _a.convertInstallPathToAbsolute(dotnetInstallDir); - } - else { - if (utils_1.IS_WINDOWS) { - process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows; - } - else { - process.env['DOTNET_INSTALL_DIR'] = utils_1.IS_LINUX - ? installationDirectoryLinux - : installationDirectoryMac; - } - } + const dotnetInstallDirDefault = { + linux: '/usr/share/dotnet', + mac: path_1.default.join(process.env['HOME'] + '', '.dotnet'), + windows: path_1.default.join(process.env['PROGRAMFILES'] + '', 'dotnet') + }[(0, utils_1.getPlatform)()]; + const dotnetInstallDir = process.env['DOTNET_INSTALL_DIR'] + ? _a.convertInstallPathToAbsolute(process.env['DOTNET_INSTALL_DIR']) + : dotnetInstallDirDefault; + process.env['DOTNET_INSTALL_DIR'] = dotnetInstallDir; })(); @@ -591,9 +582,17 @@ run(); "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.IS_LINUX = exports.IS_WINDOWS = void 0; +exports.getPlatform = exports.IS_LINUX = exports.IS_WINDOWS = void 0; exports.IS_WINDOWS = process.platform === 'win32'; exports.IS_LINUX = process.platform === 'linux'; +const getPlatform = () => { + if (exports.IS_WINDOWS) + return 'windows'; + if (exports.IS_LINUX) + return 'linux'; + return 'mac'; +}; +exports.getPlatform = getPlatform; /***/ }), diff --git a/src/installer.ts b/src/installer.ts index ef9e993..b2132ef 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -8,7 +8,7 @@ import {readdir} from 'fs/promises'; import path from 'path'; import os from 'os'; import semver from 'semver'; -import {IS_LINUX, IS_WINDOWS} from './utils'; +import {IS_WINDOWS, getPlatform} from './utils'; import {QualityOptions} from './setup-dotnet'; export interface DotnetVersion { @@ -115,29 +115,17 @@ export class DotnetCoreInstaller { private quality: QualityOptions; static { - const installationDirectoryWindows = path.join( - process.env['PROGRAMFILES'] + '', - 'dotnet' - ); - const installationDirectoryLinux = '/usr/share/dotnet'; - const installationDirectoryMac = path.join( - process.env['HOME'] + '', - '.dotnet' - ); - const dotnetInstallDir: string | undefined = - process.env['DOTNET_INSTALL_DIR']; - if (dotnetInstallDir) { - process.env['DOTNET_INSTALL_DIR'] = - this.convertInstallPathToAbsolute(dotnetInstallDir); - } else { - if (IS_WINDOWS) { - process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows; - } else { - process.env['DOTNET_INSTALL_DIR'] = IS_LINUX - ? installationDirectoryLinux - : installationDirectoryMac; - } - } + const dotnetInstallDirDefault = { + linux: '/usr/share/dotnet', + mac: path.join(process.env['HOME'] + '', '.dotnet'), + windows: path.join(process.env['PROGRAMFILES'] + '', 'dotnet') + }[getPlatform()]; + + const dotnetInstallDir = process.env['DOTNET_INSTALL_DIR'] + ? this.convertInstallPathToAbsolute(process.env['DOTNET_INSTALL_DIR']) + : dotnetInstallDirDefault; + + process.env['DOTNET_INSTALL_DIR'] = dotnetInstallDir; } constructor(version: string, quality: QualityOptions) { diff --git a/src/utils.ts b/src/utils.ts index 77886ce..d671d70 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,2 +1,7 @@ export const IS_WINDOWS = process.platform === 'win32'; export const IS_LINUX = process.platform === 'linux'; +export const getPlatform = (): 'windows' | 'linux' | 'mac' => { + if (IS_WINDOWS) return 'windows'; + if (IS_LINUX) return 'linux'; + return 'mac'; +};