Skip to content

Commit

Permalink
Improve error message when --template target is a package
Browse files Browse the repository at this point in the history
Fixes #148
  • Loading branch information
jfmengels committed Nov 10, 2024
1 parent 72ede8a commit 38453c1
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/remote-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 38453c1

Please sign in to comment.