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

Improve error message when --template target is a package #306

Merged
merged 2 commits into from
Nov 10, 2024
Merged
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
34 changes: 31 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 @@ -152,10 +152,38 @@ I need this file to determine the rest of the configuration.`
throw error;
});

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 {ElmJson}
*/
function parseElmJson(body, repoName) {
try {
const json = /** @type {unknown} */ (JSON.parse(response.body));
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
Loading