Skip to content
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
6 changes: 3 additions & 3 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"README.md",
"LICENSE"
],
"limit": "128.66 kB",
"limit": "128.85 kB",
"brotli": false,
"gzip": false
},
Expand All @@ -33,7 +33,7 @@
"build/globals.js",
"build/deno.js"
],
"limit": "849.95 kB",
"limit": "850.20 kB",
"brotli": false,
"gzip": false
},
Expand Down Expand Up @@ -66,7 +66,7 @@
"README.md",
"LICENSE"
],
"limit": "911.66 kB",
"limit": "911.85 kB",
"brotli": false,
"gzip": false
}
Expand Down
31 changes: 18 additions & 13 deletions build/core.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -452,34 +452,39 @@ var getSnapshot = (opts, from, pieces, args) => __spreadProps(__spreadValues({},
function within(callback) {
return storage.run(__spreadValues({}, getStore()), callback);
}
var $ = new Proxy(
// prettier-ignore
var $ = sync$(
function(pieces, ...args) {
const opts = getStore();
if (!Array.isArray(pieces)) {
return function(...args2) {
return within(() => Object.assign($, opts, pieces).apply(this, args2));
};
}
if (!Array.isArray(pieces))
return sync$(
function(...args2) {
return within(() => Object.assign($, opts, pieces).apply(this, args2));
},
() => $(__spreadProps(__spreadValues({}, pieces), { sync: true }))
);
const from = Fail.getCallerLocation();
const cb = () => cb[SHOT] = getSnapshot(opts, from, pieces, args);
const pp = new ProcessPromise(cb);
if (!pp.isHalted()) pp.run();
return pp.sync ? pp.output : pp;
},
{
() => $({ sync: true })
);
function sync$(fn, makeSync) {
return new Proxy(fn, {
get(t, key) {
if (key === "sync") return makeSync();
return Reflect.get(key in Function.prototype ? t : getStore(), key);
},
set(t, key, value) {
return Reflect.set(
key in Function.prototype ? t : getStore(),
key === "sync" ? SYNC : key,
value
);
},
get(t, key) {
return key === "sync" ? $({ sync: true }) : Reflect.get(key in Function.prototype ? t : getStore(), key);
}
}
);
});
}
var _ProcessPromise = class _ProcessPromise extends Promise {
constructor(executor) {
let resolve;
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@
"globby": {
"fast-glob": "3.3.3"
},
"c8": {
"test-exclude": {
"minimatch": "10.2.2"
}
},
"tsx": {
"esbuild": "$esbuild"
},
Expand Down
41 changes: 24 additions & 17 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,38 +191,45 @@ export interface Shell<
// The zx
export type $ = Shell & Options

export const $: $ = new Proxy<$>(
// prettier-ignore
export const $: $ = sync$(
function (pieces: TemplateStringsArray | Partial<Options>, ...args: any[]) {
const opts = getStore()
if (!Array.isArray(pieces)) {
return function (this: any, ...args: any) {
return within(() => Object.assign($, opts, pieces).apply(this, args))
}
}

if (!Array.isArray(pieces))
return sync$(
function (this: any, ...args: any) {
return within(() => Object.assign($, opts, pieces).apply(this, args))
} as $,
() => $({ ...pieces, sync: true })
)

const from = Fail.getCallerLocation()
const cb: PromiseCallback = () => (cb[SHOT] = getSnapshot(opts, from, pieces as TemplateStringsArray, args))
const pp = new ProcessPromise(cb)
const cb: PromiseCallback = () =>
(cb[SHOT] = getSnapshot(opts, from, pieces as TemplateStringsArray, args))

const pp = new ProcessPromise(cb)
if (!pp.isHalted()) pp.run()

return pp.sync ? pp.output : pp
} as $,
{
() => $({ sync: true })
)

function sync$(fn: $, makeSync: () => $): $ {
return new Proxy(fn, {
get(t, key) {
if (key === 'sync') return makeSync()
return Reflect.get(key in Function.prototype ? t : getStore(), key)
},
set(t, key, value) {
return Reflect.set(
key in Function.prototype ? t : getStore(),
key === 'sync' ? SYNC : key,
value
)
},
get(t, key) {
return key === 'sync'
? $({ sync: true })
: Reflect.get(key in Function.prototype ? t : getStore(), key)
},
}
)
})
}

type ProcessStage = 'initial' | 'halted' | 'running' | 'fulfilled' | 'rejected'

Expand Down
3 changes: 3 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ describe('core', () => {

const $3 = $({ sync: true })({ nothrow: true })
assert.equal($3`exit 3`.exitCode, 3)

assert.equal($1.sync`exit 4`.exitCode, 4)
assert.equal($.sync({ nothrow: true })`exit 5`.exitCode, 5)
})

test('handles `nothrow` option', async () => {
Expand Down
Loading