Skip to content

Commit

Permalink
chore: Adds an error message if preprocess is missing in the svelte…
Browse files Browse the repository at this point in the history
… config (#11)
  • Loading branch information
AdrianGonz97 authored Dec 20, 2023
1 parent 2fa818a commit b97e12a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-dogs-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@melt-ui/cli': patch
---

chore: Added an error message if the `preprocess` field is missing from `svelte.config.js`
4 changes: 1 addition & 3 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export const init = new Command()
await updateSvelteConfig(svelteConfigPath);
}

logger.info('');
logger.info(`${chalk.green('Success!')} MeltUI installation completed.`);
logger.info('');
logger.info(`\n${chalk.green('Success!')} MeltUI installation completed.\n`);
} catch (e) {
handleError(e);
}
Expand Down
15 changes: 14 additions & 1 deletion src/utils/add-pp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { CallExpression, ImportDeclaration } from 'estree';
import type { Node } from 'estree-walker';
import prettier from 'prettier';
import { generate } from 'astring';
import chalk from 'chalk';

type ParsedSvelteConfig = ReturnType<typeof parseSvelteConfig>;

Expand All @@ -28,6 +29,8 @@ export async function installMeltPP(config: ParsedSvelteConfig) {
// @ts-expect-error body is always there
ast.body.unshift(...createPPImports());

let ppFound = false;

const updatedSvelteConfig = walk(ast as Node, {
enter(node) {
if (node.type !== 'Property') return;
Expand All @@ -40,6 +43,8 @@ export async function installMeltPP(config: ParsedSvelteConfig) {

if (!isIdentifier && !isLiteral) return;

ppFound = true;

const ppCallExpression: CallExpression = {
type: 'CallExpression',
callee: { type: 'Identifier', name: 'preprocessMeltUI' },
Expand Down Expand Up @@ -83,8 +88,16 @@ export async function installMeltPP(config: ParsedSvelteConfig) {
},
});

if (ppFound === false) {
throw new Error(
`The ${chalk.cyan('preprocess')} field is missing in ${chalk.cyanBright(
'svelte.config.js'
)}. Add ${chalk.cyan('preprocess: []')} to the config and run again.`
);
}

if (!updatedSvelteConfig) {
throw new Error('Could not update svelte.config.js');
throw new Error(`Could not update ${chalk.cyanBright('svelte.config.js')}.`);
}

attachComments(updatedSvelteConfig, comments);
Expand Down
17 changes: 10 additions & 7 deletions src/utils/handle-error.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { logger } from './logger.js';

export function handleError(error: unknown) {
export function handleError(error: unknown): never {
const PREFIX = 'ERROR:';
logger.info('\n');

if (typeof error === 'string') {
logger.error(error);
return (process.exitCode = 1);
logger.error(`${PREFIX} ${error}`);
process.exit(1);
}

if (error instanceof Error) {
logger.error(error.message);
return (process.exitCode = 1);
logger.error(`${PREFIX} ${error.message}`);
process.exit(1);
}

logger.error('Something went wrong. Please try again.');
return (process.exitCode = 1);
logger.error(`${PREFIX} Something went wrong. Please try again.`);
process.exit(1);
}

0 comments on commit b97e12a

Please sign in to comment.