-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig-loader.js
More file actions
207 lines (176 loc) · 6.24 KB
/
config-loader.js
File metadata and controls
207 lines (176 loc) · 6.24 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
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
import { resolve } from 'node:path';
import { cosmiconfigSync } from 'cosmiconfig';
import { validateVizzlyConfigWithDefaults } from './config-schema.js';
import {
getApiToken,
getApiUrl,
getBuildName,
getParallelId,
} from './environment-config.js';
import { getProjectMapping } from './global-config.js';
import * as output from './output.js';
const DEFAULT_CONFIG = {
// API Configuration
apiKey: undefined, // Will be set from env, global config, or CLI overrides
apiUrl: getApiUrl(),
// Server Configuration (for run command)
server: {
port: 47392,
timeout: 30000,
},
// Build Configuration
build: {
name: 'Build {timestamp}',
environment: 'test',
},
// Upload Configuration (for upload command)
upload: {
screenshotsDir: './screenshots',
batchSize: 10,
timeout: 30000,
},
// Comparison Configuration (CIEDE2000 Delta E: 0=exact, 1=JND, 2=recommended)
comparison: {
threshold: 2.0,
},
// TDD Configuration
tdd: {
openReport: false, // Whether to auto-open HTML report in browser
},
// Plugins
plugins: [],
};
export async function loadConfig(configPath = null, cliOverrides = {}) {
// 1. Load from config file using cosmiconfig
const explorer = cosmiconfigSync('vizzly');
const result = configPath ? explorer.load(configPath) : explorer.search();
let fileConfig = {};
if (result?.config) {
// Handle ESM default export (cosmiconfig wraps it in { default: {...} })
fileConfig = result.config.default || result.config;
}
// 2. Validate config file using Zod schema
const validatedFileConfig = validateVizzlyConfigWithDefaults(fileConfig);
// Create a proper clone of the default config to avoid shared object references
const config = {
...DEFAULT_CONFIG,
server: { ...DEFAULT_CONFIG.server },
build: { ...DEFAULT_CONFIG.build },
upload: { ...DEFAULT_CONFIG.upload },
comparison: { ...DEFAULT_CONFIG.comparison },
tdd: { ...DEFAULT_CONFIG.tdd },
plugins: [...DEFAULT_CONFIG.plugins],
};
// Merge validated file config
mergeConfig(config, validatedFileConfig);
// 3. Check project mapping for current directory (if no CLI flag)
if (!cliOverrides.token) {
const currentDir = process.cwd();
const projectMapping = await getProjectMapping(currentDir);
if (projectMapping?.token) {
// Handle both string tokens and token objects (backward compatibility)
let token;
if (typeof projectMapping.token === 'string') {
token = projectMapping.token;
} else if (
typeof projectMapping.token === 'object' &&
projectMapping.token.token
) {
// Handle nested token object from old API responses
token = projectMapping.token.token;
} else {
token = String(projectMapping.token);
}
config.apiKey = token;
config.projectSlug = projectMapping.projectSlug;
config.organizationSlug = projectMapping.organizationSlug;
output.debug(
'config',
`linked to ${projectMapping.projectSlug} (${projectMapping.organizationSlug})`
);
}
}
// 4. Override with environment variables (higher priority than fallbacks)
const envApiKey = getApiToken();
const envApiUrl = getApiUrl();
const envBuildName = getBuildName();
const envParallelId = getParallelId();
if (envApiKey) {
config.apiKey = envApiKey;
output.debug('config', 'using token from environment');
}
if (envApiUrl !== 'https://app.vizzly.dev') config.apiUrl = envApiUrl;
if (envBuildName) {
config.build.name = envBuildName;
output.debug('config', 'using build name from environment');
}
if (envParallelId) config.parallelId = envParallelId;
// 5. Apply CLI overrides (highest priority)
if (cliOverrides.token) {
output.debug('config', 'using token from --token flag');
}
applyCLIOverrides(config, cliOverrides);
return config;
}
/**
* Apply CLI option overrides to config
* @param {Object} config - The config object to modify
* @param {Object} cliOverrides - CLI options to apply
*/
function applyCLIOverrides(config, cliOverrides = {}) {
// Global overrides
if (cliOverrides.token) config.apiKey = cliOverrides.token;
// Build-related overrides
if (cliOverrides.buildName) config.build.name = cliOverrides.buildName;
if (cliOverrides.environment)
config.build.environment = cliOverrides.environment;
if (cliOverrides.branch) config.build.branch = cliOverrides.branch;
if (cliOverrides.commit) config.build.commit = cliOverrides.commit;
if (cliOverrides.message) config.build.message = cliOverrides.message;
if (cliOverrides.parallelId) config.parallelId = cliOverrides.parallelId;
// Server overrides
if (cliOverrides.port) config.server.port = parseInt(cliOverrides.port, 10);
if (cliOverrides.timeout)
config.server.timeout = parseInt(cliOverrides.timeout, 10);
// Upload overrides
if (cliOverrides.batchSize !== undefined) {
config.upload.batchSize = parseInt(cliOverrides.batchSize, 10);
}
if (cliOverrides.uploadTimeout !== undefined) {
config.upload.timeout = parseInt(cliOverrides.uploadTimeout, 10);
}
// Comparison overrides
if (cliOverrides.threshold !== undefined)
config.comparison.threshold = cliOverrides.threshold;
// Baseline overrides
if (cliOverrides.baselineBuild)
config.baselineBuildId = cliOverrides.baselineBuild;
if (cliOverrides.baselineComparison)
config.baselineComparisonId = cliOverrides.baselineComparison;
// Behavior flags
if (cliOverrides.eager !== undefined) config.eager = cliOverrides.eager;
if (cliOverrides.wait !== undefined) config.wait = cliOverrides.wait;
if (cliOverrides.allowNoToken !== undefined)
config.allowNoToken = cliOverrides.allowNoToken;
}
function mergeConfig(target, source) {
for (const key in source) {
if (
source[key] &&
typeof source[key] === 'object' &&
!Array.isArray(source[key])
) {
if (!target[key]) target[key] = {};
mergeConfig(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
export function getScreenshotPaths(config) {
const screenshotsDir = config.upload?.screenshotsDir || './screenshots';
const paths = Array.isArray(screenshotsDir)
? screenshotsDir
: [screenshotsDir];
return paths.map(p => resolve(process.cwd(), p));
}