Skip to content

Commit

Permalink
Extract Go/NodeJS OS/arch translation logic (#532)
Browse files Browse the repository at this point in the history
* Extract Go/NodeJS OS/arch translation logic

* Move goOs & goArch logic into functions
  • Loading branch information
radeksimko authored Jan 5, 2021
1 parent b6ef1bf commit 1bb5ed1
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions src/languageServerInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,14 @@ export class LanguageServerInstaller {
const destination = `${installDir}/terraform-ls_v${release.version}.zip`;
fs.mkdirSync(installDir, { recursive: true }); // create install directory if missing

let platform = process.platform.toString();
if (platform === 'win32') {
platform = 'windows';
}
let arch: string;
switch (process.arch) {
case 'x64':
arch = 'amd64'
break;
case 'ia32':
arch = '386'
break;
case 'arm64':
if (platform === 'darwin') {
// On Apple Silicon, install the amd64 version and rely on Rosetta2
// until a native build is available.
arch = 'amd64'
}
break;
}
const build = release.getBuild(platform, arch);
let os = goOs();
let arch = goArch();
const build = release.getBuild(os, arch);
if (!build) {
throw new Error("Install error: no matching terraform-ls binary for platform");
throw new Error(`Install error: no matching terraform-ls binary for ${os}/${arch}`);
}
try {
this.removeOldBinary(installDir, platform);
this.removeOldBinary(installDir, os);
} catch {
// ignore missing binary (new install)
}
Expand All @@ -96,8 +78,8 @@ export class LanguageServerInstaller {
});
}

removeOldBinary(directory: string, platform: string): void {
if (platform === "windows") {
removeOldBinary(directory: string, goOs: string): void {
if (goOs === "windows") {
fs.unlinkSync(`${directory}/terraform-ls.exe`);
} else {
fs.unlinkSync(`${directory}/terraform-ls`);
Expand All @@ -108,3 +90,31 @@ export class LanguageServerInstaller {
return del(`${directory}/terraform-ls*.zip`, { force: true });
}
}

function goOs(): string {
let platform = process.platform.toString();
if (platform === 'win32') {
return 'windows';
}
if (platform === 'sunos') {
return 'solaris';
}
return platform;
}

function goArch(): string {
let arch = process.arch;

if (arch === 'ia32') {
return '386';
}
if (arch === 'x64') {
return 'amd64';
}
if (arch === 'arm64' && process.platform.toString() === 'darwin') {
// On Apple Silicon, install the amd64 version and rely on Rosetta2
// until a native build is available.
return 'amd64';
}
return arch;
}

0 comments on commit 1bb5ed1

Please sign in to comment.