Skip to content

Commit

Permalink
Various output improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Aug 19, 2020
1 parent b8e48c0 commit 552dffc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 21 deletions.
44 changes: 33 additions & 11 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,36 @@ const messages = require('../messages');
const env = require('process').env;
const { traverse } = require('../traverse');

const printErrors = errors => {
console.error('');
errors.forEach((error, idx) => {
console.error(
` ${`${idx + 1})`} ${chalk.cyan(error.field)} ${error.message}`
);
const printErrors = ({ code, errors }) => {
switch (code) {
case 'CONFIG_VALIDATION_ERROR':
console.error('');
errors.forEach((error, idx) => {
console.error(
` ${`${idx + 1})`} ${chalk.cyan(error.field)} ${error.message}`
);
});
console.error('');
break;

default:
console.error('');
errors.forEach((error, idx) => {
console.error(` ${`${idx + 1})`} ${error.message}`);
});
console.error('');
break;
}
};

const printDeployLog = ({ log }) => {
if (!log) return;

console.log('');
log.forEach((item, idx) => {
console.log(` ${`${idx + 1})`} ${item}`);
});
console.error('');
console.log('');
};

exports.command = 'deploy';
Expand Down Expand Up @@ -158,7 +180,7 @@ exports.handler = async args => {
log.success(
`Deployment succeeded ${chalk.gray(`(${response.data.id})`)}`
);

printDeployLog(response.data);
return;

case 401:
Expand All @@ -167,8 +189,8 @@ exports.handler = async args => {
return;

case 422:
log.error(`Deployment failed ${chalk.gray(`(${response.data.id})`)}`);
printErrors(response.data.errors);
log.error(`Deployment failed`);
printErrors(response.data);
process.exitCode = 1;
return;

Expand All @@ -181,6 +203,6 @@ exports.handler = async args => {
spinner.stop();
log.error('Deployment failed unexpectedly');
process.exitCode = 1;
throw error;
return;
}
};
32 changes: 29 additions & 3 deletions test/cmds/__snapshots__/deploy.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`displays general validation errors 1`] = `
exports[`displays general errors 1`] = `
Array [
Array [
"[31m[1m✕[22m[39m [31m[1mDeployment failed [90m(xxxx-xxxx-xxxx)[31m[22m[39m",
"[31m[1m✕[22m[39m [31m[1mDeployment failed[22m[39m",
],
Array [
"",
],
Array [
" 1) name is required",
" 1) JSON parsing error",
],
Array [
"",
],
]
`;

exports[`displays validation errors 1`] = `
Array [
Array [
"✕ Deployment failed",
],
Array [
"",
],
Array [
" 1) foo.bar.baz is required",
],
Array [
"",
Expand All @@ -22,6 +39,15 @@ Array [
Array [
"✔ Deployment succeeded (xxxx-xxxx-xxxx)",
],
Array [
"",
],
Array [
" 1) Added contact form",
],
Array [
"",
],
]
`;

Expand Down
34 changes: 27 additions & 7 deletions test/cmds/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,45 @@ it('sends a deploy request with the right params', async () => {
expect(params.key).toBe('xxx');
expect(params.userAgent).toBe(`@formspree/cli@${version}`);

return Promise.resolve({ status: 200, data: { id: 'xxxx-xxxx-xxxx' } });
return Promise.resolve({
status: 200,
data: { id: 'xxxx-xxxx-xxxx', log: ['Added contact form'] }
});
});

await cmd.handler({ config: '{}', key: 'xxx' });
expect(console.log.mock.calls).toMatchSnapshot();
});

it('displays general validation errors', async () => {
it('displays general errors', async () => {
deploy.request.mockImplementation(_params => {
return Promise.resolve({
status: 422,
data: {
code: 'CONFIG_SYNTAX_ERROR',
errors: [
{
message: 'JSON parsing error'
}
]
}
});
});

await cmd.handler({ config: '{}', key: 'xxx' });
expect(console.error.mock.calls).toMatchSnapshot();
});

it('displays validation errors', async () => {
deploy.request.mockImplementation(_params => {
return Promise.resolve({
status: 422,
data: {
id: 'xxxx-xxxx-xxxx',
code: 'CONFIG_VALIDATION_ERROR',
errors: [
{
code: 'REQUIRED',
field: 'name',
message: 'is required',
properties: {}
field: 'foo.bar.baz',
message: 'is required'
}
]
}
Expand Down

0 comments on commit 552dffc

Please sign in to comment.