simple-stdout 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present Craig Patik
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # simple-stdout
2
+
3
+ Run a shell command and get the `stdout` as a string.
4
+
5
+ ## Usage
6
+
7
+ ```sh
8
+ pnpm install simple-stdout
9
+ ```
10
+
11
+ ```tsx
12
+ import stdout from 'simple-stdout'
13
+
14
+ console.log(await stdout('echo "Hello, world!"'))
15
+ // Logs 'Hello, world!'
16
+ ```
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Returns stdout from the given shell command
3
+ */
4
+ export default function stdout(command: string): Promise<string>;
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ import { exec } from 'node:child_process';
2
+ /**
3
+ * Returns stdout from the given shell command
4
+ */
5
+ export default async function stdout(command) {
6
+ return new Promise((resolve, reject) => {
7
+ exec(command, (err, stdout, stderr) => {
8
+ if (err || stderr) {
9
+ reject(err || new Error(stderr));
10
+ }
11
+ else if (typeof stdout === 'string') {
12
+ resolve(stdout.replace(/\n$/, '')); // Remove trailing newline
13
+ }
14
+ });
15
+ });
16
+ }
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAEzC;;GAEG;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,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;YAClC,IAAI,GAAG,IAAI,MAAM,EAAE,CAAC;gBAChB,MAAM,CAAC,GAAG,IAAI,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAA;YACpC,CAAC;iBAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;gBACpC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAA,CAAC,0BAA0B;YACjE,CAAC;QACL,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACN,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,69 @@
1
+ import { execSync } from 'node:child_process';
2
+ import { describe, expect, it } from 'vitest';
3
+ import stdout from './index.js';
4
+ describe('stdout()', () => {
5
+ it('should run a command and return its stdout', async () => {
6
+ const result = await stdout('echo "Hello, world!"');
7
+ expect(result).toBe('Hello, world!');
8
+ });
9
+ it('should remove trailing newline', async () => {
10
+ const result = await stdout('echo "No trailing newline"');
11
+ expect(result).toBe('No trailing newline');
12
+ });
13
+ it('should reject on error', async () => {
14
+ await expect(stdout('nonexistent-command')).rejects.toThrow();
15
+ });
16
+ it('should reject on empty command', async () => {
17
+ await expect(stdout('')).rejects.toThrow();
18
+ });
19
+ it('should reject on stderr output', async () => {
20
+ await expect(stdout('echo "This will cause an error" >&2')).rejects.toThrow();
21
+ });
22
+ it('should handle commands with arguments', async () => {
23
+ const result = await stdout('echo "Argument test"');
24
+ expect(result).toBe('Argument test');
25
+ });
26
+ it('should handle commands with multiple lines', async () => {
27
+ const result = await stdout('echo "Line 1\nLine 2"');
28
+ expect(result).toBe('Line 1\nLine 2');
29
+ });
30
+ it('should handle commands with special characters', async () => {
31
+ const result = await stdout('echo "Special characters: !@#$%^&*()"');
32
+ expect(result).toBe('Special characters: !@#$%^&*()');
33
+ });
34
+ it('should handle commands with spaces', async () => {
35
+ const result = await stdout('echo "Command with spaces"');
36
+ expect(result).toBe('Command with spaces');
37
+ });
38
+ it('should handle commands with quotes', async () => {
39
+ const result = await stdout('echo "Quoted text"');
40
+ expect(result).toBe('Quoted text');
41
+ });
42
+ it('should handle commands with multiple arguments', async () => {
43
+ const result = await stdout('echo "Multiple" "Arguments"');
44
+ expect(result).toBe('Multiple Arguments');
45
+ });
46
+ it('should handle commands with environment variables', async () => {
47
+ const result = await stdout('echo $HOME');
48
+ expect(result).toBe(process.env.HOME || '');
49
+ });
50
+ it('should handle commands with pipes', async () => {
51
+ const result = await stdout('echo "Piped output" | sed "s/Piped/Modified/"');
52
+ expect(result).toBe('Modified output');
53
+ });
54
+ it('should handle commands with redirection', async () => {
55
+ const result = await stdout('echo "Redirected output" > temp.txt && cat temp.txt');
56
+ expect(result).toBe('Redirected output');
57
+ // Clean up
58
+ execSync('rm temp.txt', { stdio: 'ignore' });
59
+ });
60
+ it('should handle commands with subshells', async () => {
61
+ const result = await stdout('echo $(echo "Subshell output")');
62
+ expect(result).toBe('Subshell output');
63
+ });
64
+ it('should handle commands with background processes', async () => {
65
+ const result = await stdout('echo "Background process" & wait');
66
+ expect(result).toBe('Background process');
67
+ });
68
+ });
69
+ //# sourceMappingURL=index.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,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,CAAC,OAAO,EAAE,CAAA;IACjE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,gCAAgC,EAAE,KAAK,IAAI,EAAE;QAC5C,MAAM,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CAAA;IAC9C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,gCAAgC,EAAE,KAAK,IAAI,EAAE;QAC5C,MAAM,MAAM,CAAC,MAAM,CAAC,qCA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,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "simple-stdout",
3
+ "version": "1.0.0",
4
+ "scripts": {
5
+ "build": "tsc",
6
+ "test": "vitest .",
7
+ "test:once": "vitest run",
8
+ "format": "prettier --write --list-different .",
9
+ "check-format": "prettier --check .",
10
+ "lint": "tsc --noEmit && eslint . --fix && pnpm run format",
11
+ "check-exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
12
+ "ci": "pnpm build && pnpm check-format && pnpm check-exports",
13
+ "prepublishOnly": "pnpm run ci",
14
+ "prepare": "husky"
15
+ },
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "devDependencies": {
22
+ "@arethetypeswrong/cli": "^0.18.2",
23
+ "@eslint/js": "^9.28.0",
24
+ "@types/node": "^22.15.30",
25
+ "eslint": "^9.28.0",
26
+ "eslint-config-prettier": "^10.1.5",
27
+ "eslint-plugin-prettier": "^5.4.1",
28
+ "husky": "^9.1.7",
29
+ "prettier": "3.5.3",
30
+ "tsx": "^4.19.4",
31
+ "typescript": "^5.8.3",
32
+ "typescript-eslint": "^8.33.1",
33
+ "vitest": "^3.2.3"
34
+ },
35
+ "packageManager": "pnpm@10.12.1",
36
+ "keywords": [],
37
+ "description": "Get any shell command's stdout as a string",
38
+ "author": {
39
+ "name": "Craig Patik",
40
+ "email": "craig@patik.com",
41
+ "url": "https://patik.com"
42
+ },
43
+ "homepage": "https://github.com/patik/simple-stdout",
44
+ "repository": {
45
+ "type": "git",
46
+ "url": "https://github.com/patik/simple-stdout.git"
47
+ },
48
+ "license": "MIT",
49
+ "publishConfig": {
50
+ "access": "public"
51
+ }
52
+ }