Skip to content

Commit

Permalink
update: separate error while running gulpfile into task not found a…
Browse files Browse the repository at this point in the history
…nd `fail to run`
  • Loading branch information
sttk committed Mar 21, 2024
1 parent e5c7983 commit 25f283e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/versioned/^4.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
});
Expand Down
29 changes: 29 additions & 0 deletions lib/versioned/^4.0.0/log/check-task-not-found.js
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 15 additions & 0 deletions test/execution-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions test/lib/check-task-not-found.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 25f283e

Please sign in to comment.