Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export spawn ctx defaults #15

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type * from './spawn.js'
export type * from './x.js'
export type * from './zurk.js'

export { invoke, exec } from './spawn.js'
export { invoke, exec, defaults } from './spawn.js'
export { $ } from './x.js'
export { zurk } from './zurk.js'
export { type Promisified, buildCmd } from './util.js'
27 changes: 16 additions & 11 deletions src/main/ts/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cp from 'node:child_process'
import process from 'node:process'
import EventEmitter from 'node:events'
import { Readable, Writable, Stream, Transform } from 'node:stream'
import { assign, noop } from './util.js'
import { assign, noop, randomId } from './util.js'

export * from './util.js'

Expand Down Expand Up @@ -79,31 +79,36 @@ export interface TSpawnCtxNormalized {
run: (cb: () => void, ctx: TSpawnCtxNormalized) => void
}

export const normalizeCtx = (...ctxs: TSpawnCtx[]): TSpawnCtxNormalized => assign({
id: Math.random().toString(36).slice(2),
export const defaults: TSpawnCtxNormalized = {
get id() { return randomId() },
cmd: '',
cwd: process.cwd(),
get cwd() { return process.cwd() },
sync: false,
args: [],
input: null,
env: process.env,
ee: new EventEmitter(),
ac: global.AbortController && new AbortController(),
get ee() { return new EventEmitter() },
get ac() { return global.AbortController && new AbortController() },
get signal() { return this.ac?.signal },
on: {},
detached: process.platform !== 'win32',
shell: true,
spawn: cp.spawn,
spawnSync: cp.spawnSync,
spawnOpts: {},
store: createStore(),
get store() { return createStore() },
callback: noop,
stdin: new VoidWritable(),
stdout: new VoidWritable(),
stderr: new VoidWritable(),
get stdin() { return new VoidWritable() },
get stdout(){ return new VoidWritable() },
get stderr(){ return new VoidWritable() },
stdio: ['pipe', 'pipe', 'pipe'],
run: setImmediate,
}, ...ctxs)
}

export const normalizeCtx = (...ctxs: TSpawnCtx[]): TSpawnCtxNormalized => assign({
...defaults,
get signal() { return this.ac?.signal }},
...ctxs)

export const processInput = (child: TChild, input?: TInput | null) => {
if (input && child.stdin && !child.stdin.destroyed) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Stream } from 'node:stream'

export const noop = () => { /* noop */ }

export const randomId = () => Math.random().toString(36).slice(2)

export type PromiseResolve<T = any> = (value: T | PromiseLike<T>) => void

export type TVoidCallback = (...any: any) => void
Expand Down
3 changes: 2 additions & 1 deletion src/test/ts/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as assert from 'node:assert'
import { describe, it } from 'node:test'
import { invoke, zurk, $, buildCmd, exec } from '../../main/ts/index.js'
import { invoke, zurk, $, buildCmd, exec, defaults } from '../../main/ts/index.js'

describe('index', () => {
it('has proper exports', () => {
assert.equal(typeof defaults, 'object')
assert.equal(typeof $, 'function')
assert.equal(typeof zurk, 'function')
assert.equal(typeof exec, 'function')
Expand Down