From a9ceed7c0ec40d9c49f4bbf4b1f1cf23845b0d62 Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Wed, 20 Dec 2023 12:20:38 -0500 Subject: [PATCH 1/2] added error msg if `preprocess` is missing from the svelte config --- src/commands/init.ts | 4 +--- src/utils/add-pp.ts | 15 ++++++++++++++- src/utils/handle-error.ts | 17 ++++++++++------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index 0da3752..018bded 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -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); } diff --git a/src/utils/add-pp.ts b/src/utils/add-pp.ts index 93246da..37ac140 100644 --- a/src/utils/add-pp.ts +++ b/src/utils/add-pp.ts @@ -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; @@ -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; @@ -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' }, @@ -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); diff --git a/src/utils/handle-error.ts b/src/utils/handle-error.ts index 2e83a94..552cfb9 100644 --- a/src/utils/handle-error.ts +++ b/src/utils/handle-error.ts @@ -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); } From 9b9ffe60c61a30c791ac0383a647085618c4da42 Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Wed, 20 Dec 2023 12:21:46 -0500 Subject: [PATCH 2/2] changeset --- .changeset/moody-dogs-mate.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/moody-dogs-mate.md diff --git a/.changeset/moody-dogs-mate.md b/.changeset/moody-dogs-mate.md new file mode 100644 index 0000000..3937600 --- /dev/null +++ b/.changeset/moody-dogs-mate.md @@ -0,0 +1,5 @@ +--- +'@melt-ui/cli': patch +--- + +chore: Added an error message if the `preprocess` field is missing from `svelte.config.js`