-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
configParser.ts
57 lines (49 loc) · 1.44 KB
/
configParser.ts
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
import * as path from 'path';
import chalk from 'chalk';
import * as prettyjson from 'prettyjson';
export function getNameFromPath(fullPath: string): string {
const filename = fullPath.replace(/^.*[\\\/]/, '');
return filename;
}
export function resolveFilePath(relativeFilePath: string): string {
const configPath = path.resolve(process.cwd() + '/' + relativeFilePath);
return configPath;
}
export function fetchConfig(configPath: string): object {
let config = null;
try {
config = require(configPath);
} catch (e) {
process.stdout.write(chalk.red(`Error:`, e.code) + `\n` + e);
}
return config;
}
const CONFIG_SCHEMA = {
plugins: 'Array',
};
function modifyConfig(config, key) {
switch (CONFIG_SCHEMA[key]) {
case 'Array':
config[key].forEach((element, idx) => {
config[key][idx] = {
name: chalk.greenBright(element.constructor.name),
...element,
};
});
}
}
export function configReader(config): string[] {
let filteredArray = [];
let options = {
noColor: true,
};
Object.keys(config).map((key): void => {
if (CONFIG_SCHEMA[key]) {
modifyConfig(config, key);
}
let rowArray = [key];
rowArray.push(prettyjson.render(config[key], options));
filteredArray = [...filteredArray, rowArray];
});
return filteredArray;
}