From 700bb1e6fd9d76a40379ca2126a0f864737f8d11 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Mon, 13 Jun 2022 16:28:36 +0300 Subject: [PATCH 1/2] feat: transform some core types into ifaces to let them be extendable in plugins --- src/context.ts | 4 ++-- src/core.ts | 2 +- src/experimental.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/context.ts b/src/context.ts index 8236217b12..c28f42e487 100644 --- a/src/context.ts +++ b/src/context.ts @@ -15,7 +15,7 @@ import { AsyncLocalStorage } from 'node:async_hooks' import { spawn } from 'node:child_process' -export type Options = { +export interface Options { verbose: boolean | number cwd: string env: NodeJS.ProcessEnv @@ -30,7 +30,7 @@ export type Options = { logIgnore?: string | string[] } -export type Context = Options & { +export interface Context extends Options { nothrow?: boolean cmd: string __from: string diff --git a/src/core.ts b/src/core.ts index 1f655f7d3b..e70d707206 100644 --- a/src/core.ts +++ b/src/core.ts @@ -31,7 +31,7 @@ import psTreeModule from 'ps-tree' const psTree = promisify(psTreeModule) -interface Zx extends Options { +export interface Zx extends Options { (pieces: TemplateStringsArray, ...args: any[]): ProcessPromise } diff --git a/src/experimental.ts b/src/experimental.ts index de462fd4da..70138a4f53 100644 --- a/src/experimental.ts +++ b/src/experimental.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { ProcessOutput, $ } from './core.js' +import { ProcessOutput, $, Zx } from './core.js' import { sleep } from './goods.js' import { isString } from './util.js' import { getCtx, runInCtx } from './context.js' @@ -81,7 +81,7 @@ export function startSpinner(title = '') { )(setInterval(spin, 100)) } -export function ctx(cb: (_$: typeof $) => R): R { +export function ctx(cb: (_$: Zx) => R): R { const _$ = Object.assign($.bind(null), getCtx()) return runInCtx(_$, cb, _$) From 052861b7c4866cab23a69820c2f4712f77151fb0 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Mon, 13 Jun 2022 17:00:09 +0300 Subject: [PATCH 2/2] feat: provide configurable ref for `ctx()` --- README.md | 5 +++++ src/experimental.ts | 4 ++-- test/experimental.test.js | 8 ++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e686e05bc4..9b634d1c53 100644 --- a/README.md +++ b/README.md @@ -502,6 +502,11 @@ ctx(async ($) => { // _$.cwd refers to /foo // but _$.cwd !== $.cwd }) + +const ref = $.bind(null) +ctx(($) => { + ref === $ // true +}, ref) ``` ### `log()` diff --git a/src/experimental.ts b/src/experimental.ts index 70138a4f53..ccf15d5060 100644 --- a/src/experimental.ts +++ b/src/experimental.ts @@ -81,8 +81,8 @@ export function startSpinner(title = '') { )(setInterval(spin, 100)) } -export function ctx(cb: (_$: Zx) => R): R { - const _$ = Object.assign($.bind(null), getCtx()) +export function ctx(cb: (_$: Zx) => R, ref: Zx = $.bind(null)): R { + const _$ = Object.assign(ref, getCtx()) return runInCtx(_$, cb, _$) } diff --git a/test/experimental.test.js b/test/experimental.test.js index 0336945f69..908e1ff2cb 100644 --- a/test/experimental.test.js +++ b/test/experimental.test.js @@ -111,6 +111,14 @@ test('ctx() provides isolates running scopes', async () => { $.verbose = false }) +test('ctx accepts optional ref', () => { + const ref = $.bind(null) + + ctx(($) => { + assert.is(ref, $) + }, ref) +}) + test('bound ctx is attached to Promise', async () => { const kResourceStoreSymbol = Object.getOwnPropertySymbols( new Promise(() => {})