From 8b7e741e5005c48ea9639c9566ba74d41d43f92c Mon Sep 17 00:00:00 2001 From: Ryan Haskell-Glatz Date: Mon, 8 Feb 2021 21:54:57 -0600 Subject: [PATCH] handle npx elm compile error messages --- src/cli/src/cli/build.ts | 17 ++++++++++++++++- src/cli/src/process.ts | 5 ++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/cli/src/cli/build.ts b/src/cli/src/cli/build.ts index 16fd4bb..d8f72a5 100644 --- a/src/cli/src/cli/build.ts +++ b/src/cli/src/cli/build.ts @@ -137,8 +137,23 @@ const compileMainElm = (env : Environment) => async () => { .catch(colorElmError) } + const red = colors.RED + const green = colors.green + const colorElmError = (err : string) => { - const errors = JSON.parse(err).errors as Error[] || [] + let errors = [] + + try { + errors = JSON.parse(err).errors as Error[] || [] + } catch (e) { + return Promise.reject([ + `${red}Something went wrong with elm-spa.${reset}`, + `Please report this entire error to ${green}https://github.com/ryannhg/elm-spa/issues${reset}`, + `-----`, + err, + `-----` + ].join('\n\n')) + } const strIf = (str : string) => (cond : boolean) : string => cond ? str : '' const boldIf = strIf(bold) diff --git a/src/cli/src/process.ts b/src/cli/src/process.ts index 3f70816..08018de 100644 --- a/src/cli/src/process.ts +++ b/src/cli/src/process.ts @@ -2,5 +2,8 @@ import { exec } from 'child_process' export const run = (cmd : string) : Promise => new Promise((resolve, reject) => - exec(cmd, (err, stdout, stderr) => err ? reject(stderr) : resolve(stdout)) + exec(cmd, (err, stdout, stderr) => err + ? reject(stderr.split('npm ERR!')[0] || stderr) + : resolve(stdout) + ) ) \ No newline at end of file