/
utils.ts
171 lines (153 loc) · 4.76 KB
/
utils.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @packageModule
* @module Utils
*/
import path from "path";
import { cursorTo, clearScreenDown } from "readline";
import rc from "rc";
import flatten, { unflatten } from "flat";
import {
Config,
ConfigValue,
ConfigDefault,
ConfigFlat,
ConfigCheck
} from "./interfaces";
import DEFAULT_CONFIG from "./default-config";
/**
* Loads a configuration object. Merging .nodehawkrc (if it exists) and the default configuration file. Otherwise returns the default configuration.
* @module Utils
* @category Core
*/
export function loadConfiguration(): Config {
const defaultConfig: Config = unflatten(
Object.entries(DEFAULT_CONFIG).reduce<ConfigFlat>(function(
flatConfig: ConfigFlat,
[key, value]: [string, ConfigDefault]
): ConfigFlat {
return {
...flatConfig,
[key]: value.default
};
},
{} as ConfigFlat)
) as Config;
return rc("nodehawk", defaultConfig) as Config;
}
/**
* Checks whether the configuration object types match the ones expected.
* @param {Config} config The configuration object to check.
* @category Core
*/
export function checkConfig(config: Config): ConfigCheck {
const configFlat: ConfigFlat = flatten(config, {
safe: true
});
const entries: [string, ConfigValue][] = Object.entries(configFlat);
for (const entry of entries) {
const [key, entryValue]: [string, ConfigValue] = entry;
if (!key.includes("env.")) {
const defaultValueObject: ConfigDefault = DEFAULT_CONFIG[key];
if (!defaultValueObject) {
return {
success: false,
keyError: true,
key
};
}
const { type: desiredType }: ConfigDefault = defaultValueObject;
const availableTypes: string[] = desiredType.split("|");
const arrayTypes: string[] = availableTypes.filter(type =>
/\[*\]/.test(type)
);
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const providedType: string = resolveTypeof(entryValue);
if (
!availableTypes.includes(providedType) &&
!(
Array.isArray(entryValue) &&
arrayTypes.some(arrayType => {
const type = arrayType
.replace(/\[|\]/g, "")
.toLowerCase();
return entryValue.every(value => typeof value === type);
})
)
) {
return {
success: false,
key,
desiredType,
providedType
};
}
}
}
return {
success: true
};
}
/**
* Returns a root-relative joined paths to with the supplied paths.
*
* @param {string} root A path string resolving to the root directory.
* @param {(string|string[])} paths A string or an array of paths to resolve with respect to root.
* @category Core
*/
export function generateBasePathRelativeToRoot(
root: string,
paths: string | string[]
): string | string[] {
if (Array.isArray(paths)) {
return paths.reduce(
(updatedPaths: string[], currentPath: string): string[] => [
...updatedPaths,
path.join(root, currentPath)
],
[]
);
} else {
return path.join(root, paths);
}
}
/**
* Clears the terminal screen.
* @category Utils
*/
export function clearScreen(): void {
const blank = "\n".repeat(process.stdout.rows);
console.log(blank);
cursorTo(process.stdout, 0, 0);
clearScreenDown(process.stdout);
}
/**
* Resolve the type from string, used to validate configuration object.
* @param {any} value Value of the key of configuration object.
* @category Utils
*/
function resolveTypeof(value: any): string {
if (Array.isArray(value)) {
return "array";
}
if (typeof value === "object" && value instanceof RegExp) {
return "regexp";
}
if (typeof value === "undefined" || value === null) {
return "undefined";
}
return typeof value;
}
/**
* Assign environemnt variables from the configuration to the process.
* @param {Config} config The configuration object.
* @category Core
*/
export function assignEnvironmentVariables(config: Config): void {
process.env.PORT = config.port.toString();
const environmentVariables = Object.entries(config.env);
if (environmentVariables.length) {
environmentVariables.forEach(([key, value]): void => {
process.env[key] = value.toString();
});
}
}