Skip to content

Commit

Permalink
simplify os detection and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-paulger-sonarsource committed Apr 16, 2024
1 parent ff7b7e7 commit 020d58d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 55 deletions.
7 changes: 1 addition & 6 deletions src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ let logLevel = DEFAULT_LOG_LEVEL;

export function log(level: LogLevel, ...message: unknown[]) {
if (logLevelValues[level] <= logLevelValues[logLevel]) {
const messageStr = message
.map(m => {
return typeof m === 'object' ? JSON.stringify(m) : m;
})
.join(' ');
console.log(`[${level}] Bootstrapper ${messageStr}`);
console.log(`[${level}] Bootstrapper:: `, ...message);
}
}

Expand Down
38 changes: 3 additions & 35 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,7 @@ export function getArch(): NodeJS.Architecture {
}

function isLinux(): boolean {
return /^linux/.test(process.platform);
}

function isSupportedLinux(): SupportedOS | false {
const linuxMapping: { [nodePlatform: string]: SupportedOS } = {
linux: isAlpineLinux() ? 'alpine' : 'linux',
openbsd: 'linux',
sunos: 'linux',
freebsd: 'linux',
};

return linuxMapping[process.platform] || false;
}

function isWindows(): boolean {
return /^win/.test(process.platform);
}

function isMac(): boolean {
return /^darwin/.test(process.platform);
return process.platform.startsWith('linux');
}

/**
Expand All @@ -66,27 +47,14 @@ function isAlpineLinux(): boolean {
const fileContent = fs.readFileSync('/usr/lib/os-release');
content = fileContent.toString();
} catch (error) {
log(LogLevel.ERROR, 'Failed to read /etc/os-release or /usr/lib/os-release');
log(LogLevel.WARN, 'Failed to read /etc/os-release or /usr/lib/os-release');
}
}
return !!content && (content.match(/^ID=([^\u001b\r\n]*)/m) || [])[1] === 'alpine';
}

function getSupportedOS(): SupportedOS {
if (isWindows()) {
return 'windows';
}

if (isMac()) {
return 'macos';
}

const supportedLinux = isSupportedLinux();
if (supportedLinux) {
return supportedLinux;
}

throw new Error(`Unsupported platform: ${process.platform}`);
return isAlpineLinux() ? 'alpine' : process.platform;
}

export function getPlatformInfo(): PlatformInfo {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import { LogLevel } from './logging';

export type SupportedOS = 'windows' | 'linux' | 'alpine' | 'macos' | 'aix';
export type SupportedOS = NodeJS.Platform | 'alpine';

export type PlatformInfo = {
os: SupportedOS | null;
Expand Down
17 changes: 4 additions & 13 deletions test/unit/platform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import * as logging from '../../src/logging';
import fs from 'fs';
import sinon from 'sinon';

// Mock the getPlatform function

describe('getPlatformInfo', () => {
it('detect macos', () => {
const platformStub = sinon.stub(process, 'platform').value('darwin');
const archStub = sinon.stub(process, 'arch').value('arm64');

expect(platform.getPlatformInfo()).toEqual({
os: 'macos',
os: 'darwin',
arch: 'arm64',
});

Expand All @@ -44,20 +42,20 @@ describe('getPlatformInfo', () => {
const archStub = sinon.stub(process, 'arch').value('x64');

expect(platform.getPlatformInfo()).toEqual({
os: 'windows',
os: 'win32',
arch: 'x64',
});

platformStub.restore();
archStub.restore();
});

it('detect supported linux', () => {
it('detect linux flavor', () => {
const platformStub = sinon.stub(process, 'platform').value('openbsd');
const archStub = sinon.stub(process, 'arch').value('x64');

expect(platform.getPlatformInfo()).toEqual({
os: 'linux',
os: 'openbsd',
arch: 'x64',
});

Expand Down Expand Up @@ -118,11 +116,4 @@ describe('getPlatformInfo', () => {
archStub.restore();
logSpy.restore();
});

it('throw if unsupported', () => {
const stub = sinon.stub(process, 'platform').value('android');

expect(() => platform.getPlatformInfo()).toThrow('Unsupported platform: android');
stub.restore();
});
});

0 comments on commit 020d58d

Please sign in to comment.