UNPKG

1.22 kBJavaScriptView Raw
1const { isWin, isMac } = require("./const");
2
3/**
4 * 获取命令行的参数
5 * dep 等价于 dep=true
6 * --dep 等价于 dep=true
7 */
8exports.getParams = function(arr) {
9 const reg = /=|--/i;
10 const result = arr
11 .filter((_, i) => i > 1)
12 .reduce((acc, cur) => {
13 if (!reg.test(cur)) {
14 cur = `${cur}=true`;
15 }
16 cur = cur.replace(/--([^\s]+)/, "$1=true");
17 const [key, value] = cur.split("=");
18 acc[key] = value;
19 return acc;
20 }, {});
21 return result;
22};
23
24/**
25 * 复制属性
26 */
27exports.extend = function(source, target) {
28 if (!target) return source;
29 for (var key in source) {
30 if (target[key] !== undefined) {
31 source[key] = target[key];
32 }
33 }
34 return source;
35};
36
37exports.toArray = function(source) {
38 if (Array.isArray(source)) {
39 return source;
40 }
41 if (!source) {
42 return [];
43 }
44 return [source];
45};
46
47exports.getPageOption = function(config, key) {
48 const option = Object.assign({}, config.pages, config.keys);
49 return option[key] || {};
50};
51
52exports.upath = url => {
53 if (isWin) {
54 return url.replace(/\\+/g, "\\\\");
55 } else {
56 return url.replace(/\\+/g, "/");
57 }
58};