Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve validation of $.sync options binding #551

Merged
merged 1 commit into from Mar 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions index.js
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
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`node test/fixtures/echo.js foo`;
const {stdout} = $.sync`node test/fixtures/echo.js ${foo} bar`;
Expand Down