From fe7d8b75e6784747288a9cc270cd704582533620 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 12 Mar 2023 23:52:02 +0000 Subject: [PATCH] Improve validation of `$.sync` options binding --- index.js | 12 ++++++++---- test/command.js | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 87c827624c..963c96dc78 100644 --- a/index.js +++ b/index.js @@ -234,15 +234,19 @@ export function execaSync(file, args, options) { function create$(options) { function $(templatesOrOptions, ...expressions) { - if (Array.isArray(templatesOrOptions)) { - const [file, ...args] = parseTemplates(templatesOrOptions, expressions); - return execa(file, args, options); + if (!Array.isArray(templatesOrOptions)) { + return create$({...options, ...templatesOrOptions}); } - return create$({...options, ...templatesOrOptions}); + const [file, ...args] = parseTemplates(templatesOrOptions, expressions); + return execa(file, args, options); } $.sync = (templates, ...expressions) => { + if (!Array.isArray(templates)) { + throw new TypeError('Please use $(options).sync`command` instead of $.sync(options)`command`.'); + } + const [file, ...args] = parseTemplates(templates, expressions); return execaSync(file, args, options); }; diff --git a/test/command.js b/test/command.js index 43b74955f4..670e2f3f68 100644 --- a/test/command.js +++ b/test/command.js @@ -196,6 +196,10 @@ test('$.sync accepts options', t => { t.is(stdout, 'foo'); }); +test('$.sync must be used after options binding, not before', t => { + t.throws(() => $.sync({})`noop.js`, {message: /Please use/}); +}); + test('$.sync allows execa return value interpolation', t => { const foo = $.sync`node test/fixtures/echo.js foo`; const {stdout} = $.sync`node test/fixtures/echo.js ${foo} bar`;