create.js
This file
Path
lib/methods/create.js
65 lines · 2.5 KiB · JavaScript
Last changed by
Harden some thingsSindre Sorhusbefore adoption4a0a121
History of this fileTake it
The walk stopped after 200 commits: lines older than c8a6ed7 point at it.
Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
lines 1–52why, one step back1import isPlainObject from 'is-plain-obj';2import {normalizeParameters} from './parameters.js';3import {isTemplateString, parseTemplates} from './template.js';4import {execaCoreSync} from './main-sync.js';5import {execaCoreAsync} from './main-async.js';6import {mergeOptions} from './bind.js';78// Wraps every exported methods to provide the following features:9// - template string syntax: execa`command argument`10// - options binding: boundExeca = execa(options)11// - optional argument/options: execa(file), execa(file, args), execa(file, options), execa(file, args, options)12// `mapArguments()` and `setBoundExeca()` allows for method-specific logic.13export const createExeca = (mapArguments, boundOptions, deepOptions, setBoundExeca) => {14 const createNested = (mapArguments, boundOptions, setBoundExeca) => createExeca(mapArguments, boundOptions, deepOptions, setBoundExeca);15 const boundExeca = (...execaArguments) => callBoundExeca({16 mapArguments,17 deepOptions,18 boundOptions,19 setBoundExeca,20 createNested,21 }, ...execaArguments);2223 if (setBoundExeca !== undefined) {24 setBoundExeca(boundExeca, createNested, boundOptions);25 }2627 return boundExeca;28};2930const callBoundExeca = ({mapArguments, deepOptions = {}, boundOptions = {}, setBoundExeca, createNested}, firstArgument, ...nextArguments) => {31 if (isPlainObject(firstArgument)) {32 return createNested(mapArguments, mergeOptions(boundOptions, firstArgument), setBoundExeca);33 }3435 const {file, commandArguments, options, isSync} = parseArguments({36 mapArguments,37 firstArgument,38 nextArguments,39 deepOptions,40 boundOptions,41 });42 return isSync43 ? execaCoreSync(file, commandArguments, options)44 : execaCoreAsync(file, commandArguments, options, createNested);45};4647const parseArguments = ({mapArguments, firstArgument, nextArguments, deepOptions, boundOptions}) => {48 const callArguments = isTemplateString(firstArgument)49 ? parseTemplates(firstArgument, nextArguments)50 : [firstArgument, ...nextArguments];51 const [initialFile, initialArguments, initialOptions] = normalizeParameters(...callArguments);52 const mergedOptions = mergeOptions(mergeOptions(deepOptions, boundOptions), initialOptions);Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 53–54why, one step back53 // `mapArguments()` returns a plain object, so its result is read from a null-prototype copy.54 // Otherwise a polluted `Object.prototype` would replace every option, or switch the execution mode.Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
lines 55–57why, one step back55 const {56 options = mergedOptions,57 isSync = false,Harden some thingsSindre Sorhusbefore adoption4a0a121
line 58why, one step back58 } = {__proto__: null, ...mapArguments({options: mergedOptions})};Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
line 59why, one step back59 return {Remove `execaCommand()` and `execaCommandSync()` (#1244)Sindre Sorhusbefore adoption3ced394
lines 60–61why, one step back60 file: initialFile,61 commandArguments: initialArguments,Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
lines 62–65why, one step back62 options,63 isSync,64 };65};