/
Main.ts
95 lines (83 loc) · 2.82 KB
/
Main.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
import Watcher from "./Watcher";
import Provider from "../higher-order/Provider";
import { Config, ConfigCheck, WatcherListeners } from "interfaces";
import {
loadConfiguration,
checkConfig,
assignEnvironmentVariables,
clearScreen
} from "../../utils";
/**
* Nodehawk is a hyper configurable watcher for all of your Node server development needs.
* @extends Provider
* @category Core
* @example
* ```
* import Nodehawk from 'nodehawk';
* const nodehawk = new Nodehawk();
* ```
*/
export default class Main extends Provider {
/**
* Instance of `Watcher` created for this instance.
*/
private watcher: Watcher;
/**
* Create an instance of `Nodehawk` which reads the configuration file and spawns a watcher to execute commands.
* @param {Config} [userConfig] Part of a custom config, to override defaults. Useful while running Nodehawk programmatically.
* @param {WatcherListeners} [watcherListeners] A list of custom listeners to run against certain events.
*/
constructor(userConfig?: Config, watcherListeners?: WatcherListeners) {
userConfig = userConfig ?? {};
const {
config: configPath,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
configs: configsPath,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_,
...config
} = loadConfiguration();
super({
...config,
...userConfig
});
const {
success,
key,
keyError,
desiredType,
providedType
}: ConfigCheck = checkConfig(this.config);
if (this.config.clearScreen) {
clearScreen();
}
if (!success) {
if (keyError) {
this.log.fatal(
new Error(
`Key ${key} is not valid. Please refer to the configuration document and provide a valid key.`
)
);
} else {
this.log.fatal(
new Error(
`Key ${key} in configuration has a type mismatch. Expected ${desiredType
.split("|")
.join(", ")} but instead got ${providedType}.`
)
);
}
}
this.log.info(
configPath
? `Configuration loaded from ${configPath}`
: "No configuration found. Using default configurations."
);
this.log.debug("Configuration verified.");
assignEnvironmentVariables(this.config);
/**
* Create an instance of the watcher. This also starts the watch server.
*/
this.watcher = new Watcher(this.config, watcherListeners);
}
}