Skip to content

Commit

Permalink
Fix formatting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-wilkins committed Sep 7, 2024
1 parent 1a75f64 commit 5c10e48
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ bunx jsr add @cross/utils
- **stripAnsi(text: string): string**
- Removes all ANSI control codes from a string for cleaner logs and output.

- **spawn(command: CommandArray, extraEnvVars?: Object, cwd?: string, stdio: StdIO)**
- **spawn(command: CommandArray, extraEnvVars?: Object, cwd?: string, stdio:
StdIO)**
- Spawns a sub process.

- **table(data: string[][])**
Expand Down Expand Up @@ -136,8 +137,8 @@ functionality:
- **Colors Class** - Provides methods for easy console text styling.

- **@cross/utils/spawn**
- **spawn(command: CommandArray, extraEnvVars?: Object, cwd?: string, stdio: StdIO):
Promise<>** - Spawns subprocesses in a cross-runtime manner.
- **spawn(command: CommandArray, extraEnvVars?: Object, cwd?: string, stdio:
StdIO): Promise<>** - Spawns subprocesses in a cross-runtime manner.

- **@cross/utils/args**
- **args(all?: boolean): string[]** - Fetches command-line arguments
Expand Down Expand Up @@ -221,11 +222,11 @@ functionality:
sealedCopy.w = 7; // Throws an error in strict mode
original.w = 20; // Succeeds, original is unchanged
```

- **@cross/utils/stdio**
- **stdin(): ReadableStream**
- Get the stdin as a web-standard `ReadableStream` object.
- **stdout(): WritableStream**
- Get the stdout as a web-standard `WritableStream` object.
- **stderr(): WritableStream**
- Get the stderr as a web-standard `WritableStream` object.
- Get the stderr as a web-standard `WritableStream` object.
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export {
uptime,
} from "./utils/sysinfo.ts";
export { deepFreeze, deepSeal } from "./utils/objectManip.ts";
export { stdin, stdout, stderr } from "./utils/stdio.ts";
export { stderr, stdin, stdout } from "./utils/stdio.ts";
39 changes: 26 additions & 13 deletions utils/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (CurrentRuntime === Runtime.Bun) {
writer.write(chunk);
},
});
}
};
}

/**
Expand Down Expand Up @@ -98,18 +98,22 @@ export function spawn(
stdio = {
stdin: null,
stdout: new WritableStream({
write(chunk) {stdoutBuffer += chunk.toString()},
write(chunk) {
stdoutBuffer += chunk.toString();
},
}),
stderr: new WritableStream({
write(chunk) {stderrBuffer += chunk.toString()},
write(chunk) {
stderrBuffer += chunk.toString();
},
}),
}
};
}

const stdio_node: ("pipe" | "inherit" | "ignore")[] = [
stdio.stdin === "inherit" ? "inherit" : stdio.stdin ? "pipe" : "ignore",
stdio.stdout === "inherit" ? "inherit" : stdio.stdout ? "pipe" : "ignore",
stdio.stderr === "inherit" ? "inherit" : stdio.stderr ? "pipe" : "ignore"
stdio.stderr === "inherit" ? "inherit" : stdio.stderr ? "pipe" : "ignore",
];

const childProcess = spawnChild(
Expand All @@ -123,18 +127,27 @@ export function spawn(
},
);

// @ts-ignore Node's types here are weird
if (stdio.stdin instanceof ReadableStream) Readable.fromWeb(stdio.stdin).pipe(childProcess.stdin, { end: false });
// @ts-ignore Node's types here are weird
if (stdio.stdout instanceof WritableStream) childProcess.stdout.pipe(Writable.fromWeb(stdio.stdout), { end: false });
// @ts-ignore Node's types here are weird
if (stdio.stderr instanceof WritableStream) childProcess.stderr.pipe(Writable.fromWeb(stdio.stderr), { end: false });
if (stdio.stdin instanceof ReadableStream) {
// @ts-ignore Node's types here are weird
Readable.fromWeb(stdio.stdin).pipe(childProcess.stdin, { end: false });
}

if (stdio.stdout instanceof WritableStream) {
// @ts-ignore Node's types here are weird
childProcess.stdout.pipe(Writable.fromWeb(stdio.stdout), { end: false });
}

if (stdio.stderr instanceof WritableStream) {
// @ts-ignore Node's types here are weird
childProcess.stderr.pipe(Writable.fromWeb(stdio.stderr), { end: false });
}

return new Promise((resolve, reject) => { // Still need Promise here due to event listeners
childProcess.on("error", (error: Error) => reject(error));
childProcess.on(
"close",
(code: number) => resolve({ code, stdout: stdoutBuffer, stderr: stderrBuffer }),
(code: number) =>
resolve({ code, stdout: stdoutBuffer, stderr: stderrBuffer }),
);
});
}
}
15 changes: 8 additions & 7 deletions utils/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ if (CurrentRuntime === Runtime.Bun) {
// Bun has a bug in Writable.toWeb, so polyfill
Writable.toWeb = (streamWritable) => {
return new WritableStream({
write(chunk) {
streamWritable.write(chunk)
},
write(chunk) {
streamWritable.write(chunk);
},
});
}
};
}

export const stdin = () => (Readable.toWeb(process.stdin) as ReadableStream);
export const stdout = () => Writable.toWeb(process.stdout);
export const stderr = () => Writable.toWeb(process.stderr);
// @ts-ignore Node has strange typings on Stream objects
export const stdin = (): ReadableStream => (Readable.toWeb(process.stdin));
export const stdout = (): WritableStream => Writable.toWeb(process.stdout);
export const stderr = (): WritableStream => Writable.toWeb(process.stderr);

0 comments on commit 5c10e48

Please sign in to comment.