Skip to content

Commit 25f283e

Browse files
committed
update: separate error while running gulpfile into task not found and fail to run
1 parent e5c7983 commit 25f283e

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

lib/versioned/^4.0.0/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var logTasks = require('../../shared/log/tasks');
1313
var logEvents = require('./log/events');
1414
var logSyncTask = require('./log/sync-task');
1515
var logTasksSimple = require('./log/tasks-simple');
16+
var checkTaskNotFound = require('./log/check-task-not-found');
1617
var registerExports = require('../../shared/register-exports');
1718

1819
var copyTree = require('../../shared/log/copy-tree');
@@ -85,8 +86,14 @@ function execute(env, cfg, opts) {
8586
}
8687
});
8788
} catch (err) {
88-
log.error(chalk.red(err.message));
89-
log.error('To list available tasks, try running: gulp --tasks');
89+
var task = checkTaskNotFound(err);
90+
if (task) {
91+
log.error(chalk.red(err.message));
92+
log.error(chalk.red('To list available tasks, try running: gulp --tasks'));
93+
} else /* istanbul ignore next */ {
94+
log.error(chalk.red(err.message));
95+
log.error('Please check the documentation for proper gulpfile formatting');
96+
}
9097
exit(1);
9198
}
9299
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
function checkTaskNotFound(err) {
4+
/* istanbul ignore if */
5+
if (!err || !err.message) {
6+
return undefined;
7+
}
8+
var fixed0 = 'Task never defined: ';
9+
var fixed1 = ' - did you mean? ';
10+
11+
if (err.message.startsWith(fixed0)) {
12+
var target = err.message.slice(fixed0.length);
13+
var similar = undefined;
14+
15+
var index = target.indexOf(fixed1);
16+
if (index >= 0) {
17+
similar = target.slice(index + fixed1.length).split(', ');
18+
target = target.slice(0, index);
19+
}
20+
21+
if (similar && similar.length) {
22+
return { target: target, similar: similar };
23+
} else {
24+
return { target: target };
25+
}
26+
}
27+
}
28+
29+
module.exports = checkTaskNotFound;

test/execution-errors.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ describe('execution error', function() {
2929
}
3030
});
3131

32+
it('should output an error if a task is not defined but a similar task is found', function(done) {
33+
var opts = { cwd: path.join(__dirname, './fixtures/gulpfiles') };
34+
exec(gulp('test0'), opts, cb);
35+
36+
function cb(err, stdout, stderr) {
37+
expect(err).not.toBeNull();
38+
expect(err.code).toEqual(1);
39+
expect(eraseTime(stdout)).toMatch('Using gulpfile ');
40+
expect(eraseTime(stderr)).toEqual(
41+
'Task never defined: test0 - did you mean? test1, test2, test3, test4, test5, test6, test7, test8\n' +
42+
'To list available tasks, try running: gulp --tasks\n');
43+
done();
44+
}
45+
});
46+
3247
it('should output an error if gulp version is unsupported', function(done) {
3348
var opts = { cwd: path.join(__dirname, './fixtures/errors/bad-gulp-version') };
3449
exec(gulp(), opts, cb);

test/lib/check-task-not-found.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
var expect = require('expect');
4+
var checkTaskNotFound = require('../../lib/versioned/^4.0.0/log/check-task-not-found');
5+
6+
describe('lib: checkTaskNotFound', function() {
7+
8+
it('Should return target task and similar tasks if both are included in error message', function(done) {
9+
var err = new Error('Task never defined: task2 - did you mean? task0, task1');
10+
expect(checkTaskNotFound(err)).toEqual({
11+
target: 'task2',
12+
similar: ['task0', 'task1']
13+
});
14+
done();
15+
});
16+
17+
it('Should return only target task if similar tasks is not included in error message', function(done) {
18+
var err = new Error('Task never defined: task2');
19+
expect(checkTaskNotFound(err)).toEqual({ target: 'task2' });
20+
done();
21+
});
22+
23+
it('Should return undefined if error is other', function(done) {
24+
var err = new Error('xxx');
25+
expect(checkTaskNotFound(err)).toBeUndefined();
26+
done();
27+
});
28+
});

0 commit comments

Comments
 (0)