From ab3320ede793e232ef96afe072eb7bb56d863c09 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Fri, 20 Sep 2024 16:59:29 +0300 Subject: [PATCH] feat: export spawn ctx defaults --- src/main/ts/index.ts | 2 +- src/main/ts/spawn.ts | 27 ++++++++++++++++----------- src/main/ts/util.ts | 2 ++ src/test/ts/index.test.ts | 3 ++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/ts/index.ts b/src/main/ts/index.ts index 92ba0b1..d7f9ac6 100644 --- a/src/main/ts/index.ts +++ b/src/main/ts/index.ts @@ -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' diff --git a/src/main/ts/spawn.ts b/src/main/ts/spawn.ts index ed1a545..eafdca6 100644 --- a/src/main/ts/spawn.ts +++ b/src/main/ts/spawn.ts @@ -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' @@ -79,16 +79,16 @@ 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', @@ -96,14 +96,19 @@ export const normalizeCtx = (...ctxs: TSpawnCtx[]): TSpawnCtxNormalized => assig 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) { diff --git a/src/main/ts/util.ts b/src/main/ts/util.ts index b242883..20769e6 100644 --- a/src/main/ts/util.ts +++ b/src/main/ts/util.ts @@ -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 = (value: T | PromiseLike) => void export type TVoidCallback = (...any: any) => void diff --git a/src/test/ts/index.test.ts b/src/test/ts/index.test.ts index 54541f4..deda28a 100644 --- a/src/test/ts/index.test.ts +++ b/src/test/ts/index.test.ts @@ -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')