magic-nix-cache-action/dist/platform.js

84 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-04-19 16:04:49 +02:00
// MIT, lifted from https://github.com/actions/toolkit/blob/5a736647a123ecf8582376bdaee833fbae5b3847/packages/core/src/platform.ts
// since it isn't in @actions/core 1.10.1 which is their current release as 2024-04-19
import os from "os";
import * as exec from "@actions/exec";
2024-04-19 17:43:40 +02:00
import * as core from "@actions/core";
2024-04-19 16:41:36 +02:00
import { releaseInfo } from "linux-release-info";
2024-04-19 16:04:49 +02:00
const getWindowsInfo = async () => {
const { stdout: version } = await exec.getExecOutput('powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Version"', undefined, {
silent: true,
});
const { stdout: name } = await exec.getExecOutput('powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Caption"', undefined, {
silent: true,
});
return {
name: name.trim(),
version: version.trim(),
};
};
const getMacOsInfo = async () => {
const { stdout } = await exec.getExecOutput("sw_vers", undefined, {
silent: true,
});
const version = stdout.match(/ProductVersion:\s*(.+)/)?.[1] ?? "";
const name = stdout.match(/ProductName:\s*(.+)/)?.[1] ?? "";
return {
name,
version,
};
};
2024-04-19 17:43:40 +02:00
function getPropertyViaWithDefault(data, names, defaultValue) {
for (const name of names) {
2024-04-19 17:59:14 +02:00
const ret = getPropertyWithDefault(data, name, defaultValue);
if (ret !== defaultValue) {
2024-04-19 17:43:40 +02:00
return ret;
}
}
return defaultValue;
}
function getPropertyWithDefault(data, name, defaultValue) {
if (!data.hasOwnProperty(name)) {
return defaultValue;
}
const value = data[name];
// NB. this check won't work for object instances
if (typeof value !== typeof defaultValue) {
return defaultValue;
}
return value;
}
2024-04-19 16:04:49 +02:00
const getLinuxInfo = async () => {
2024-04-19 17:43:40 +02:00
let data = {};
try {
data = releaseInfo({ mode: "sync" });
2024-04-19 17:45:17 +02:00
// eslint-disable-next-line no-console
console.log(data);
2024-04-19 17:43:40 +02:00
}
catch (e) {
core.debug(`Error collecting release info: ${e}`);
}
2024-04-19 16:04:49 +02:00
return {
2024-04-19 17:43:40 +02:00
name: getPropertyViaWithDefault(data, ["id", "name", "pretty_name", "id_like"], "unknown"),
version: getPropertyViaWithDefault(data, ["version_id", "version", "version_codename"], "unknown"),
2024-04-19 16:04:49 +02:00
};
};
export const platform = os.platform();
export const arch = os.arch();
export const isWindows = platform === "win32";
export const isMacOS = platform === "darwin";
export const isLinux = platform === "linux";
export async function getDetails() {
return {
...(await (isWindows
? getWindowsInfo()
: isMacOS
? getMacOsInfo()
: getLinuxInfo())),
platform,
arch,
isWindows,
isMacOS,
isLinux,
};
}