forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
67 lines (56 loc) · 1.87 KB
/
config.ts
File metadata and controls
67 lines (56 loc) · 1.87 KB
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
import { createError } from './errors.js';
export interface Config {
debug: boolean;
noColor: boolean;
output: 'text' | 'json';
apiToken?: string;
projectPath?: string;
}
const parseNoColor = (): boolean => {
return process.env.NO_COLOR === '1' || process.env.NO_COLOR === 'true';
};
const parseOutputFormat = (): 'text' | 'json' => {
return process.env.OUTPUT_FORMAT === 'json' ? 'json' : 'text';
};
const getEnvConfig = (): Partial<Config> => {
return {
debug: process.env.DEBUG === 'auto-engineer',
noColor: parseNoColor(),
output: parseOutputFormat(),
apiToken: process.env.AUTO_ENGINEER_API_TOKEN,
projectPath: process.env.AUTO_ENGINEER_PROJECT_PATH,
};
};
const getDefaultConfig = (): Config => ({
debug: false,
noColor: false,
output: 'text',
});
export const loadConfig = (cliArgs: Partial<Config> = {}): Config => {
const envConfig = getEnvConfig();
const defaultConfig = getDefaultConfig();
// Merge in order of precedence: CLI args > env vars > defaults
return {
...defaultConfig,
...envConfig,
...cliArgs,
};
};
const validateApiToken = (apiToken: string | undefined): void => {
if (apiToken !== undefined && apiToken !== null && apiToken.length > 0 && apiToken.length < 10) {
throw createError('API token must be at least 10 characters long', 'E4003');
}
};
const validateProjectPath = (projectPath: string | undefined): void => {
if (projectPath !== undefined && projectPath !== null && projectPath.length > 0) {
const isValidPath = projectPath.startsWith('/') || projectPath.startsWith('./');
if (!isValidPath) {
throw createError('Project path must be an absolute path or relative path starting with ./', 'E4004');
}
}
};
export const validateConfig = (config: Config): void => {
const { apiToken, projectPath } = config;
validateApiToken(apiToken);
validateProjectPath(projectPath);
};