From 72ede8a9c9db63715a5ff682fc7313fc5aebe6b8 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Sun, 10 Nov 2024 22:56:17 +0100 Subject: [PATCH 1/2] Extract function --- lib/remote-template.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/remote-template.js b/lib/remote-template.js index b5e760c51..e087d9c0c 100644 --- a/lib/remote-template.js +++ b/lib/remote-template.js @@ -152,8 +152,18 @@ I need this file to determine the rest of the configuration.` throw error; }); + const elmJson = parseElmJson(response.body, repoName); + return elmJson; +} + +/** + * @param {string} body + * @param {string} repoName + * @returns {ApplicationElmJson} + */ +function parseElmJson(body, repoName) { try { - const json = /** @type {unknown} */ (JSON.parse(response.body)); + const json = /** @type {unknown} */ (JSON.parse(body)); return /** @type {ApplicationElmJson} */ (json); } catch (error) { From 38453c161ca3c4273482760b7c4576203e3ced72 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Sun, 10 Nov 2024 23:28:42 +0100 Subject: [PATCH 2/2] Improve error message when --template target is a package Fixes #148 --- lib/remote-template.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/remote-template.js b/lib/remote-template.js index e087d9c0c..d9285ab64 100644 --- a/lib/remote-template.js +++ b/lib/remote-template.js @@ -2,7 +2,7 @@ * @import {OptionsOfJSONResponseBody} from 'got'; * @import {ErrorMessageInfo} from './types/error-message'; * @import {Options, Template} from './types/options'; - * @import {ApplicationElmJson} from './types/content'; + * @import {ElmJson, ApplicationElmJson} from './types/content'; * @import {Path} from './types/path'; */ const https = require('node:https'); @@ -153,19 +153,37 @@ I need this file to determine the rest of the configuration.` }); const elmJson = parseElmJson(response.body, repoName); + + if (elmJson.type === 'package') { + const referenceAsUrl = template.reference ? `/${template.reference}` : ''; + + throw new ErrorMessage.CustomError( + // prettier-ignore + 'INVALID TEMPLATE ELM.JSON TYPE', + // prettier-ignore + `I found the ${chalk.yellow('elm.json')} associated with ${chalk.yellow(repoName)} repository on GitHub, +but it is of type ${chalk.red('package')} when I need it to be of type ${chalk.yellow('application')}. + +Maybe you meant to target the ${chalk.cyan('example')} or the ${chalk.cyan('preview')} folder in that repository? + + elm-review --template ${repoName}${referenceAsUrl}/example + elm-review --template ${repoName}${referenceAsUrl}/review` + ); + } + return elmJson; } /** * @param {string} body * @param {string} repoName - * @returns {ApplicationElmJson} + * @returns {ElmJson} */ function parseElmJson(body, repoName) { try { const json = /** @type {unknown} */ (JSON.parse(body)); - return /** @type {ApplicationElmJson} */ (json); + return /** @type {ElmJson} */ (json); } catch (error) { throw new ErrorMessage.CustomError( // prettier-ignore