UNPKG

1.9 kBJavaScriptView Raw
1const {dirname} = require('path');
2const {isString, isFunction, castArray, isArray, isPlainObject, isNil} = require('lodash');
3const resolveFrom = require('resolve-from');
4
5const validateSteps = (conf) => {
6 return conf.every((conf) => {
7 if (
8 isArray(conf) &&
9 (conf.length === 1 || conf.length === 2) &&
10 (isString(conf[0]) || isFunction(conf[0])) &&
11 (isNil(conf[1]) || isPlainObject(conf[1]))
12 ) {
13 return true;
14 }
15
16 conf = castArray(conf);
17
18 if (conf.length !== 1) {
19 return false;
20 }
21
22 const [name, config] = parseConfig(conf[0]);
23 return (isString(name) || isFunction(name)) && isPlainObject(config);
24 });
25};
26
27function validatePlugin(conf) {
28 return (
29 isString(conf) ||
30 (isArray(conf) &&
31 (conf.length === 1 || conf.length === 2) &&
32 (isString(conf[0]) || isPlainObject(conf[0])) &&
33 (isNil(conf[1]) || isPlainObject(conf[1]))) ||
34 (isPlainObject(conf) && (isNil(conf.path) || isString(conf.path) || isPlainObject(conf.path)))
35 );
36}
37
38function validateStep({required}, conf) {
39 conf = castArray(conf).filter(Boolean);
40 if (required) {
41 return conf.length >= 1 && validateSteps(conf);
42 }
43
44 return conf.length === 0 || validateSteps(conf);
45}
46
47function loadPlugin({cwd}, name, pluginsPath) {
48 const basePath = pluginsPath[name]
49 ? dirname(resolveFrom.silent(__dirname, pluginsPath[name]) || resolveFrom(cwd, pluginsPath[name]))
50 : __dirname;
51 return isFunction(name) ? name : require(resolveFrom.silent(basePath, name) || resolveFrom(cwd, name));
52}
53
54function parseConfig(plugin) {
55 let path;
56 let config;
57 if (isArray(plugin)) {
58 [path, config] = plugin;
59 } else if (isPlainObject(plugin) && !isNil(plugin.path)) {
60 ({path, ...config} = plugin);
61 } else {
62 path = plugin;
63 }
64
65 return [path, config || {}];
66}
67
68module.exports = {validatePlugin, validateStep, loadPlugin, parseConfig};