From 5f9f0add4514240258e553dbfb95cad42016b8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Poullain?= Date: Wed, 6 Mar 2024 21:43:34 +0100 Subject: [PATCH] [CLI] Improve logging of script arguments validation errors --- .../4-the-shell-script-create-todo.md | 2 +- .../4-the-shell-script-create-todo.md | 2 +- .../4-the-shell-script-create-todo.md | 2 +- .../4-the-shell-script-create-todo.md | 2 +- packages/cli/src/run-script/run-script.spec.ts | 18 ++++++++++++------ packages/cli/src/run-script/run-script.ts | 8 ++++++-- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/docs/docs/tutorials/simple-todo-list/4-the-shell-script-create-todo.md b/docs/docs/tutorials/simple-todo-list/4-the-shell-script-create-todo.md index e2386be160..09d6270f42 100644 --- a/docs/docs/tutorials/simple-todo-list/4-the-shell-script-create-todo.md +++ b/docs/docs/tutorials/simple-todo-list/4-the-shell-script-create-todo.md @@ -71,4 +71,4 @@ foal run create-todo text="Write tests" > Note that if you try to create a new to-do without specifying the text argument, you'll get the error below. > -> `Error: The command line arguments should have required property 'text'.` +> `Script error: arguments must have required property 'text'.` diff --git a/docs/i18n/es/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md b/docs/i18n/es/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md index 84b6db76ae..55ab610b1f 100644 --- a/docs/i18n/es/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md +++ b/docs/i18n/es/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md @@ -71,4 +71,4 @@ foal run create-todo text="Write tests" > Observe que si intenta crear una nueva tarea sin especificar el argumento del texto, obtendrá el siguiente error. > -> `Error: The command line arguments should have required property 'text'.` +> `Script error: arguments must have required property 'text'.` diff --git a/docs/i18n/fr/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md b/docs/i18n/fr/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md index 64317badc3..f9dbfabefe 100644 --- a/docs/i18n/fr/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md +++ b/docs/i18n/fr/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md @@ -71,4 +71,4 @@ foal run create-todo text="Write tests" > Notez que si vous essayez de créer une nouvelle tâche sans spécifier l'argument texte, vous obtiendrez l'erreur ci-dessous. > -> `Error: The command line arguments should have required property 'text'.` +> `Script error: arguments must have required property 'text'.` diff --git a/docs/i18n/id/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md b/docs/i18n/id/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md index e2386be160..09d6270f42 100644 --- a/docs/i18n/id/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md +++ b/docs/i18n/id/docusaurus-plugin-content-docs/current/tutorials/simple-todo-list/4-the-shell-script-create-todo.md @@ -71,4 +71,4 @@ foal run create-todo text="Write tests" > Note that if you try to create a new to-do without specifying the text argument, you'll get the error below. > -> `Error: The command line arguments should have required property 'text'.` +> `Script error: arguments must have required property 'text'.` diff --git a/packages/cli/src/run-script/run-script.spec.ts b/packages/cli/src/run-script/run-script.spec.ts index 9a595207a6..282e3c76e6 100644 --- a/packages/cli/src/run-script/run-script.spec.ts +++ b/packages/cli/src/run-script/run-script.spec.ts @@ -67,14 +67,14 @@ describe('runScript', () => { it('should validate the process arguments with the schema if it is given.', async () => { mkdirIfDoesNotExist('build/scripts'); const scriptContent = `const { writeFileSync } = require('fs'); - module.exports.schema = { type: 'object', properties: { email: { type: 'string', format: 'email' } } }; + module.exports.schema = { type: 'object', properties: { email: { type: 'string', format: 'email', maxLength: 2 }, password: { type: 'string' }, n: { type: 'number', maximum: 10 } }, required: ['password'] }; module.exports.main = function main(args) { writeFileSync('my-script-temp', JSON.stringify(args), 'utf8'); }`; writeFileSync('build/scripts/my-script.js', scriptContent, 'utf8'); - let msg; - const log = (message: any) => msg = message; + const msgs: string[] = []; + const log = (message: any) => msgs.push(message); delete require.cache[join(process.cwd(), `./build/scripts/my-script.js`)]; @@ -84,11 +84,17 @@ describe('runScript', () => { 'run-script', 'my-script', 'email=bar', + 'n=11' ], log); - strictEqual( - msg, - 'Error: The command line arguments must match format "email".' + deepStrictEqual( + msgs, + [ + 'Script error: arguments must have required property \'password\'.', + 'Script error: the value of "email" must NOT have more than 2 characters.', + 'Script error: the value of "email" must match format "email".', + 'Script error: the value of "n" must be <= 10.', + ] ); }); diff --git a/packages/cli/src/run-script/run-script.ts b/packages/cli/src/run-script/run-script.ts index ec481be9b1..ae396ecd3f 100644 --- a/packages/cli/src/run-script/run-script.ts +++ b/packages/cli/src/run-script/run-script.ts @@ -32,11 +32,15 @@ export async function runScript({ name }: { name: string }, argv: string[], log const args = getCommandLineArguments(argv); if (schema) { - const ajv = new Ajv({ useDefaults: true }); + const ajv = new Ajv({ useDefaults: true, allErrors: true }); addFormats(ajv); if (!ajv.validate(schema, args)) { ajv.errors!.forEach(err => { - log(`Error: The command line arguments ${err.message}.`); + if (err.instancePath) { + log(`Script error: the value of "${err.instancePath.split('/')[1]}" ${err.message}.`); + } else { + log(`Script error: arguments ${err.message}.`); + } }); return; }