Skip to content

Commit

Permalink
docs: add jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jan 12, 2025
1 parent 78fb292 commit 538b43a
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/ts/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @module zurk/error
* @module
*
* Zurk spawn error codes & handling utilities
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export { zurk } from './zurk.js'
export { type Promisified, buildCmd } from './util.js'

/**
* @module zurk
* @module
*
* A generic process spawner
*
Expand Down
6 changes: 6 additions & 0 deletions src/main/ts/mixin/kill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { assign } from '../util.js'
import type { TMixin, TShell, TShellCtx } from '../x.js'
import { type TZurk, type TZurkPromise, isZurkAny } from '../zurk.js'

/**
* @module
*
* Zurk $ pipe mixin
*/

// https://github.com/nodejs/node/issues/37518
// https://github.com/nodejs/node/issues/46865
const kill = (child?: ChildProcess, signal: null | string | number | NodeJS.Signals = 'SIGTERM') => new Promise<typeof signal>((resolve, reject) => {
Expand Down
6 changes: 6 additions & 0 deletions src/main/ts/mixin/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { VoidStream } from '../spawn.js'
import type { TShell, TMixin, TShellCtx } from '../x.js'
import { type TZurk, type TZurkPromise, isZurkAny } from '../zurk.js'

/**
* @module
*
* Zurk $ pipe mixin
*/

// eslint-disable-next-line sonarjs/cognitive-complexity
export const pipeMixin: TMixin = <T extends TZurk | TZurkPromise >($: TShell, result: T, ctx: TShellCtx) =>
isZurkAny(result)
Expand Down
6 changes: 6 additions & 0 deletions src/main/ts/mixin/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { assign, pFinally } from '../util.js'
import type { TMixin, TShell, TShellCtx } from '../x.js'
import { type TZurk, type TZurkPromise, isZurkPromise } from '../zurk.js'

/**
* @module
*
* Zurk $ timeout mixin
*/

const attachTimeout = <T extends TZurkPromise & { kill?: (signal: NodeJS.Signals) => void }>(
ctx: TShellCtx,
result: T
Expand Down
43 changes: 42 additions & 1 deletion src/main/ts/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Readable, Writable, Stream, Transform } from 'node:stream'
import { assign, noop, randomId, g, immediate } from './util.js'

/**
* @module zurk/spawn
* @module
*
* Zurk internal child_process caller API
*
Expand Down Expand Up @@ -105,6 +105,9 @@ export interface TSpawnCtxNormalized {
stack: string
}

/**
* zurk default settings
*/
export const defaults: TSpawnCtxNormalized = {
get id() { return randomId() },
cmd: '',
Expand Down Expand Up @@ -132,11 +135,22 @@ export const defaults: TSpawnCtxNormalized = {
stack: ''
}

/**
* Normalizes spawn context.
*
* @param ctxs Contexts to normalize
* @returns
*/
export const normalizeCtx = (...ctxs: TSpawnCtx[]): TSpawnCtxNormalized => assign({
...defaults,
get signal() { return this.ac?.signal }},
...ctxs)

/**
* Redirects input to child process stdin
* @param child
* @param input
*/
export const processInput = (child: TChild, input?: TInput | null): void => {
if (input && child.stdin && !child.stdin.destroyed) {
if (input instanceof Stream) {
Expand All @@ -148,13 +162,21 @@ export const processInput = (child: TChild, input?: TInput | null): void => {
}
}

/**
* Transformer that emits data but does not consume it.
*/
export class VoidStream extends Transform {
_transform(chunk: any, _: string, cb: (err?: Error) => void) {
this.emit('data', chunk)
cb()
}
}

/**
* Builds spawn options
* @param ctx
* @returns spawn options
*/
export const buildSpawnOpts = ({spawnOpts, stdio, cwd, shell, input, env, detached, signal}: TSpawnCtxNormalized) => ({
...spawnOpts,
env,
Expand All @@ -167,6 +189,12 @@ export const buildSpawnOpts = ({spawnOpts, stdio, cwd, shell, input, env, detach
signal
})

/**
* Toggles event listeners
* @param pos 'on' | 'off'
* @param ee EventEmitter
* @param on listeners map
*/
export const toggleListeners = (pos: 'on' | 'off', ee: EventEmitter, on: Partial<TSpawnListeners> = {}): void => {
for (const [name, listener] of Object.entries(on)) {
ee[pos](name, listener as any)
Expand All @@ -175,12 +203,20 @@ export const toggleListeners = (pos: 'on' | 'off', ee: EventEmitter, on: Partial
ee.once('end', () => toggleListeners('off', ee, on))
}

/**
* Creates a new spawn store
*/
export const createStore = (): TSpawnStore => ({
stdout: [],
stderr: [],
stdall: [],
})

/**
* Invokes a child process
* @param c Normalized context.
* @returns Normalized context.
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
export const invoke = (c: TSpawnCtxNormalized): TSpawnCtxNormalized => {
const now = Date.now()
Expand Down Expand Up @@ -296,6 +332,11 @@ export const invoke = (c: TSpawnCtxNormalized): TSpawnCtxNormalized => {
return c
}

/**
* Executes a child process
* @param ctx TSpawnCtx
* @returns TSpawnCtxNormalized
*/
export const exec = (ctx: TSpawnCtx): TSpawnCtxNormalized => invoke(normalizeCtx(ctx))

// https://2ality.com/2018/05/child-process-streams.html
2 changes: 1 addition & 1 deletion src/main/ts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import process from 'node:process'
import { Buffer } from 'node:buffer'

/**
* @module zurk/util
* @module
*
* Zurk utility functions
*
Expand Down
27 changes: 27 additions & 0 deletions src/main/ts/x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ import { pipeMixin } from './mixin/pipe.js'
import { killMixin } from './mixin/kill.js'
import { timeoutMixin } from './mixin/timeout.js'


/**
* @module
*
* Zurk $ API
*
* @example
* ```ts
* import {$} from 'zurk/x'
*
* const p = await $`echo foo`'
* ```
*/

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface TShellCtxExtra {
}
Expand Down Expand Up @@ -88,6 +102,12 @@ export interface TShellSync {
(opts: TShellOptions): TShellSync
}

/**
* Zurk $ template API
*
* @param pieces
* @param args
*/
export const $: TShell = function(this: any, pieces?: any, ...args: any): any {
const self = (this !== g) && this
const preset = self || {}
Expand Down Expand Up @@ -125,6 +145,13 @@ const zurkMixin: TMixin = ($: TShell, target: TShellOptions | TZurk | TZurkPromi

$.mixins = [zurkMixin, killMixin, pipeMixin, timeoutMixin]

/**
* Applies mixins to the result.
* @param $
* @param result
* @param parent
* @returns TZurk | TZurkPromise | TShellOptions
*/
export const applyMixins = ($: TShell, result: TZurk | TZurkPromise | TShellOptions, parent?: TZurk | TZurkPromise): TZurk | TZurkPromise | TShellOptions => {
let ctx: TShellCtx = (parent as TZurkPromise | TZurk)?.ctx

Expand Down
2 changes: 1 addition & 1 deletion src/main/ts/zurk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {


/**
* @module zurk/zurk
* @module
*
* Zurk process spawner
*
Expand Down

0 comments on commit 538b43a

Please sign in to comment.