Skip to content

Commit 0726433

Browse files
WardenGnawgregg-miskelly
authored andcommitted
Fixing tsc error TS1252 in platform.ts (#801)
* Fixing tsc error TS1252 in platform.ts. Moved the getValue function within getCurrentPlatform function outside of function scope. Added string array parameter to getValue function because lines was originally a local variable within the getCurrentPlatform function. * Changing getValue function to be Map etc/os-release is a file that has multiple 'key=value' lines. Removing the getValue function and then have the text in os-release to be converted to a map instead does the same thing. * Fixing code review issues Moving key variable into if statement.
1 parent 26cc04a commit 0726433

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/platform.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ export enum Platform {
2020
Ubuntu16
2121
}
2222

23+
function convertOSReleaseTextToMap(text : string) : Map<string, string> {
24+
let ret: Map<string, string> = new Map<string, string>();
25+
const lines: string[] = text.split('\n');
26+
27+
for (let line of lines) {
28+
line = line.trim();
29+
30+
let equalsIndex = line.indexOf('=');
31+
if (equalsIndex >= 0) {
32+
let key = line.substring(0, equalsIndex);
33+
let value = line.substring(equalsIndex + 1);
34+
35+
// Strip double quotes if necessary
36+
if (value.length > 1 && value.startsWith('"') && value.endsWith('"')) {
37+
value = value.substring(1, value.length - 1);
38+
}
39+
40+
ret.set(key, value);
41+
}
42+
}
43+
44+
return ret;
45+
}
46+
47+
2348
export function getCurrentPlatform() {
2449
if (process.platform === 'win32') {
2550
return Platform.Windows;
@@ -32,35 +57,14 @@ export function getCurrentPlatform() {
3257
// For details: https://www.freedesktop.org/software/systemd/man/os-release.html
3358
// When any new distro or version is added, please update GetClrDbg.sh in MIEngine or inform the contributers of MIEngine.
3459
const text = child_process.execSync('cat /etc/os-release').toString();
35-
const lines = text.split('\n');
36-
37-
function getValue(name: string) {
38-
for (let line of lines) {
39-
line = line.trim();
40-
if (line.startsWith(name)) {
41-
const equalsIndex = line.indexOf('=');
42-
if (equalsIndex >= 0) {
43-
let value = line.substring(equalsIndex + 1);
44-
45-
// Strip double quotes if necessary
46-
if (value.length > 1 && value.startsWith('"') && value.endsWith('"')) {
47-
value = value.substring(1, value.length - 1);
48-
}
49-
50-
return value;
51-
}
52-
}
53-
}
54-
55-
return undefined;
56-
}
60+
const osReleaseMap = convertOSReleaseTextToMap(text);
5761

58-
const id = getValue("ID");
62+
const id = osReleaseMap.get("ID");
5963

6064
switch (id)
6165
{
6266
case 'ubuntu':
63-
const versionId = getValue("VERSION_ID");
67+
const versionId = osReleaseMap.get("VERSION_ID");
6468
if (versionId.startsWith("14")) {
6569
// This also works for Linux Mint
6670
return Platform.Ubuntu14;
@@ -84,7 +88,7 @@ export function getCurrentPlatform() {
8488
// Oracle Linux is binary compatible with CentOS
8589
return Platform.CentOS;
8690
case 'elementary OS':
87-
const eOSVersionId = getValue("VERSION_ID");
91+
const eOSVersionId = osReleaseMap.get("VERSION_ID");
8892
if (eOSVersionId.startsWith("0.3")) {
8993
// Elementary OS 0.3 Freya is binary compatible with Ubuntu 14.04
9094
return Platform.Ubuntu14;
@@ -96,7 +100,7 @@ export function getCurrentPlatform() {
96100

97101
break;
98102
case 'linuxmint':
99-
const lmVersionId = getValue("VERSION_ID");
103+
const lmVersionId = osReleaseMap.get("VERSION_ID");
100104
if (lmVersionId.startsWith("18")) {
101105
// Linux Mint 18 is binary compatible with Ubuntu 16.04
102106
return Platform.Ubuntu16;

0 commit comments

Comments
 (0)