-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcfg-resolve.js
More file actions
110 lines (89 loc) · 2.68 KB
/
Copy pathcfg-resolve.js
File metadata and controls
110 lines (89 loc) · 2.68 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import path from 'path';
import {cosmiconfigSync} from 'cosmiconfig';
import toCamelCase from 'to-camel-case';
import mergeOptions from 'merge-options';
import normalizePath from 'normalize-path';
import fg from 'fast-glob';
export default ({input, flags = {}}) => {
const explorer = cosmiconfigSync('posthtml');
let {
config,
use,
options = {},
output,
root = './',
skip = [],
allInOutput = false
} = flags;
if (config) {
({config} = explorer.load(config));
}
if (use) {
const configPluginOptions = config?.plugins ?? {};
// Plugins defined via CLI options take precedence over the ones from config file.
use = [].concat(use).reduce((cfg, name) => {
let cliOptions = flags[toCamelCase(name)];
let configOptions = configPluginOptions[name];
// We merge this way because options can be both strings and objects.
const merged = mergeOptions({[name]: configOptions}, {[name]: cliOptions || {}});
// Assigning as we loop `use` makes sure that the order in cfg.plugins is correct.
cfg.plugins[name] = merged[name];
if (configOptions) {
delete configPluginOptions[name];
}
return cfg;
}, {plugins: {}});
// Add the remaining plugins if there is any.
if (config && config.plugins) {
for (let name in configPluginOptions) {
if (configPluginOptions[name]) {
use.plugins[name] = configPluginOptions[name];
}
}
// Now all the plugins are in `use.plugins`.
// Delete `config.plugins` for correct merging later: mergeOptions(config, {...}, use)
delete config.plugins;
}
}
if (!config && !use) {
const search = explorer.search();
config = search?.config;
}
if (config?.root) {
root = config.root;
}
if (config?.allInOutput) {
allInOutput = config.allInOutput;
}
if (config?.skip) {
skip = skip.concat(config.skip);
}
input = []
.concat(input && input.length > 0 ? input : config?.input)
.filter(Boolean)
.map(file => {
const ignoreFile = file.startsWith('!');
let ignoreSymbol = '';
if (ignoreFile) {
ignoreSymbol = '!';
file = file.slice(1);
}
return `${ignoreSymbol}${normalizePath(path.join(path.resolve(root), file))}`;
});
if (input.length === 0) {
throw new TypeError('input files not found');
}
output = output ?? config?.output;
if (output) {
output = normalizePath(output);
}
skip = fg.sync(skip, {cwd: path.resolve(root)}).map(file => normalizePath(path.join(path.resolve(root), file)));
return mergeOptions(config ?? {}, {
input,
output,
options,
root,
allInOutput,
skip
}, use ?? {});
};