Skip to content

Commit

Permalink
fix(cli): handle exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidenke committed Oct 22, 2024
1 parent ad56cec commit 059b83f
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import { loadDecapConfig } from './utils/decap.utils.js';
import { formatCode } from './utils/format.utils.js';
import { transformCollection } from './utils/transform.utils.js';

enum ERRORS {
enum ERROR {
MISSING_CONFIG = 'Missing required argument: --config. Please provide a path to the Decap config.yml file.',
MISSING_TARGET = 'Missing required argument: --target. Please provide a path where the collections will be stored.',
ZOD_MISSING = 'Zod is required for schema validation. Please install it by running `npm install zod`.',
PARSING_FAILED = 'Failed to parse the Decap config file.',
FORMATTING_FAILED = 'Failed to format the generated schema.',
WRITING_FAILED = 'Failed to write the generated schema to the target folder.',
}

// parse cli arguments
Expand All @@ -29,13 +33,26 @@ function fail(message: string, exitCode = 1) {
exit(exitCode);
}

function tryOrFail<T>(fn: () => T, error: ERROR, exitCode: false | number = 1): T {
try {
return fn();
} catch (_) {
fail(error);
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
exitCode && exit(1);
return undefined as never;
}
}

// read config and transform collections
export async function loadAndTransformCollections(from?: string, to?: string, isUpdate = false) {
if (!from) return fail(ERRORS.MISSING_CONFIG);
if (!to) return fail(ERRORS.MISSING_TARGET);
if (!from) return fail(ERROR.MISSING_CONFIG);
if (!to) return fail(ERROR.MISSING_TARGET);

const zod = await tryOrFail(() => import('zod'), ERROR.ZOD_MISSING);
const config = await tryOrFail(() => loadDecapConfig(from), ERROR.PARSING_FAILED);
const { collections = [] } = config ?? {};

const zod = await import('zod');
const { collections = [] } = (await loadDecapConfig(from)) ?? {};
await Promise.all(
collections.map(async collection => {
// transform collection
Expand All @@ -44,12 +61,12 @@ export async function loadAndTransformCollections(from?: string, to?: string, is

// build content and prettify if possible
const raw = `import { z } from 'astro:content';\n\nexport const schema = ${cptime};\n`;
const pretty = await formatCode(raw, 'typescript');
const pretty = await tryOrFail(() => formatCode(raw, 'typescript'), ERROR.FORMATTING_FAILED);

// prepare folder if non-existent, remove existing and write file
if (!existsSync(to)) await mkdir(to, { recursive: true });
if (existsSync(path)) await rm(path);
await writeFile(path, pretty, 'utf-8');
await tryOrFail(() => writeFile(path, pretty, 'utf-8'), ERROR.WRITING_FAILED);

// inform user
const action = isUpdate ? 'updated at' : 'written to';
Expand Down

0 comments on commit 059b83f

Please sign in to comment.