diff --git a/.github/workflows/test-node.yml b/.github/workflows/test-node.yml index 38fe2fa..fb6e4ea 100644 --- a/.github/workflows/test-node.yml +++ b/.github/workflows/test-node.yml @@ -11,6 +11,7 @@ on: - '!CHANGELOG.md' - '!.github/**' - '!.vscode/**' + - '.github/workflows/app-test.yml' # trigger deployment manually workflow_dispatch: diff --git a/README.md b/README.md index 926e686..159b934 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ `machini` is a utility to get hardware information about system devices. -- Lightweight, zero-dependency, super-fast! +- Lightweight and Fast! +- It doesn't use any additional modules - it utilizes pure NodeJS APIs. - You don't need system privileges to get the value. - Friendly to modern **Node.js** development and [**TypeScript**](https://www.typescriptlang.org). - Cross-platform (Windows, macOS, Unix/Linux) support. diff --git a/lib/index.ts b/lib/index.ts index b23e95c..01efde3 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,47 +1,57 @@ import { platform } from 'os'; import { exec as processExec } from 'child_process'; -export default class Machini { - static machineId(): Promise { - return new Promise((resolve, reject) => { - const platformName = platform(); - let execCommands = ''; - - if (platformName === 'win32') { - execCommands = - 'for /f "tokens=3 delims= " %i in (\'REG QUERY HKLM\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid ^| findstr MachineGuid\') do @echo %i'; - } else if (platformName === 'darwin') { - execCommands = - "ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/' | cut -d '\"' -f4"; - } else if (platformName === 'freebsd') { - execCommands = 'kenv -q smbios.system.uuid || sysctl -n kern.hostuuid'; - } else { - execCommands = - '(cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname) | head -n 1 || :'; - } - - const execProcess = processExec( - execCommands, - { encoding: 'utf8', windowsHide: true }, - (error, stdout) => { - if (error) { - reject(error); - return; - } +const execCommand = (platformName: string, command: string): Promise => + new Promise((resolve, reject) => { + const execCommandProcess = processExec( + command, + { encoding: 'utf8', windowsHide: true }, + (error, stdout) => { + if (error) { + reject(error); + return; + } - const output = platformName === 'win32' ? stdout : stdout.split('\r\n')?.[0] || ''; + const output = platformName === 'win32' ? stdout : stdout.split('\r\n')?.[0] || ''; - if (output) { - resolve(output); - return; - } + execCommandProcess?.stdin?.end(); - reject(new Error(`Failed to get machine id`)); + if (output) { + resolve(output); + return; } - ); - execProcess?.stdin?.end(); - }); + reject(new Error(`Command failed`)); + } + ); + }); + +export default class Machini { + static async machineId(): Promise { + const platformName = platform(); + let command = ''; + + if (platformName === 'win32') { + command = + 'for /f "tokens=3 delims= " %i in (\'REG QUERY HKLM\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid ^| findstr MachineGuid\') do @echo %i'; + } else if (platformName === 'darwin') { + command = "ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/' | cut -d '\"' -f4"; + } else if (platformName === 'freebsd') { + command = 'kenv -q smbios.system.uuid || sysctl -n kern.hostuuid'; + } else { + command = + '(cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname) | head -n 1 || :'; + } + + try { + return await execCommand(platformName, command); + } catch (error) { + if (error instanceof Error) { + throw new Error(error?.message); + } + } + + throw new Error('Failed to get machine id'); } }