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

adding --timeout option #339

Merged
2 commits merged into from
Apr 17, 2017
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Do not use these unless you know what you are doing! Not needed for typical usag
-a, --upgradeAll include even those dependencies whose latest
version satisfies the declared semver dependency
--removeRange remove version ranges from the final package version
--timeout a global timeout in ms

Integration
--------------
Expand Down
1 change: 1 addition & 0 deletions bin/ncu
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ program
.option('-r, --registry <url>', 'specify third-party npm registry')
.option('-s, --silent', "don't output anything (--loglevel silent)")
.option('-t, --greatest', 'find the highest versions available instead of the latest stable versions')
.option('--timeout <seconds>', 'a global timeout in ms')
.option('-u, --upgrade', 'overwrite package file')
.option('-x, --reject <matches>', 'exclude packages matching the given string, comma-delimited list, or regex')
.option('-a, --upgradeAll', 'include even those dependencies whose latest version satisfies the declared semver dependency')
Expand Down
17 changes: 16 additions & 1 deletion lib/npm-check-updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,24 @@ function run(opts) {

options = initOptions(options);

return options.global ?
if (options.timeout) {
var timeoutMs = _.isString(options.timeout) ? parseInt(options.timeout, 10) : options.timeout;
var timeout = setTimeout(function () {
programError(options, chalk.red('Exceeded global timeout of ' + timeoutMs + 'ms'));
}, timeoutMs);
}

var pendingAnalysis = options.global ?
analyzeGlobalPackages(options) :
findPackage(options).spread(_.partial(analyzeProjectDependencies, options));

if (timeout) {
pendingAnalysis = pendingAnalysis.then(function () {
clearTimeout(timeout);
});
}

return pendingAnalysis;
});
}

Expand Down
17 changes: 17 additions & 0 deletions test/test-ncu.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,23 @@ describe('npm-check-updates', function () {
output.trim().should.equal('');
});
});

describe('with timeout option', function () {

it('should exit with error when timeout exceeded', function (done) {
spawn('node', ['bin/ncu', '--timeout', '1'], '{ "dependencies": { "express": "1" } }')
.then(function () {
done(new Error('should not resolve'));
}).catch(function (stderr) {
stderr.should.contain('Exceeded global timeout of 1ms');
done();
});
});

it('completes successfully with timeout', function () {
return spawn('node', ['bin/ncu', '--timeout', '100000'], '{ "dependencies": { "express": "1" } }');
});
});
});

});