-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
GroupHelper.js
70 lines (62 loc) · 2.17 KB
/
GroupHelper.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
66
67
68
69
70
const { resolve, join } = require('path');
const { existsSync } = require('fs');
class GroupHelper {
constructor(options) {
this.args = this.arrayToObject(options);
this.opts = {
outputOptions: {},
options: {},
processingMessageBuffer: [],
};
this.strategy = undefined;
}
hyphenToUpperCase(name) {
if (!name) {
return name;
}
return name.replace(/-([a-z])/g, function (g) {
return g[1].toUpperCase();
});
}
arrayToObject(arr) {
if (!arr) {
return;
}
return arr.reduce((result, currentItem) => {
const key = Object.keys(currentItem)[0];
result[this.hyphenToUpperCase(key)] = currentItem[key];
return result;
}, {});
}
resolveFilePath(filename = '', defaultValue) {
if (filename && Array.isArray(filename)) {
return filename.map((fp) => this.resolveFilePath(fp, defaultValue)).filter((e) => e);
}
if (filename && !filename.includes('.js')) {
filename = filename + '.js';
}
if (defaultValue && !defaultValue.includes('.js')) {
defaultValue = defaultValue + '.js';
}
let configPath;
const predefinedConfigPath = filename ? resolve(process.cwd(), filename) : undefined;
const configPathExists = predefinedConfigPath ? existsSync(predefinedConfigPath) : undefined;
if (!configPathExists) {
const LOOKUP_PATHS = [`${filename}`, `src/${filename}`, ...(defaultValue ? [defaultValue, `src/${defaultValue}`] : [])];
if (filename) {
LOOKUP_PATHS.unshift(filename);
}
LOOKUP_PATHS.forEach((p) => {
const lookUpPath = join(process.cwd(), ...p.split('/'));
if (existsSync(lookUpPath) && !configPath) {
configPath = lookUpPath;
}
});
}
return configPathExists ? predefinedConfigPath : configPath;
}
run() {
throw new Error('You must implement the "run" function');
}
}
module.exports = GroupHelper;