Skip to content

Commit 46ba584

Browse files
author
Mark Molinaro
committed
Allow for optional exec opts to be passed in on wrap creation
1 parent 642ceed commit 46ba584

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

execr.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ type ExecAsyncFunction = (...args: ExecParameters) => AttachedPromise<ExecResult
3232
const memoizeSpawnSync = memoize(spawn.sync, (...args) => JSON.stringify(args));
3333
const isObject = (maybeObj: unknown) => Object.prototype.toString.call(maybeObj) == "[object Object]";
3434

35-
const normalizeArgs = (maybeArgs: MaybeArgs, options?: ExecOpts) => {
36-
const defaultArgs = {
35+
const normalizeArgs = (maybeArgs: MaybeArgs, suppliedOptions?: ExecOpts, defaultOptions?: ExecOpts) => {
36+
const globalDefaultOpts = {
3737
maxBuffer: 1024 * 1024 * 10,
3838
failOnError: true,
3939
memoize: false
4040
}
4141

42-
let finalOpts = isObject(maybeArgs) ? maybeArgs as ExecOpts : options || {};
43-
finalOpts = { ...defaultArgs, ...finalOpts }
42+
const normalizedDefaultOpts = defaultOptions || {};
43+
44+
let finalOpts = isObject(maybeArgs) ? maybeArgs as ExecOpts : suppliedOptions || {};
45+
finalOpts = { ...globalDefaultOpts, ...normalizedDefaultOpts, ...finalOpts }
4446

4547
const finalArgs = Array.isArray(maybeArgs) ? maybeArgs.filter(Boolean) : [];
4648

@@ -130,18 +132,19 @@ function exec(
130132
* Example `az.artifacts.universal.download(["--file", "<name>"])`
131133
*
132134
* @param command - string of command which will be passed as first argument to exec.
135+
* @param defaultOptions - Default options to be used for all invocations of this wrapped command.
133136
* Examples: `git`, `yarn`, `az`.
134137
*/
135-
function _wrap<T extends AnyFunction>(fn: T, cmd: string): AnyKey<AnyKey<AnyKey<AnyKey<AnyKey<AnyKey<AnyKey<CurriedFunction<T>>>>>>>>;
136-
function _wrap<T extends AnyFunction>(fn: T, cmd: string): CurriedFunction<T> {
138+
function _wrap<T extends AnyFunction>(fn: T, cmd: string, defaultOpts?: ExecOpts): AnyKey<AnyKey<AnyKey<AnyKey<AnyKey<AnyKey<AnyKey<CurriedFunction<T>>>>>>>>;
139+
function _wrap<T extends AnyFunction>(fn: T, cmd: string, defaultOpts?: ExecOpts): CurriedFunction<T> {
137140
let args: string[] | undefined = [];
138141

139-
function _fn(...[endArgsOrOpts, opts]: Parameters<CurriedFunction<T>>): ReturnType<CurriedFunction<T>> {
142+
function _fn(...[endArgsOrOpts, suppliedOpts]: Parameters<CurriedFunction<T>>): ReturnType<CurriedFunction<T>> {
140143
if (!args) {
141144
args = [];
142145
}
143146

144-
const [endArgs, execOpts] = normalizeArgs(endArgsOrOpts, opts);
147+
const [endArgs, execOpts] = normalizeArgs(endArgsOrOpts, suppliedOpts, defaultOpts);
145148

146149
try {
147150
return fn(cmd, args.concat(endArgs), execOpts);
@@ -166,8 +169,8 @@ function _wrap<T extends AnyFunction>(fn: T, cmd: string): CurriedFunction<T> {
166169
return new Proxy(_fn, handler);
167170
}
168171

169-
const wrap = (cmd: string) => _wrap<ExecFunction>(exec, cmd);
170-
const wrapAsync = (cmd: string) => _wrap<ExecAsyncFunction>(execAsync, cmd);
172+
const wrap = (cmd: string, defaultOptions?: ExecOpts) => _wrap<ExecFunction>(exec, cmd, defaultOptions);
173+
const wrapAsync = (cmd: string, defaultOptions?: ExecOpts) => _wrap<ExecAsyncFunction>(execAsync, cmd, defaultOptions);
171174

172175
export {
173176
exec,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@markjm/execr",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "Utility to streamline exec calls",
55
"main": "execr.js",
66
"types": "execr.d.ts",

0 commit comments

Comments
 (0)