Skip to content

Commit 881fbad

Browse files
authored
Improve validation of $.sync options binding (#551)
1 parent 5fa61d8 commit 881fbad

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,19 @@ export function execaSync(file, args, options) {
234234

235235
function create$(options) {
236236
function $(templatesOrOptions, ...expressions) {
237-
if (Array.isArray(templatesOrOptions)) {
238-
const [file, ...args] = parseTemplates(templatesOrOptions, expressions);
239-
return execa(file, args, options);
237+
if (!Array.isArray(templatesOrOptions)) {
238+
return create$({...options, ...templatesOrOptions});
240239
}
241240

242-
return create$({...options, ...templatesOrOptions});
241+
const [file, ...args] = parseTemplates(templatesOrOptions, expressions);
242+
return execa(file, args, options);
243243
}
244244

245245
$.sync = (templates, ...expressions) => {
246+
if (!Array.isArray(templates)) {
247+
throw new TypeError('Please use $(options).sync`command` instead of $.sync(options)`command`.');
248+
}
249+
246250
const [file, ...args] = parseTemplates(templates, expressions);
247251
return execaSync(file, args, options);
248252
};

test/command.js

+4
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ test('$.sync accepts options', t => {
196196
t.is(stdout, 'foo');
197197
});
198198

199+
test('$.sync must be used after options binding, not before', t => {
200+
t.throws(() => $.sync({})`noop.js`, {message: /Please use/});
201+
});
202+
199203
test('$.sync allows execa return value interpolation', t => {
200204
const foo = $.sync`echo.js foo`;
201205
const {stdout} = $.sync`echo.js ${foo} bar`;

0 commit comments

Comments
 (0)