Skip to content

Commit

Permalink
Improve validation of $.sync options binding (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 14, 2023
1 parent 5fa61d8 commit 881fbad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
4 changes: 4 additions & 0 deletions test/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`echo.js foo`;
const {stdout} = $.sync`echo.js ${foo} bar`;
Expand Down

0 comments on commit 881fbad

Please sign in to comment.