Skip to content

Commit

Permalink
dependencies; removed new API as it is at a dead end
Browse files Browse the repository at this point in the history
  • Loading branch information
j50n committed May 30, 2023
1 parent f91fba7 commit 87e5d09
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 22 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ Blue sky. Let's make something wonderful!

## A Warning

**Do not convert to the new API yet.** There are some major changes coming.
**The new API is in work and not yet ready (not even really close).** Big (good)
changes coming. Stay tuned.

## Documentation (Legacy API)

The documentation for the legacy API is available at
[Legacy Documentation](./legacy/README.md). It is recommended that you continue
to use the legacy API for the time being.

Note that only the legacy API is available from
`https://deno.land/x/proc/mod.ts`.

## Documentation (New API)

The documentation is available at
Expand Down
13 changes: 13 additions & 0 deletions build-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

HERE="$(realpath "$(dirname "$0")")"

deno install -rf --allow-read="$HERE/" --allow-write="$HERE/" --allow-net https://deno.land/x/udd/main.ts

cd "$HERE/legacy/" && (
./build.ts
)

cd "$HERE/" && (
./build.sh
)
2 changes: 1 addition & 1 deletion docs/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function playground_text(playground, hidden = true) {
// even if highlighting doesn't apply
code_nodes.forEach(function (block) { block.classList.add('hljs'); });

Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) {
Array.from(document.querySelectorAll("code.hljs")).forEach(function (block) {

var lines = Array.from(block.querySelectorAll('.boring'));
// If no lines were hidden, return
Expand Down
4 changes: 2 additions & 2 deletions legacy/deps-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "https://deno.land/std@0.177.1/testing/asserts.ts";
export * from "https://deno.land/std@0.190.0/testing/asserts.ts";
export * from "https://deno.land/x/[email protected]/mod.ts";
export * from "https://deno.land/std@0.177.1/fmt/colors.ts";
export * from "https://deno.land/std@0.190.0/fmt/colors.ts";
4 changes: 2 additions & 2 deletions legacy/deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "https://deno.land/std@0.177.1/async/mod.ts";
export * from "https://deno.land/std@0.177.1/io/mod.ts";
export * from "https://deno.land/std@0.190.0/async/mod.ts";
export * from "https://deno.land/std@0.190.0/io/mod.ts";
5 changes: 3 additions & 2 deletions legacy/examples/pushiterable/example-of-pushiterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import { Answer, Question } from "./common-json-defs.ts";
import * as proc from "../../mod.ts";
import { PushIterable } from "../../../mod1.ts";
import { asynciter } from "https://deno.land/x/[email protected]/mod.ts";
import { blue, red } from "https://deno.land/std@0.177.1/fmt/colors.ts";
import { blue, red } from "https://deno.land/std@0.190.0/fmt/colors.ts";

/**
* This demonstrates sending objects to and receiving objects from a child process
Expand All @@ -12,7 +13,7 @@ import { blue, red } from "https://deno.land/[email protected]/fmt/colors.ts";
* This shows how you can use `proc` to implement a worker using child processes.
*/

const it = new proc.PushIterable<Question>();
const it = new PushIterable<Question>();

/*
* Write "questions" to the push-iterable.
Expand Down
3 changes: 2 additions & 1 deletion legacy/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export * from "./runners/closers.ts";
export * from "./runners/process-exit-error.ts";
export * from "./runners/chained-error.ts";
export * from "./runners/stderr-support.ts";
export * from "./runners/push-iterable.ts";
export * from "../src/push-iterable.ts";

export {
bytesToTextLines,
dirname,
Expand Down
2 changes: 1 addition & 1 deletion legacy/runners/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isWindows } from "https://deno.land/std@0.177.1/_util/os.ts";
import { isWindows } from "https://deno.land/std@0.190.0/_util/os.ts";

export const LINESEP: string = (() => {
if (isWindows) {
Expand Down
2 changes: 1 addition & 1 deletion legacy/runners/handlers/bytes.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/std@0.177.1/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.190.0/testing/asserts.ts";
import * as proc from "../../mod.ts";

Deno.test({
Expand Down
2 changes: 1 addition & 1 deletion legacy/runners/utility.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BufReader, BufWriter } from "../deps.ts";
import * as path from "https://deno.land/std@0.177.1/path/mod.ts";
import * as path from "https://deno.land/std@0.190.0/path/mod.ts";

export const DEFAULT_BUFFER_SIZE = 4096;

Expand Down
2 changes: 1 addition & 1 deletion legacy/tests/line-split.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/std@0.177.1/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.190.0/testing/asserts.ts";
import * as proc from "../mod.ts";

Deno.test({
Expand Down
2 changes: 1 addition & 1 deletion legacy/tests/piped.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/std@0.177.1/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.190.0/testing/asserts.ts";
import * as proc from "../mod.ts";

Deno.test({
Expand Down
4 changes: 2 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./src/proc-readable-stream.ts";
export * from "./legacy/mod.ts";
/* Stable (legacy) API only for now. */
export * from "./mod1.ts";
1 change: 1 addition & 0 deletions mod1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./legacy/mod.ts";
2 changes: 2 additions & 0 deletions mod2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./src/proc-readable-stream.ts";
export * from "./src/push-iterable.ts";
122 changes: 122 additions & 0 deletions src/proc-iterable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// deno-lint-ignore-file no-inner-declarations
import { readableStreamFromIterable } from "./deps/streams.ts";
import { PushIterable } from "./push-iterable.ts";

export type PipeKinds = "piped" | "inherit" | "null";

export async function pipeTo<T>(src: AsyncIterable<T>, dest: PushIterable<T>) {
try {
for await (const item of src) {
dest.write(item);
}
} finally {
dest.close();
}
}

export async function* pipeThrough<T, U>(
src: AsyncIterable<T>,
transform: (item: T) => U | Promise<U>,
): AsyncIterableIterator<U> {
for await (const item of src) {
yield transform(item);
}
}

export class Shell implements Deno.Closer {
constructor() {}

async close(): Promise<void> {
}
}

/** Command options. */
export interface ProcIterOptions {
/** Current working directory. */
readonly cwd?: string;
/** Environment variables. */
readonly env?: Record<string, string>;

stdin: PipeKinds;
stdout: PipeKinds;
stderr: PipeKinds;
}

export class ProcIterProcess implements Deno.Closer {
constructor(
public shell: Shell,
protected readonly process: Deno.ChildProcess,
) {}

private _stderr: AsyncIterableIterator<Uint8Array> | undefined;
private _stdout: AsyncIterableIterator<Uint8Array> | undefined;
private _stdin: PushIterable<Uint8Array> | undefined;

async close(): Promise<void> {
if (this._stdin != null) {
await this.stdin.close();
}
}

get pid() {
return this.process.pid;
}

async status() {
return await this.process.status;
}

get stderr(): AsyncIterableIterator<Uint8Array> {
if (this._stderr == null) {
const process = this.process;
async function* gen() {
yield* process.stderr;
}
this._stderr = gen();
}
return this._stderr;
}

get stdout(): AsyncIterableIterator<Uint8Array> {
if (this._stdout == null) {
const process = this.process;
async function* gen() {
yield* process.stdout;
}
this._stdout = gen();
}
return this._stdout;
}

get stdin(): PushIterable<Uint8Array> {
if (this._stdin == null) {
const pi = new PushIterable<Uint8Array>({
onclose: () => this.stdin.close(),
});
readableStreamFromIterable(pi).pipeTo(this.process.stdin);
this._stdin = pi;
}
return this._stdin;
}
}

export class ProcIter {
readonly args: readonly string[];

constructor(
public shell: Shell,
public readonly options: ProcIterOptions,
public readonly cmd: string | URL,
...args: string[]
) {
this.args = [...args];
}

spawn() {
return new ProcIterProcess(
this.shell,
new Deno.Command(this.cmd, { ...this.options, args: [...this.args] })
.spawn(),
);
}
}
7 changes: 5 additions & 2 deletions legacy/runners/push-iterable.ts → src/push-iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class PushIterable<T> implements Deno.Closer {
/**
* Create a new `PushIterable`.
*/
constructor() {
constructor(protected options?: { onclose?: () => void | Promise<void> }) {
this.addEmptyPromiseToQueue();
}

Expand All @@ -43,9 +43,12 @@ export class PushIterable<T> implements Deno.Closer {
*
* It is safe to call `close()` multiple times.
*/
close(): void {
async close(): Promise<void> {
this.closed = true;
this.queue[this.queue.length - 1].resolve(new None());
if (this.options?.onclose != null) {
await this.options.onclose();
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/docs/simple-examples.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execute, lines, run } from "../../mod.ts";
import { execute, lines, run } from "../../mod2.ts";
import { TextLineStream } from "../deps/streams.ts";

Deno.test({
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/count-words.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fromFileUrl } from "../deps/path.ts";
import { assertEquals } from "../deps/asserts.ts";
import { bytes, lines, ProcReadableStream, text } from "../../mod.ts";
import { bytes, lines, ProcReadableStream, text } from "../../mod2.ts";

class ToLowerCaseStream extends TransformStream<string, string> {
constructor(options?: { lineOrientedText?: boolean }) {
Expand Down
2 changes: 1 addition & 1 deletion tests/exitcode-errors/errors-1.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExitCodeError, lines, run } from "../../mod.ts";
import { ExitCodeError, lines, run } from "../../mod2.ts";
import { assertEquals, assertRejects } from "../deps/asserts.ts";

Deno.test({
Expand Down
2 changes: 1 addition & 1 deletion tests/proc-readable-stream.ts/toText.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lines, ProcReadableStream } from "../../mod.ts";
import { lines, ProcReadableStream } from "../../mod2.ts";
import { readableStreamFromIterable } from "../deps/streams.ts";
import { assertEquals, assertRejects } from "../deps/asserts.ts";

Expand Down

0 comments on commit 87e5d09

Please sign in to comment.