-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathcreate.js
65 lines (59 loc) · 2.4 KB
/
create.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import isPlainObject from 'is-plain-obj';
import {normalizeParameters} from './parameters.js';
import {isTemplateString, parseTemplates} from './template.js';
import {execaCoreSync} from './main-sync.js';
import {execaCoreAsync} from './main-async.js';
import {mergeOptions} from './bind.js';
// Wraps every exported methods to provide the following features:
// - template string syntax: execa`command argument`
// - options binding: boundExeca = execa(options)
// - optional argument/options: execa(file), execa(file, args), execa(file, options), execa(file, args, options)
// `mapArguments()` and `setBoundExeca()` allows for method-specific logic.
export const createExeca = (mapArguments, boundOptions, deepOptions, setBoundExeca) => {
const createNested = (mapArguments, boundOptions, setBoundExeca) => createExeca(mapArguments, boundOptions, deepOptions, setBoundExeca);
const boundExeca = (...execaArguments) => callBoundExeca({
mapArguments,
deepOptions,
boundOptions,
setBoundExeca,
createNested,
}, ...execaArguments);
if (setBoundExeca !== undefined) {
setBoundExeca(boundExeca, createNested, boundOptions);
}
return boundExeca;
};
const callBoundExeca = ({mapArguments, deepOptions = {}, boundOptions = {}, setBoundExeca, createNested}, firstArgument, ...nextArguments) => {
if (isPlainObject(firstArgument)) {
return createNested(mapArguments, mergeOptions(boundOptions, firstArgument), setBoundExeca);
}
const {file, commandArguments, options, isSync} = parseArguments({
mapArguments,
firstArgument,
nextArguments,
deepOptions,
boundOptions,
});
return isSync
? execaCoreSync(file, commandArguments, options)
: execaCoreAsync(file, commandArguments, options, createNested);
};
const parseArguments = ({mapArguments, firstArgument, nextArguments, deepOptions, boundOptions}) => {
const callArguments = isTemplateString(firstArgument)
? parseTemplates(firstArgument, nextArguments)
: [firstArgument, ...nextArguments];
const [initialFile, initialArguments, initialOptions] = normalizeParameters(...callArguments);
const mergedOptions = mergeOptions(mergeOptions(deepOptions, boundOptions), initialOptions);
const {
file = initialFile,
commandArguments = initialArguments,
options = mergedOptions,
isSync = false,
} = mapArguments({file: initialFile, commandArguments: initialArguments, options: mergedOptions});
return {
file,
commandArguments,
options,
isSync,
};
};