Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Adds an error message if preprocess is missing in the svelte config #11

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}