-
Notifications
You must be signed in to change notification settings - Fork 23
/
load_config.ts
44 lines (39 loc) · 1012 Bytes
/
load_config.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
import {
parseYaml,
path,
existsSync,
readFileStrSync,
} from "../deps.ts";
import { ScriptsConfiguration } from "./scripts_config.ts";
const CONFIG_FILE_NAMES = ["scripts", "velociraptor"];
const CONFIG_FILE_EXTENSIONS = ["yaml", "yml", "json"];
export interface ConfigData {
cwd: string;
config: ScriptsConfiguration;
}
export function loadConfig(): ConfigData | null {
let ext, name, dir = Deno.cwd();
while (parent(dir) !== dir) {
for (ext of CONFIG_FILE_EXTENSIONS) {
for (name of CONFIG_FILE_NAMES) {
const p = `${path.join(dir, name)}.${ext}`;
if (existsSync(p)) {
return {
cwd: dir,
config: parseConfig(p),
};
}
}
}
dir = parent(dir);
}
return null;
}
function parent(dir: string) {
return path.join(dir, "..");
}
function parseConfig(configPath: string): ScriptsConfiguration {
return parseYaml(
readFileStrSync(configPath, { encoding: "utf8" }),
) as ScriptsConfiguration;
}