deno.land / x / denon@2.5.0 / src / 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2020-2021 the denosaurs team. All rights reserved. MIT license.
import { existsSync, extname, globToRegExp, JSON_SCHEMA, log, parseYaml, resolve,} from "../deps.ts";
import type { Args } from "./args.ts";import type { RunnerConfig } from "./runner.ts";import type { Template } from "./templates.ts";import type { WatcherConfig } from "./watcher.ts";
import { merge } from "./merge.ts";
const logger = log.create("conf");
/** Possible default configuration files. */export const configs = [ "denon.yaml", "denon.yml", "denon.json",
"scripts.json", "scripts.yml", "scripts.yaml",
"denon.config.ts", "scripts.config.ts", "denon.config.js", "scripts.config.js",
"scripts.ts", "scripts.js",];
export const reConfig = new RegExp( configs .map((_) => `**/${_}`) .map((_) => globToRegExp(_).source) .join("|"), // i know, right);
export type DenonConfig = RunnerConfig & Partial<CompleteDenonConfig>;
/** Parameters are not optional */export interface CompleteDenonConfig extends RunnerConfig { [key: string]: unknown; watcher: WatcherConfig; args?: Args; logger: { quiet: boolean; debug: boolean; fullscreen: boolean; }; configPath: string;}
/** The default denon configuration */export const DEFAULT_DENON_CONFIG: CompleteDenonConfig = { scripts: {}, watcher: { interval: 350, paths: [], exts: ["ts", "tsx", "js", "jsx", "json"], match: ["**/*.*"], skip: ["**/.git/**"], }, watch: true, logger: { quiet: false, debug: false, fullscreen: false, }, configPath: "",};
/** Read YAML config, throws if YAML format is not valid */async function readYaml(file: string): Promise<unknown> { const source = await Deno.readTextFile(file); return parseYaml(source, { schema: JSON_SCHEMA, json: true, });}
/** Read JSON config, throws if JSON format is not valid */async function readJson(file: string): Promise<unknown> { const source = await Deno.readTextFile(file); return JSON.parse(source);}
/** Safe import a TypeScript file */async function importConfig( file: string,): Promise<Partial<DenonConfig> | undefined> { try { const configRaw = await import(`file://${resolve(file)}`); return configRaw.default as Partial<DenonConfig>; } catch (error) { logger.error(error.message ?? "Error opening ts config config"); return; }}
/** Clean config from malformed strings */function cleanConfig( config: Partial<DenonConfig>, file?: string,): Partial<DenonConfig> { if (config.watcher) { if (config.watcher.exts) { config.watcher.exts = config.watcher.exts.map((_) => _.startsWith(".") ? _.substr(1) : _ ); }
if (config.watcher.skip) { config.watcher.skip = config.watcher.skip.map((_) => _.startsWith("./") ? _.substr(2) : _ ); }
if (config.watcher.match) { config.watcher.match = config.watcher.match.map((_) => _.startsWith("./") ? _.substr(2) : _ ); } } if (file) { config.configPath = resolve(file); } return config;}
/** Returns, if exists, the config filename */export function getConfigFilename(): string | undefined { return configs.find((filename) => { return existsSync(filename) && Deno.statSync(filename).isFile; });}
/** Reads the denon config from a file */export async function readConfig( file: string | undefined = getConfigFilename(),): Promise<CompleteDenonConfig> { let config: CompleteDenonConfig = DEFAULT_DENON_CONFIG; if (!config.watcher.paths) config.watcher.paths = []; config.watcher.paths.push(Deno.cwd());
if (file) { if (file.endsWith(".js") || file.endsWith(".ts")) { const parsed = await importConfig(file); if (parsed) { config = merge( config, cleanConfig(parsed as Partial<DenonConfig>, file), ); } } else { try { const extension = extname(file); if (/^\.ya?ml$/.test(extension)) { const parsed = await readYaml(file); config = merge( config, cleanConfig(parsed as Partial<DenonConfig>, file), ); } else if (/^\.json$/.test(extension)) { const parsed = await readJson(file); config = merge( config, cleanConfig(parsed as Partial<DenonConfig>, file), ); } else { try { const parsed = await readJson(file); config = merge( config, cleanConfig(parsed as Partial<DenonConfig>, file), ); } catch { const parsed = await readYaml(file); config = merge( config, cleanConfig(parsed as Partial<DenonConfig>, file), ); } } } catch { logger.warning(`unsupported configuration \`${file}\``); } } } return config;}
/** Reads the denon config from a file */export async function writeConfigTemplate(template: Template): Promise<void> { try { logger.info(`writing template to \`${template.filename}\``); await Deno.writeTextFile( resolve(Deno.cwd(), template.filename), template.source, ); logger.info( `\`${template.filename}\` created in current working directory`, ); } catch { logger.error( `\`${template.filename}\` cannot be saved in current working directory`, ); }}
denon

Version Info

Tagged at
2 years ago