Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: separate error while running gulpfile into task not found and fail to run #262

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/versioned/^4.0.0/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 registerExports = require('../../shared/register-exports');

Expand Down Expand Up @@ -85,8 +86,13 @@ function execute(env, cfg, opts) {
}
});
} catch (err) {
log.error(chalk.red(err.message));
log.error('To list available tasks, try running: gulp --tasks');
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));
}
exit(1);
}
});
Expand Down
26 changes: 26 additions & 0 deletions lib/versioned/^4.0.0/normalize-error.js
Original file line number Diff line number Diff line change
@@ -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;
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
31 changes: 31 additions & 0 deletions test/lib/check-task-not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var expect = require('expect');
var normalizeError = require('../../lib/versioned/^4.0.0/normalize-error');

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');
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');
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');
normalizeError(err)
expect(err).not.toHaveProperty('task');
expect(err).not.toHaveProperty('similar');
done();
});
});
Loading