@rcompat/io 0.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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) Terrablue <terrablue@proton.me> and contributors.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,287 @@
1
+ # @rcompat/io
2
+
3
+ Standard input/output utilities for JavaScript runtimes.
4
+
5
+ ## What is @rcompat/io?
6
+
7
+ A cross-runtime module providing access to standard streams (stdin, stdout,
8
+ stderr) and process utilities like executing commands, spawning processes,
9
+ and reading user input. Works consistently across Node, Deno, and Bun.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install @rcompat/io
15
+ ```
16
+
17
+ ```bash
18
+ pnpm add @rcompat/io
19
+ ```
20
+
21
+ ```bash
22
+ yarn add @rcompat/io
23
+ ```
24
+
25
+ ```bash
26
+ bun add @rcompat/io
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Standard streams
32
+
33
+ ```js
34
+ import io from "@rcompat/io";
35
+
36
+ // write to stdout (no newline)
37
+ io.stdout.write("Hello, ");
38
+ io.stdout.write("World!\n");
39
+
40
+ // write to stderr
41
+ io.stderr.write("Error: something went wrong\n");
42
+ ```
43
+
44
+ ### Check if running in a terminal
45
+
46
+ ```js
47
+ import io from "@rcompat/io";
48
+
49
+ if (io.isatty()) {
50
+ // interactive terminal - show colors, prompts, etc.
51
+ console.log("\x1b[32mGreen text!\x1b[0m");
52
+ } else {
53
+ // piped or redirected - plain output
54
+ console.log("Plain text");
55
+ }
56
+ ```
57
+
58
+ ### Run a command
59
+
60
+ ```js
61
+ import io from "@rcompat/io";
62
+
63
+ // get command output as string
64
+ const output = await io.run("echo hello");
65
+ console.log(output); // "hello\n"
66
+
67
+ // with options
68
+ const files = await io.run("ls -la", { cwd: "/tmp" });
69
+ ```
70
+
71
+ ### Spawn a process
72
+
73
+ ```js
74
+ import io from "@rcompat/io";
75
+
76
+ const { stdin, stdout, stderr } = io.spawn("cat", { cwd: "." });
77
+
78
+ // write to process stdin (Web WritableStream)
79
+ const writer = stdin.getWriter();
80
+ await writer.write(new TextEncoder().encode("Hello from stdin!"));
81
+ await writer.close();
82
+
83
+ // read from process stdout (Web ReadableStream)
84
+ const reader = stdout.getReader();
85
+ const { value } = await reader.read();
86
+ console.log(new TextDecoder().decode(value)); // "Hello from stdin!"
87
+ ```
88
+
89
+ ### Find an executable
90
+
91
+ ```js
92
+ import io from "@rcompat/io";
93
+
94
+ const node = await io.which("node");
95
+ console.log(node); // "/usr/local/bin/node"
96
+
97
+ const bun = await io.which("bun");
98
+ console.log(bun); // "/home/user/.bun/bin/bun"
99
+ ```
100
+
101
+ ## API Reference
102
+
103
+ ### `stdin`
104
+
105
+ ```ts
106
+ import io from "@rcompat/io";
107
+ io.stdin;
108
+ ```
109
+
110
+ The standard input stream. A `ReadStream` for reading user input.
111
+
112
+ ### `stdout`
113
+
114
+ ```ts
115
+ import io from "@rcompat/io";
116
+
117
+ io.stdout.write(data: string | Uint8Array): boolean;
118
+ ```
119
+
120
+ The standard output stream. Use `write()` to output without a newline.
121
+
122
+ ### `stderr`
123
+
124
+ ```ts
125
+ import io from "@rcompat/io";
126
+
127
+ io.stderr.write(data: string | Uint8Array): boolean;
128
+ ```
129
+
130
+ The standard error stream. Use for error messages and diagnostics.
131
+
132
+ ### `isatty`
133
+
134
+ ```ts
135
+ import io from "@rcompat/io";
136
+
137
+ io.isatty(): boolean;
138
+ ```
139
+
140
+ Returns `true` if stdout is connected to a terminal (TTY).
141
+
142
+ ### `run`
143
+
144
+ ```ts
145
+ import io from "@rcompat/io";
146
+
147
+ io.run(command: string, options?: ExecOptions): Promise<string>;
148
+ ```
149
+
150
+ Runs a shell command and returns its stdout as a string. Rejects on error.
151
+
152
+ | Parameter | Type | Description |
153
+ | --------- | ------------- | -------------------------- |
154
+ | `command` | `string` | Shell command to execute |
155
+ | `options` | `ExecOptions` | Optional execution options |
156
+
157
+ Options include `cwd`, `env`, `timeout`, `maxBuffer`, etc.
158
+
159
+ ### `spawn`
160
+
161
+ ```ts
162
+ import io from "@rcompat/io";
163
+
164
+ io.spawn(command: string, options: SpawnOptions): {
165
+ stdin: WritableStream<Uint8Array>;
166
+ stdout: ReadableStream<Uint8Array>;
167
+ stderr: ReadableStream<Uint8Array>;
168
+ };
169
+ ```
170
+
171
+ Spawns a process and returns Web Streams for I/O.
172
+
173
+ | Parameter | Type | Description |
174
+ | --------- | -------------- | ------------------------- |
175
+ | `command` | `string` | Command to spawn |
176
+ | `options` | `SpawnOptions` | Spawn options (e.g., cwd) |
177
+
178
+ ### `which`
179
+
180
+ ```ts
181
+ import io from "@rcompat/io";
182
+
183
+ io.which(command: string): Promise<string>;
184
+ ```
185
+
186
+ Finds the full path to an executable. Cross-platform (uses `which` on Unix,
187
+ `where` on Windows).
188
+
189
+ ## Examples
190
+
191
+ ### Run a command and process output
192
+
193
+ ```js
194
+ import io from "@rcompat/io";
195
+
196
+ async function getBranch() {
197
+ try {
198
+ const branch = await io.run("git branch --show-current");
199
+ return branch.trim();
200
+ } catch {
201
+ return null;
202
+ }
203
+ }
204
+
205
+ const branch = await getBranch();
206
+ console.log(`Current branch: ${branch ?? "not a git repo"}`);
207
+ ```
208
+
209
+ ### Stream processing with spawn
210
+
211
+ ```js
212
+ import io from "@rcompat/io";
213
+
214
+ const { stdout } = io.spawn("ping -c 3 localhost", { cwd: "." });
215
+
216
+ const reader = stdout.getReader();
217
+ const decoder = new TextDecoder();
218
+
219
+ while (true) {
220
+ const { done, value } = await reader.read();
221
+ if (done) break;
222
+ process.stdout.write(decoder.decode(value));
223
+ }
224
+ ```
225
+
226
+ ### Check for required tools
227
+
228
+ ```js
229
+ import io from "@rcompat/io";
230
+
231
+ async function checkDependencies(tools) {
232
+ const missing = [];
233
+
234
+ for (const tool of tools) {
235
+ try {
236
+ await io.which(tool);
237
+ } catch {
238
+ missing.push(tool);
239
+ }
240
+ }
241
+
242
+ return missing;
243
+ }
244
+
245
+ const missing = await checkDependencies(["git", "node", "docker"]);
246
+
247
+ if (missing.length > 0) {
248
+ console.error(`Missing tools: ${missing.join(", ")}`);
249
+ process.exit(1);
250
+ }
251
+ ```
252
+
253
+ ### Conditional formatting
254
+
255
+ ```js
256
+ import io from "@rcompat/io";
257
+
258
+ function log(message, color = 32) {
259
+ if (io.isatty()) {
260
+ io.stdout.write(`\x1b[${color}m${message}\x1b[0m\n`);
261
+ } else {
262
+ io.stdout.write(`${message}\n`);
263
+ }
264
+ }
265
+
266
+ log("Success!", 32); // green in terminal, plain when piped
267
+ log("Warning!", 33); // yellow in terminal
268
+ log("Error!", 31); // red in terminal
269
+ ```
270
+
271
+ ## Cross-Runtime Compatibility
272
+
273
+ | Runtime | Supported |
274
+ | ------- | --------- |
275
+ | Node.js | ✓ |
276
+ | Deno | ✓ |
277
+ | Bun | ✓ |
278
+
279
+ No configuration required — just import and use.
280
+
281
+ ## License
282
+
283
+ MIT
284
+
285
+ ## Contributing
286
+
287
+ See [CONTRIBUTING.md](../../CONTRIBUTING.md) in the repository root.
@@ -0,0 +1,3 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ export default AsyncLocalStorage;
3
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1,3 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ export default AsyncLocalStorage;
3
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1,6 @@
1
+ declare namespace _default {
2
+ export { context };
3
+ }
4
+ export default _default;
5
+ import context from "#async/context";
6
+ //# sourceMappingURL=async.d.ts.map
@@ -0,0 +1,5 @@
1
+ import context from "#async/context";
2
+ export default {
3
+ context,
4
+ };
5
+ //# sourceMappingURL=async.js.map
@@ -0,0 +1,24 @@
1
+ declare const _default: {
2
+ async: {
3
+ context: typeof import("node:async_hooks").AsyncLocalStorage;
4
+ };
5
+ isatty: () => boolean;
6
+ run: (command: string, options?: import("node:child_process").ExecOptions) => Promise<string>;
7
+ spawn: (command: string, options: import("node:child_process").SpawnOptions) => {
8
+ stderr: import("node:stream/web").ReadableStream<any>;
9
+ stdin: import("node:stream/web").WritableStream<any>;
10
+ stdout: import("node:stream/web").ReadableStream<any>;
11
+ };
12
+ stderr: NodeJS.WriteStream & {
13
+ fd: 2;
14
+ };
15
+ stdin: NodeJS.ReadStream & {
16
+ fd: 0;
17
+ };
18
+ stdout: NodeJS.WriteStream & {
19
+ fd: 1;
20
+ };
21
+ which: (command: string) => Promise<string>;
22
+ };
23
+ export default _default;
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,19 @@
1
+ import async from "#async";
2
+ import isatty from "#isatty";
3
+ import run from "#run";
4
+ import spawn from "#spawn";
5
+ import stderr from "#std/err";
6
+ import stdin from "#std/in";
7
+ import stdout from "#std/out";
8
+ import which from "#which";
9
+ export default {
10
+ async,
11
+ isatty,
12
+ run,
13
+ spawn,
14
+ stderr,
15
+ stdin,
16
+ stdout,
17
+ which,
18
+ };
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ declare const _default: () => boolean;
2
+ export default _default;
3
+ //# sourceMappingURL=isatty.d.ts.map
@@ -0,0 +1,2 @@
1
+ export default () => Boolean(process.stdout.isTTY);
2
+ //# sourceMappingURL=isatty.js.map
@@ -0,0 +1,4 @@
1
+ import { type ExecOptions } from "node:child_process";
2
+ declare const _default: (command: string, options?: ExecOptions) => Promise<string>;
3
+ export default _default;
4
+ //# sourceMappingURL=run.d.ts.map
@@ -0,0 +1,5 @@
1
+ import { exec } from "node:child_process";
2
+ export default (command, options) => new Promise((resolve, reject) => {
3
+ exec(command, options ?? {}, (error, stdout, stderr) => error === null ? resolve(stdout) : reject(stderr));
4
+ });
5
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1,8 @@
1
+ import { type SpawnOptions } from "node:child_process";
2
+ declare const _default: (command: string, options: SpawnOptions) => {
3
+ stderr: import("node:stream/web").ReadableStream<any>;
4
+ stdin: import("node:stream/web").WritableStream<any>;
5
+ stdout: import("node:stream/web").ReadableStream<any>;
6
+ };
7
+ export default _default;
8
+ //# sourceMappingURL=spawn.d.ts.map
@@ -0,0 +1,15 @@
1
+ import { spawn } from "node:child_process";
2
+ import { Readable, Writable } from "node:stream";
3
+ export default (command, options) => {
4
+ const { stderr, stdin, stdout } = spawn(command, {
5
+ ...options,
6
+ shell: true,
7
+ stdio: ["pipe", "pipe", "pipe"],
8
+ });
9
+ return {
10
+ stderr: Readable.toWeb(stderr),
11
+ stdin: Writable.toWeb(stdin),
12
+ stdout: Readable.toWeb(stdout),
13
+ };
14
+ };
15
+ //# sourceMappingURL=spawn.js.map
@@ -0,0 +1,2 @@
1
+ export { stderr as default } from "node:process";
2
+ //# sourceMappingURL=err.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { stderr as default } from "node:process";
2
+ //# sourceMappingURL=err.js.map
@@ -0,0 +1,2 @@
1
+ export { stdin as default } from "node:process";
2
+ //# sourceMappingURL=in.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { stdin as default } from "node:process";
2
+ //# sourceMappingURL=in.js.map
@@ -0,0 +1,2 @@
1
+ export { stdout as default } from "node:process";
2
+ //# sourceMappingURL=out.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { stdout as default } from "node:process";
2
+ //# sourceMappingURL=out.js.map
@@ -0,0 +1,3 @@
1
+ declare const _default: (command: string) => Promise<string>;
2
+ export default _default;
3
+ //# sourceMappingURL=which.d.ts.map
@@ -0,0 +1,6 @@
1
+ import run from "#run";
2
+ const is_win = process.platform === "win32";
3
+ const which = is_win ? "where" : "which";
4
+ const qualify = (path) => is_win ? `"${path}"` : path;
5
+ export default async (command) => qualify(await run(`${which} ${command}`, {})).replaceAll("\n", "");
6
+ //# sourceMappingURL=which.js.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#index";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#index";
2
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@rcompat/io",
3
+ "version": "0.1.0",
4
+ "description": "Standard library input/output",
5
+ "bugs": "https://github.com/rcompat/rcompat/issues",
6
+ "license": "MIT",
7
+ "files": [
8
+ "/lib/**/*.js",
9
+ "/lib/**/*.d.ts",
10
+ "!/**/*.spec.*"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/rcompat/rcompat",
15
+ "directory": "packages/io"
16
+ },
17
+ "type": "module",
18
+ "imports": {
19
+ "#*": {
20
+ "apekit": "./src/private/*.ts",
21
+ "default": "./lib/private/*.js"
22
+ }
23
+ },
24
+ "exports": {
25
+ ".": {
26
+ "apekit": "./src/public/index.ts",
27
+ "default": "./lib/public/index.js"
28
+ }
29
+ },
30
+ "scripts": {
31
+ "build": "npm run clean && tsc",
32
+ "test": "npm run build && npx proby",
33
+ "clean": "rm -rf ./lib"
34
+ }
35
+ }