Skip to content

Commit

Permalink
refactor config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskalmar committed Nov 18, 2023
1 parent 15b4465 commit 77a3bb1
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,40 +467,51 @@ export type CustomProjectConfig = Config;

export const MEDIA_UPLOAD_CONCURRENCY = 10;

export let config: FullConfig;
export let config: Config;

const checkConfig = () => {
const missingProps: string[] = [];

for (const prop of requiredConfigProps) {
if (!get(config, prop)) {
missingProps.push(prop);
}
export const detectConfigMode = (userConfig: Config) => {
if ('apiKey' in userConfig || 'lostPixelProjectId' in userConfig) {
return 'platform';
}

if (missingProps.length > 0) {
return 'generateOnly';
};

const printConfigErrors = (error: z.ZodError) => {
for (const issue of error.issues) {
log.process(
'error',
'config',
`Error: Missing required config properties: ${missingProps.join(', ')}`,
[
'Configuration error:',
` - Path: ${issue.path.join('.')}`,
` - Message: ${issue.message}`,
].join('\n'),
);
process.exit(1);
}
};

if (
config.customShots?.currentShotsPath &&
path.relative(
path.resolve(config.imagePathCurrent),
path.resolve(config.customShots.currentShotsPath),
) === ''
) {
log.process(
'error',
'config',
`Error: 'customShots.currentShotsPath' cannot be equal to 'imagePathCurrent'`,
);
process.exit(1);
export const checkConfig = (userConfig: Config) => {
if (detectConfigMode(userConfig) === 'platform') {
const platformCheck = PlatformModeConfigSchema.safeParse(userConfig);

if (platformCheck.success) {
return;
}

printConfigErrors(platformCheck.error);
} else {
const generateOnlyCheck =
GenerateOnlyModeConfigSchema.safeParse(userConfig);

if (generateOnlyCheck.success) {
return;
}

printConfigErrors(generateOnlyCheck.error);
}

process.exit(1);
};

const configDirBase = process.env.LOST_PIXEL_CONFIG_DIR ?? process.cwd();
Expand Down

0 comments on commit 77a3bb1

Please sign in to comment.