From 25f283e23ba5f8972fdee430633e9533b802f5f9 Mon Sep 17 00:00:00 2001 From: sttk Date: Thu, 21 Mar 2024 22:48:03 +0900 Subject: [PATCH 1/2] update: separate error while running gulpfile into `task not found` and `fail to run` --- lib/versioned/^4.0.0/index.js | 11 +++++-- .../^4.0.0/log/check-task-not-found.js | 29 +++++++++++++++++++ test/execution-errors.js | 15 ++++++++++ test/lib/check-task-not-found.js | 28 ++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 lib/versioned/^4.0.0/log/check-task-not-found.js create mode 100644 test/lib/check-task-not-found.js diff --git a/lib/versioned/^4.0.0/index.js b/lib/versioned/^4.0.0/index.js index 137ad438..4e4787f6 100644 --- a/lib/versioned/^4.0.0/index.js +++ b/lib/versioned/^4.0.0/index.js @@ -13,6 +13,7 @@ var logTasks = require('../../shared/log/tasks'); var logEvents = require('./log/events'); var logSyncTask = require('./log/sync-task'); var logTasksSimple = require('./log/tasks-simple'); +var checkTaskNotFound = require('./log/check-task-not-found'); var registerExports = require('../../shared/register-exports'); var copyTree = require('../../shared/log/copy-tree'); @@ -85,8 +86,14 @@ function execute(env, cfg, opts) { } }); } catch (err) { - log.error(chalk.red(err.message)); - log.error('To list available tasks, try running: gulp --tasks'); + var task = checkTaskNotFound(err); + if (task) { + log.error(chalk.red(err.message)); + log.error(chalk.red('To list available tasks, try running: gulp --tasks')); + } else /* istanbul ignore next */ { + log.error(chalk.red(err.message)); + log.error('Please check the documentation for proper gulpfile formatting'); + } exit(1); } }); diff --git a/lib/versioned/^4.0.0/log/check-task-not-found.js b/lib/versioned/^4.0.0/log/check-task-not-found.js new file mode 100644 index 00000000..84d1a050 --- /dev/null +++ b/lib/versioned/^4.0.0/log/check-task-not-found.js @@ -0,0 +1,29 @@ +'use strict'; + +function checkTaskNotFound(err) { + /* istanbul ignore if */ + if (!err || !err.message) { + return undefined; + } + var fixed0 = 'Task never defined: '; + var fixed1 = ' - did you mean? '; + + if (err.message.startsWith(fixed0)) { + var target = err.message.slice(fixed0.length); + var similar = undefined; + + var index = target.indexOf(fixed1); + if (index >= 0) { + similar = target.slice(index + fixed1.length).split(', '); + target = target.slice(0, index); + } + + if (similar && similar.length) { + return { target: target, similar: similar }; + } else { + return { target: target }; + } + } +} + +module.exports = checkTaskNotFound; diff --git a/test/execution-errors.js b/test/execution-errors.js index 769832a9..bd607e43 100644 --- a/test/execution-errors.js +++ b/test/execution-errors.js @@ -29,6 +29,21 @@ describe('execution error', function() { } }); + it('should output an error if a task is not defined but a similar task is found', function(done) { + var opts = { cwd: path.join(__dirname, './fixtures/gulpfiles') }; + exec(gulp('test0'), opts, cb); + + function cb(err, stdout, stderr) { + expect(err).not.toBeNull(); + expect(err.code).toEqual(1); + expect(eraseTime(stdout)).toMatch('Using gulpfile '); + expect(eraseTime(stderr)).toEqual( + 'Task never defined: test0 - did you mean? test1, test2, test3, test4, test5, test6, test7, test8\n' + + 'To list available tasks, try running: gulp --tasks\n'); + done(); + } + }); + it('should output an error if gulp version is unsupported', function(done) { var opts = { cwd: path.join(__dirname, './fixtures/errors/bad-gulp-version') }; exec(gulp(), opts, cb); diff --git a/test/lib/check-task-not-found.js b/test/lib/check-task-not-found.js new file mode 100644 index 00000000..dee1a849 --- /dev/null +++ b/test/lib/check-task-not-found.js @@ -0,0 +1,28 @@ +'use strict'; + +var expect = require('expect'); +var checkTaskNotFound = require('../../lib/versioned/^4.0.0/log/check-task-not-found'); + +describe('lib: checkTaskNotFound', function() { + + it('Should return target task and similar tasks if both are included in error message', function(done) { + var err = new Error('Task never defined: task2 - did you mean? task0, task1'); + expect(checkTaskNotFound(err)).toEqual({ + target: 'task2', + similar: ['task0', 'task1'] + }); + done(); + }); + + it('Should return only target task if similar tasks is not included in error message', function(done) { + var err = new Error('Task never defined: task2'); + expect(checkTaskNotFound(err)).toEqual({ target: 'task2' }); + done(); + }); + + it('Should return undefined if error is other', function(done) { + var err = new Error('xxx'); + expect(checkTaskNotFound(err)).toBeUndefined(); + done(); + }); +}); From b9fb4e7f78e9778010461a714847be393f2c9fb0 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Thu, 21 Mar 2024 17:48:56 -0700 Subject: [PATCH 2/2] augment the error --- lib/versioned/^4.0.0/index.js | 7 ++--- .../^4.0.0/log/check-task-not-found.js | 29 ------------------- lib/versioned/^4.0.0/normalize-error.js | 26 +++++++++++++++++ test/lib/check-task-not-found.js | 19 +++++++----- 4 files changed, 40 insertions(+), 41 deletions(-) delete mode 100644 lib/versioned/^4.0.0/log/check-task-not-found.js create mode 100644 lib/versioned/^4.0.0/normalize-error.js diff --git a/lib/versioned/^4.0.0/index.js b/lib/versioned/^4.0.0/index.js index 4e4787f6..c1888bb3 100644 --- a/lib/versioned/^4.0.0/index.js +++ b/lib/versioned/^4.0.0/index.js @@ -12,8 +12,8 @@ var tildify = require('../../shared/tildify'); var logTasks = require('../../shared/log/tasks'); var logEvents = require('./log/events'); var logSyncTask = require('./log/sync-task'); +var normalizeError = require('./normalize-error'); var logTasksSimple = require('./log/tasks-simple'); -var checkTaskNotFound = require('./log/check-task-not-found'); var registerExports = require('../../shared/register-exports'); var copyTree = require('../../shared/log/copy-tree'); @@ -86,13 +86,12 @@ function execute(env, cfg, opts) { } }); } catch (err) { - var task = checkTaskNotFound(err); - if (task) { + normalizeError(err); + if (err.task) { log.error(chalk.red(err.message)); log.error(chalk.red('To list available tasks, try running: gulp --tasks')); } else /* istanbul ignore next */ { log.error(chalk.red(err.message)); - log.error('Please check the documentation for proper gulpfile formatting'); } exit(1); } diff --git a/lib/versioned/^4.0.0/log/check-task-not-found.js b/lib/versioned/^4.0.0/log/check-task-not-found.js deleted file mode 100644 index 84d1a050..00000000 --- a/lib/versioned/^4.0.0/log/check-task-not-found.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - -function checkTaskNotFound(err) { - /* istanbul ignore if */ - if (!err || !err.message) { - return undefined; - } - var fixed0 = 'Task never defined: '; - var fixed1 = ' - did you mean? '; - - if (err.message.startsWith(fixed0)) { - var target = err.message.slice(fixed0.length); - var similar = undefined; - - var index = target.indexOf(fixed1); - if (index >= 0) { - similar = target.slice(index + fixed1.length).split(', '); - target = target.slice(0, index); - } - - if (similar && similar.length) { - return { target: target, similar: similar }; - } else { - return { target: target }; - } - } -} - -module.exports = checkTaskNotFound; diff --git a/lib/versioned/^4.0.0/normalize-error.js b/lib/versioned/^4.0.0/normalize-error.js new file mode 100644 index 00000000..0f58d0c4 --- /dev/null +++ b/lib/versioned/^4.0.0/normalize-error.js @@ -0,0 +1,26 @@ +'use strict'; + +// Normalize an undertaker v1 error like an undertaker v2 error +function normalizeError(err) { + /* istanbul ignore if */ + if (!err || !err.message) { + return; + } + + var fixed0 = 'Task never defined: '; + var fixed1 = ' - did you mean? '; + + if (err.message.startsWith(fixed0)) { + var task = err.message.slice(fixed0.length); + var index = task.indexOf(fixed1); + + if (index >= 0) { + err.similar = task.slice(index + fixed1.length).split(', '); + err.task = task.slice(0, index); + } else { + err.task = task + } + } +} + +module.exports = normalizeError; diff --git a/test/lib/check-task-not-found.js b/test/lib/check-task-not-found.js index dee1a849..d06aef95 100644 --- a/test/lib/check-task-not-found.js +++ b/test/lib/check-task-not-found.js @@ -1,28 +1,31 @@ 'use strict'; var expect = require('expect'); -var checkTaskNotFound = require('../../lib/versioned/^4.0.0/log/check-task-not-found'); +var normalizeError = require('../../lib/versioned/^4.0.0/normalize-error'); -describe('lib: checkTaskNotFound', function() { +describe('lib: normalizeError', function() { it('Should return target task and similar tasks if both are included in error message', function(done) { var err = new Error('Task never defined: task2 - did you mean? task0, task1'); - expect(checkTaskNotFound(err)).toEqual({ - target: 'task2', - similar: ['task0', 'task1'] - }); + normalizeError(err); + expect(err).toHaveProperty('task', 'task2'); + expect(err).toHaveProperty('similar', ['task0', 'task1']); done(); }); it('Should return only target task if similar tasks is not included in error message', function(done) { var err = new Error('Task never defined: task2'); - expect(checkTaskNotFound(err)).toEqual({ target: 'task2' }); + normalizeError(err) + expect(err).toHaveProperty('task', 'task2'); + expect(err).not.toHaveProperty('similar'); done(); }); it('Should return undefined if error is other', function(done) { var err = new Error('xxx'); - expect(checkTaskNotFound(err)).toBeUndefined(); + normalizeError(err) + expect(err).not.toHaveProperty('task'); + expect(err).not.toHaveProperty('similar'); done(); }); });