@journeyapps/cloudcode-build-agent 0.0.0-dev.8ebefb0 → 0.0.0-dev.90a9f01

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/src/run.ts DELETED
@@ -1,23 +0,0 @@
1
- import { spawnStream, SpawnStreamEvent } from './spawn-stream';
2
- import { SpawnOptions } from 'child_process';
3
-
4
- export async function* runCommand(
5
- command: string,
6
- args: string[],
7
- options?: Partial<SpawnOptions>
8
- ): AsyncIterable<SpawnStreamEvent> {
9
- const { childProcess, stream } = spawnStream(command, args, { cwd: options?.cwd, splitLines: true, ...options });
10
-
11
- let exitCode: number | null = null;
12
-
13
- for await (let event of stream) {
14
- yield event;
15
- if (event.exitCode != null) {
16
- exitCode = event.exitCode;
17
- }
18
- }
19
-
20
- if (exitCode != 0) {
21
- throw new Error(`Command failed with code ${exitCode}`);
22
- }
23
- }
@@ -1,73 +0,0 @@
1
- import { ChildProcess, spawn, SpawnOptions } from 'child_process';
2
- import { PassThrough, Readable } from 'stream';
3
-
4
- export interface StreamOptions {
5
- splitLines?: boolean;
6
- }
7
-
8
- export function spawnStream(command: string, args: string[], options: SpawnOptions & StreamOptions) {
9
- const process = spawn(command, args, {
10
- ...options,
11
- stdio: 'pipe'
12
- });
13
-
14
- return {
15
- childProcess: process,
16
- stream: processToStream(process, options)
17
- };
18
- }
19
-
20
- export interface SpawnStreamEvent {
21
- event: 'stdout' | 'stderr' | 'exit';
22
- stdout?: string;
23
- stderr?: string;
24
- exitCode?: number;
25
- }
26
-
27
- function processToStream(process: ChildProcess, options: StreamOptions): AsyncIterable<SpawnStreamEvent> {
28
- const combinedStream = new PassThrough({ objectMode: true });
29
-
30
- async function* transform(stream: Readable, key: 'stdout' | 'stderr'): AsyncIterable<SpawnStreamEvent> {
31
- stream.setEncoding('utf-8');
32
- let iterable: AsyncIterable<string> = stream;
33
- if (options.splitLines) {
34
- iterable = splitLines(iterable);
35
- }
36
- for await (const chunk of iterable) {
37
- yield {
38
- event: key,
39
- [key]: chunk
40
- };
41
- }
42
- }
43
-
44
- if (process.stdout) {
45
- Readable.from(transform(process.stdout, 'stdout')).pipe(combinedStream, { end: false });
46
- }
47
- if (process.stderr) {
48
- Readable.from(transform(process.stderr, 'stderr')).pipe(combinedStream, { end: false });
49
- }
50
-
51
- process.on('exit', (code: number | null) => {
52
- combinedStream.write({ exitCode: code }, () => {
53
- combinedStream.end();
54
- });
55
- });
56
-
57
- return combinedStream;
58
- }
59
-
60
- async function* splitLines(stream: AsyncIterable<string>): AsyncIterable<string> {
61
- let buffer = '';
62
- for await (const chunk of stream) {
63
- buffer += chunk;
64
- const lines = buffer.split('\n');
65
- buffer = lines.pop()!;
66
- for (let line of lines) {
67
- yield `${line}\n`;
68
- }
69
- }
70
- if (buffer) {
71
- yield buffer;
72
- }
73
- }