deno.land / x / velociraptor@1.5.0 / src / load_config.ts

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
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
import { existsSync, parseYaml, path } from "../deps.ts";import { ScriptsConfiguration } from "./scripts_config.ts";
const CONFIG_FILE_NAMES = ["scripts", "velociraptor"];const STATIC_CONFIG_FILE_EXTENSIONS = ["yaml", "yml", "json"];const DYNAMIC_CONFIG_FILE_EXTENSIONS = ["ts", "js", "mjs"];const CONFIG_DENO_FILE_NAMES = ["deno.json", "deno.jsonc"];const CONFIG_FILE_EXTENSIONS = [...STATIC_CONFIG_FILE_EXTENSIONS, ...DYNAMIC_CONFIG_FILE_EXTENSIONS];
export interface ConfigData { cwd: string; config: ScriptsConfiguration;}
export async function loadConfig(): Promise<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: await parseConfig(p, DYNAMIC_CONFIG_FILE_EXTENSIONS.includes(ext)), }; } } } for (const file of CONFIG_DENO_FILE_NAMES) { const p = path.join(dir, file); if (existsSync(p)) { return { cwd: dir, config: await parseDenoConfig(p), }; } } dir = parent(dir); } return null;}
function parent(dir: string) { return path.join(dir, "..");}
async function parseConfig( configPath: string, isDynamic: boolean,): Promise<ScriptsConfiguration> { if (isDynamic) { return (await import(`file://${configPath}`)) .default as ScriptsConfiguration; } return parseYaml( Deno.readTextFileSync(configPath), ) as ScriptsConfiguration;}
async function parseDenoConfig( configPath: string,): Promise<ScriptsConfiguration> { let content = Deno.readTextFileSync(configPath); // Strips comments for .jsonc (credits to @tarkh) if (/\.jsonc$/.test(configPath)) { content = content.replace( /\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m, ); } const { velociraptor: config = {} } = JSON.parse(content); return config as ScriptsConfiguration;}
velociraptor

Version Info

Tagged at
2 years ago