Improve options binding (#965)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent a1cd3cf · Browse files · 2 files·+81 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+81 added−3 removed
- tests:+58 added−1 removed
- other:+23 added−2 removed
Provenance
- Commit
c631751- Tree
03ff979- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
a1cd3cf
2 files changed+81 added−3 removed
modified:lib/arguments/create.js+23 added-2 removed
15 unmodified lines161718192020212223245 unmodified lines303132333434353637383940414243444546474849505152535455565758596061626315 unmodified lines};
const callBoundExeca = ({mapArguments, deepOptions = {}, boundOptions = {}, setBoundExeca, createNested}, firstArgument, ...nextArguments) => { if (isPlainObject(firstArgument)) { return createNested(mapArguments, {...boundOptions, ...firstArgument}, setBoundExeca); return createNested(mapArguments, mergeOptions(boundOptions, firstArgument), setBoundExeca); }
const {file, args, options, isSync} = parseArguments({mapArguments, firstArgument, nextArguments, deepOptions, boundOptions}); return isSync5 unmodified lines const callArguments = isTemplateString(firstArgument) ? parseTemplates(firstArgument, nextArguments) : [firstArgument, ...nextArguments]; const [rawFile, rawArgs, rawOptions] = normalizeArguments(...callArguments); const mergedOptions = {...deepOptions, ...boundOptions, ...rawOptions}; const mergedOptions = mergeOptions(mergeOptions(deepOptions, boundOptions), rawOptions); const { file = rawFile, args = rawArgs, options = mergedOptions, isSync = false, } = mapArguments({file: rawFile, args: rawArgs, options: mergedOptions}); return {file, args, options, isSync};};
// Deep merge specific options like `env`. Shallow merge the other ones.const mergeOptions = (boundOptions, options) => { const newOptions = Object.fromEntries( Object.entries(options).map(([optionName, optionValue]) => [ optionName, mergeOption(optionName, boundOptions[optionName], optionValue), ]), ); return {...boundOptions, ...newOptions};};
const mergeOption = (optionName, boundOptionValue, optionValue) => { if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) { return {...boundOptionValue, ...optionValue}; }
return optionValue;};
const DEEP_OPTIONS = new Set(['env']);modified:test/arguments/create.js+58 added-1 removed
1234456789101112131455 unmodified lines70717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113210 unmodified linesimport {join} from 'node:path';import test from 'ava';import {execa, execaSync, execaNode, $} from '../../index.js';import {foobarString, foobarArray} from '../helpers/input.js';import {foobarString, foobarArray, foobarUppercase} from '../helpers/input.js';import {uppercaseGenerator} from '../helpers/generator.js';import {setFixtureDir, FIXTURES_DIR} from '../helpers/fixtures-dir.js';
setFixtureDir();const NOOP_PATH = join(FIXTURES_DIR, 'noop.js');const PRINT_ENV_PATH = join(FIXTURES_DIR, 'environment.js');
const testTemplate = async (t, execaMethod) => { const {stdout} = await execaMethod`${NOOP_PATH} ${foobarString}`; t.is(stdout, foobarString);55 unmodified linestest('execaNode() bound options have lower priority', testBindPriority, execaNode);test('$ bound options have lower priority', testBindPriority, $);test('$.sync bound options have lower priority', testBindPriority, $.sync);
const testBindUndefined = async (t, execaMethod) => { const {stdout} = await execaMethod({stripFinalNewline: false})(NOOP_PATH, [foobarString], {stripFinalNewline: undefined}); t.is(stdout, foobarString);};
test('execa() undefined options use default value', testBindUndefined, execa);test('execaSync() undefined options use default value', testBindUndefined, execaSync);test('execaNode() undefined options use default value', testBindUndefined, execaNode);test('$ undefined options use default value', testBindUndefined, $);test('$.sync undefined options use default value', testBindUndefined, $.sync);
const testMergeEnv = async (t, execaMethod) => { const {stdout} = await execaMethod({env: {FOO: 'foo'}})(PRINT_ENV_PATH, {env: {BAR: 'bar'}}); t.is(stdout, 'foo\nbar');};
test('execa() bound options are merged', testMergeEnv, execa);test('execaSync() bound options are merged', testMergeEnv, execaSync);test('execaNode() bound options are merged', testMergeEnv, execaNode);test('$ bound options are merged', testMergeEnv, $);test('$.sync bound options are merged', testMergeEnv, $.sync);
const testMergeMultiple = async (t, execaMethod) => { const {stdout} = await execaMethod({env: {FOO: 'baz'}})({env: {BAR: 'bar'}})(PRINT_ENV_PATH, {env: {FOO: 'foo'}}); t.is(stdout, 'foo\nbar');};
test('execa() bound options are merged multiple times', testMergeMultiple, execa);test('execaSync() bound options are merged multiple times', testMergeMultiple, execaSync);test('execaNode() bound options are merged multiple times', testMergeMultiple, execaNode);test('$ bound options are merged multiple times', testMergeMultiple, $);test('$.sync bound options are merged multiple times', testMergeMultiple, $.sync);
const testMergeEnvUndefined = async (t, execaMethod) => { const {stdout} = await execaMethod({env: {FOO: 'foo'}})(PRINT_ENV_PATH, {env: {BAR: undefined}}); t.is(stdout, 'foo\nundefined');};
test('execa() bound options are merged even if undefined', testMergeEnvUndefined, execa);test('execaSync() bound options are merged even if undefined', testMergeEnvUndefined, execaSync);test('execaNode() bound options are merged even if undefined', testMergeEnvUndefined, execaNode);test('$ bound options are merged even if undefined', testMergeEnvUndefined, $);test('$.sync bound options are merged even if undefined', testMergeEnvUndefined, $.sync);
const testMergeSpecific = async (t, execaMethod) => { const {stdout} = await execaMethod({stdout: {transform: uppercaseGenerator().transform, objectMode: true}})(NOOP_PATH, {stdout: {transform: uppercaseGenerator().transform}}); t.is(stdout, foobarUppercase);};
test('execa() bound options only merge specific ones', testMergeSpecific, execa);test('execaSync() bound options only merge specific ones', testMergeSpecific, execaSync);test('execaNode() bound options only merge specific ones', testMergeSpecific, execaNode);test('$ bound options only merge specific ones', testMergeSpecific, $);test('$.sync bound options only merge specific ones', testMergeSpecific, $.sync);
const testSpacedCommand = async (t, args, execaMethod) => { const {stdout} = await execaMethod('command with space.js', args); const expectedStdout = args === undefined ? '' : args.join('\n'); t.is(stdout, expectedStdout);10 unmodified lines