electron-util
Version:
Useful utilities for Electron apps and modules
42 lines (40 loc) • 1.15 kB
JavaScript
import process from 'node:process';
/**
Accepts an object with the keys as either `macos`, `windows`, `linux`, or `default`, and picks the appropriate key depending on the current platform.
If no platform key is matched, the `default` key is used if it exists.
If the value is a function, it will be executed, and the returned value will be used.
@example
```
init({
enableUnicorn: util.platform({
macos: true,
windows: false,
linux: () => false
})
});
```
*/
export const platform = (choices) => {
const { platform: _platform } = process;
let platform;
switch (_platform) {
case 'darwin': {
platform = 'macos';
break;
}
case 'win32': {
platform = 'windows';
break;
}
case 'linux': {
platform = 'linux';
break;
}
default: {
platform = 'default';
}
}
// TODO: This can't return undefined, but TypeScript doesn't know that
const fn = platform in choices ? choices[platform] : choices.default;
return typeof fn === 'function' ? fn() : fn;
};