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` 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); }