simple-stdout 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -4
- package/dist/index.d.ts +9 -0
- package/dist/index.js +25 -9
- package/dist/index.js.map +1 -1
- package/package.json +5 -2
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -73
- package/dist/index.test.js.map +0 -1
package/README.md
CHANGED
|
@@ -2,15 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
Run a shell command and get the `stdout` as a string.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```javascript
|
|
6
|
+
const name = await stdout('git config --get user.name')
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
That's it. Just a tiny library with no frills.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
6
12
|
|
|
7
13
|
```sh
|
|
8
|
-
pnpm
|
|
14
|
+
pnpm add simple-stdout
|
|
15
|
+
|
|
16
|
+
# or
|
|
17
|
+
yarn add simple-stdout
|
|
18
|
+
|
|
19
|
+
# or
|
|
20
|
+
npm install simple-stdout
|
|
9
21
|
```
|
|
10
22
|
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
11
25
|
```javascript
|
|
12
26
|
import stdout from 'simple-stdout'
|
|
13
27
|
|
|
14
|
-
|
|
15
|
-
//
|
|
28
|
+
const name = await stdout('git config --get user.name')
|
|
29
|
+
// 'Gérald Genta'
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Need `execFile` instead? (Safer for untrusted input, no shell injection worries.)
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import { stdoutFile } from 'simple-stdout'
|
|
36
|
+
|
|
37
|
+
const name = await stdoutFile('git', ['config', '--get', 'user.name'])
|
|
38
|
+
// 'Gérald Genta'
|
|
16
39
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
/**
|
|
2
3
|
* Returns stdout from the given shell command
|
|
4
|
+
*
|
|
5
|
+
* Calls `child_process.exec` under the hood.
|
|
3
6
|
*/
|
|
4
7
|
export default function stdout(command: string): Promise<string>;
|
|
8
|
+
/**
|
|
9
|
+
* Returns stdout from executing the given file/executable
|
|
10
|
+
*
|
|
11
|
+
* Calls `child_process.execFile` under the hood.
|
|
12
|
+
*/
|
|
13
|
+
export declare function stdoutFile(file: string, args?: string[]): Promise<string>;
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { exec, execFile } from 'node:child_process';
|
|
3
|
+
const callback = (resolve, reject) => (err, stdout, stderr) => {
|
|
4
|
+
if (err || stderr) {
|
|
5
|
+
reject(err || new Error(stderr));
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
resolve(stdout
|
|
9
|
+
// Remove trailing newline
|
|
10
|
+
.replace(/\n$/, ''));
|
|
11
|
+
}
|
|
12
|
+
};
|
|
2
13
|
/**
|
|
3
14
|
* Returns stdout from the given shell command
|
|
15
|
+
*
|
|
16
|
+
* Calls `child_process.exec` under the hood.
|
|
4
17
|
*/
|
|
5
18
|
export default async function stdout(command) {
|
|
6
19
|
return new Promise((resolve, reject) => {
|
|
7
|
-
exec(command, (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
exec(command, callback(resolve, reject));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns stdout from executing the given file/executable
|
|
25
|
+
*
|
|
26
|
+
* Calls `child_process.execFile` under the hood.
|
|
27
|
+
*/
|
|
28
|
+
export async function stdoutFile(file, args = []) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
execFile(file, args, callback(resolve, reject));
|
|
15
31
|
});
|
|
16
32
|
}
|
|
17
33
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAGA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAEnD,MAAM,QAAQ,GACV,CAAC,OAAsD,EAAE,MAAkC,EAAE,EAAE,CAC/F,CAAC,GAA6C,EAAE,MAAc,EAAE,MAAc,EAAE,EAAE;IAC9E,IAAI,GAAG,IAAI,MAAM,EAAE,CAAC;QAChB,MAAM,CAAC,GAAG,IAAI,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;IACpC,CAAC;SAAM,CAAC;QACJ,OAAO,CACH,MAAM;YACF,0BAA0B;aACzB,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAC1B,CAAA;IACL,CAAC;AACL,CAAC,CAAA;AAEL;;;;GAIG;AACH,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,MAAM,CAAC,OAAe;IAChD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACnC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAA;IAC5C,CAAC,CAAC,CAAA;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAY,EAAE,OAAiB,EAAE;IAC9D,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACnC,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAA;IACnD,CAAC,CAAC,CAAA;AACN,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simple-stdout",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"scripts": {
|
|
5
|
-
"build": "tsc",
|
|
5
|
+
"build": "tsc -p tsconfig.build.json",
|
|
6
6
|
"test": "vitest .",
|
|
7
7
|
"test:once": "vitest run",
|
|
8
8
|
"format": "prettier --write --list-different .",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
},
|
|
16
16
|
"type": "module",
|
|
17
17
|
"main": "./dist/index.js",
|
|
18
|
+
"bin": {
|
|
19
|
+
"simple-stdout": "./dist/index.js"
|
|
20
|
+
},
|
|
18
21
|
"files": [
|
|
19
22
|
"dist"
|
|
20
23
|
],
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { existsSync, unlinkSync } from 'node:fs';
|
|
2
|
-
import { tmpdir } from 'node:os';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
import { describe, expect, it } from 'vitest';
|
|
5
|
-
import stdout from './index.js';
|
|
6
|
-
describe('stdout()', () => {
|
|
7
|
-
it('should run a command and return its stdout', async () => {
|
|
8
|
-
const result = await stdout('echo "Hello, world!"');
|
|
9
|
-
expect(result).toBe('Hello, world!');
|
|
10
|
-
});
|
|
11
|
-
it('should remove trailing newline', async () => {
|
|
12
|
-
const result = await stdout('echo "No trailing newline"');
|
|
13
|
-
expect(result).toBe('No trailing newline');
|
|
14
|
-
});
|
|
15
|
-
it('should reject on error', async () => {
|
|
16
|
-
await expect(stdout('nonexistent-command')).rejects.toThrow();
|
|
17
|
-
});
|
|
18
|
-
it('should reject on empty command', async () => {
|
|
19
|
-
await expect(stdout('')).rejects.toThrow();
|
|
20
|
-
});
|
|
21
|
-
it('should reject on stderr output', async () => {
|
|
22
|
-
await expect(stdout('echo "This will cause an error" >&2')).rejects.toThrow();
|
|
23
|
-
});
|
|
24
|
-
it('should handle commands with arguments', async () => {
|
|
25
|
-
const result = await stdout('echo "Argument test"');
|
|
26
|
-
expect(result).toBe('Argument test');
|
|
27
|
-
});
|
|
28
|
-
it('should handle commands with multiple lines', async () => {
|
|
29
|
-
const result = await stdout('echo "Line 1\nLine 2"');
|
|
30
|
-
expect(result).toBe('Line 1\nLine 2');
|
|
31
|
-
});
|
|
32
|
-
it('should handle commands with special characters', async () => {
|
|
33
|
-
const result = await stdout('echo "Special characters: !@#$%^&*()"');
|
|
34
|
-
expect(result).toBe('Special characters: !@#$%^&*()');
|
|
35
|
-
});
|
|
36
|
-
it('should handle commands with spaces', async () => {
|
|
37
|
-
const result = await stdout('echo "Command with spaces"');
|
|
38
|
-
expect(result).toBe('Command with spaces');
|
|
39
|
-
});
|
|
40
|
-
it('should handle commands with quotes', async () => {
|
|
41
|
-
const result = await stdout('echo "Quoted text"');
|
|
42
|
-
expect(result).toBe('Quoted text');
|
|
43
|
-
});
|
|
44
|
-
it('should handle commands with multiple arguments', async () => {
|
|
45
|
-
const result = await stdout('echo "Multiple" "Arguments"');
|
|
46
|
-
expect(result).toBe('Multiple Arguments');
|
|
47
|
-
});
|
|
48
|
-
it('should handle commands with environment variables', async () => {
|
|
49
|
-
const result = await stdout('echo $HOME');
|
|
50
|
-
expect(result).toBe(process.env.HOME || '');
|
|
51
|
-
});
|
|
52
|
-
it('should handle commands with pipes', async () => {
|
|
53
|
-
const result = await stdout('echo "Piped output" | sed "s/Piped/Modified/"');
|
|
54
|
-
expect(result).toBe('Modified output');
|
|
55
|
-
});
|
|
56
|
-
it('should handle commands with redirection', async () => {
|
|
57
|
-
const tempFile = join(tmpdir(), `stdout-test-${process.pid}.txt`);
|
|
58
|
-
const result = await stdout(`echo "Redirected output" > ${tempFile} && cat ${tempFile}`);
|
|
59
|
-
expect(result).toBe('Redirected output');
|
|
60
|
-
// Clean up
|
|
61
|
-
if (existsSync(tempFile))
|
|
62
|
-
unlinkSync(tempFile);
|
|
63
|
-
});
|
|
64
|
-
it('should handle commands with subshells', async () => {
|
|
65
|
-
const result = await stdout('echo $(echo "Subshell output")');
|
|
66
|
-
expect(result).toBe('Subshell output');
|
|
67
|
-
});
|
|
68
|
-
it('should handle commands with background processes', async () => {
|
|
69
|
-
const result = await stdout('echo "Background process" & wait');
|
|
70
|
-
expect(result).toBe('Background process');
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
//# sourceMappingURL=index.test.js.map
|
package/dist/index.test.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,MAAM,MAAM,YAAY,CAAA;AAE/B,QAAQ,CAAC,UAAU,EAAE,GAAG,EAAE;IACtB,EAAE,CAAC,4CAA4C,EAAE,KAAK,IAAI,EAAE;QACxD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,sBAAsB,CAAC,CAAA;QAEnD,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,gCAAgC,EAAE,KAAK,IAAI,EAAE;QAC5C,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,4BAA4B,CAAC,CAAA;QAEzD,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAA;IAC9C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,wBAAwB,EAAE,KAAK,IAAI,EAAE;QACpC,MAAM,MAAM,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC,CAAC,OAAO,CA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