Skip to content

Commit 3d54c12

Browse files
committed
[javascript] fix: runCommand fix new line added
1 parent 8f7a0d9 commit 3d54c12

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

docs/src/en/reference/os/runCommand.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ It returns the result that is output after entering and executing the command pr
1010

1111
## Returns
1212

13-
> string
13+
> string | null
1414
1515
## Examples
1616

1717
```javascript
18-
console.log(await runCommand('echo a')); // Returns 'a\n'
18+
console.log(await runCommand('echo a')); // Returns 'a'
1919
```

docs/src/ko/reference/os/runCommand.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
## Returns
1212

13-
> string
13+
> string | null
1414
1515
## Examples
1616

1717
```javascript
18-
console.log(await runCommand('echo a')); // Returns 'a\n'
18+
console.log(await runCommand('echo a')); // Returns 'a'
1919
```

packages/javascript/lib/node/os/getMachineId.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export async function getMachineId(): Promise<string> {
1818
}
1919

2020
try {
21-
return await runCommand(command);
21+
const response = await runCommand(command);
22+
23+
if (response) {
24+
return response;
25+
}
2226
} catch (error) {
2327
if (error instanceof Error) {
2428
throw new Error(error?.message);

packages/javascript/lib/node/os/getSid.ts

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export async function getSid(): Promise<string> {
1111
`REG QUERY "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList" /s`
1212
);
1313

14+
if (!profileLists) {
15+
throw new Error('Failed to get machine id');
16+
}
17+
1418
const profiles: AnyValueObject = {};
1519
const sections = profileLists.split('HKEY_LOCAL_MACHINE\\');
1620

packages/javascript/lib/node/os/runCommand.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { exec as processExec } from 'child_process';
2-
import { platform } from 'os';
2+
import { EOL } from 'os';
33

4-
export function runCommand(command: string): Promise<string> {
4+
export function runCommand(command: string): Promise<string | null> {
55
return new Promise((resolve, reject) => {
66
const execCommandProcess = processExec(
77
command,
@@ -12,16 +12,11 @@ export function runCommand(command: string): Promise<string> {
1212
return;
1313
}
1414

15-
const output = platform() === 'win32' ? stdout : stdout.split('\r\n')?.[0] || '';
15+
const output = stdout.split(EOL)?.[0];
1616

1717
execCommandProcess?.stdin?.end();
1818

19-
if (output) {
20-
resolve(output);
21-
return;
22-
}
23-
24-
reject(new Error(`Command failed`));
19+
resolve(output);
2520
}
2621
);
2722
});

packages/javascript/test/os.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { runCommand, getMachineId, getSid } from '../dist/node';
44

55
describe('OS', () => {
66
it('runCommand', async () => {
7-
assert.strictEqual(await runCommand('echo a'), 'a\n');
8-
assert.strictEqual(await runCommand('echo b'), 'b\n');
7+
assert.strictEqual(await runCommand('echo a'), 'a');
8+
assert.strictEqual(await runCommand('echo b'), 'b');
99
});
1010

1111
/*

0 commit comments

Comments
 (0)