Files
myeasycms-v2/packages/cms/keystatic/src/create-reader.ts
giancarlo b58e9a3e7d Update KEYSTATIC paths and storage handling
The code now reads storage type from a constant instead of directly from the environment variable. It also checks if `NEXT_RUNTIME` is `nodejs` before parsing Keystatic config and core reader. The `README.md` and `keystatic.config.ts` were updated to handle a new environment variable `KEYSTATIC_CONTENT_PATH` for custom content paths.
2024-04-19 15:00:58 +08:00

50 lines
1.4 KiB
TypeScript

import { z } from 'zod';
const STORAGE_KIND = process.env.KEYSTATIC_STORAGE_KIND ?? 'local';
/**
* Create a KeyStatic reader based on the storage kind.
*/
export async function createKeystaticReader() {
switch (STORAGE_KIND) {
case 'local': {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { default: config } = await import('./keystatic.config');
const { createReader } = await import('@keystatic/core/reader');
return createReader('.', config);
} else {
// we should never get here but the compiler requires the check
// to ensure we don't parse the package at build time
throw new Error();
}
}
case 'github':
case 'cloud': {
const { default: config } = await import('./keystatic.config');
const githubConfig = z
.object({
token: z.string(),
repo: z.custom<`${string}/${string}`>(),
pathPrefix: z.string().optional(),
})
.parse({
token: process.env.KEYSTATIC_GITHUB_TOKEN,
repo: process.env.KEYSTATIC_STORAGE_REPO,
pathPrefix: process.env.KEYSTATIC_PATH_PREFIX,
});
const { createGitHubReader } = await import(
'@keystatic/core/reader/github'
);
return createGitHubReader(config, githubConfig);
}
default:
throw new Error(`Unknown storage kind`);
}
}