Skip to content

Commit

Permalink
mod: separate exec command
Browse files Browse the repository at this point in the history
  • Loading branch information
jooy2 committed May 3, 2024
1 parent 1dd550b commit 6fc87ae
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
- '!CHANGELOG.md'
- '!.github/**'
- '!.vscode/**'
- '.github/workflows/app-test.yml'
# trigger deployment manually
workflow_dispatch:

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
82 changes: 46 additions & 36 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,57 @@
import { platform } from 'os';
import { exec as processExec } from 'child_process';

export default class Machini {
static machineId(): Promise<string> {
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<string> =>
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<string> {
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');
}
}

Expand Down

0 comments on commit 6fc87ae

Please sign in to comment.