Skip to content

Commit

Permalink
fallback to package.json when stdin closed with no content
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Bennett authored and raineorshine committed Feb 17, 2017
1 parent f60019f commit ba355cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@ npm-debug.log
node_modules

.idea
*.iml
46 changes: 26 additions & 20 deletions lib/npm-check-updates.js
Expand Up @@ -378,6 +378,7 @@ function programRunLocal() {
pkgData = Promise.resolve(options.packageData);
} else if (options.packageFile) {
pkgFile = options.packageFile;
pkgData = getPackageDataFromFile(pkgFile, pkgFileName);
} else if (!process.stdin.isTTY) {
print('Waiting for package data on stdin...', 'verbose');

Expand All @@ -387,37 +388,42 @@ function programRunLocal() {
console.log(stdinWarningMessage);
}, stdinWarningTime);

pkgData = getstdin();

// clear the warning timer once stdin returns
pkgData.then(function () {
// clear the warning timer once stdin returns and fallback to scanning pwd if no content from stdin
pkgData = getstdin().then(function (_pkgData) {
clearTimeout(stdinTimer);

var isEmptyStdin = _pkgData.length === 0 || (_pkgData.length === 1 && _pkgData.charCodeAt(0) === 10);
// if no stdin content fall back to searching for package.json from pwd and up to root
if (isEmptyStdin) {
pkgFile = findUp.sync(pkgFileName);
return getPackageDataFromFile(pkgFile, pkgFileName);
} else {
return _pkgData;
}
});
} else {
// find the closest package starting from the current working directory and going up to the root
pkgFile = findUp.sync(pkgFileName);
pkgData = getPackageDataFromFile(pkgFile, pkgFileName);
}

// if pkgFile was set, make sure it exists and read it into pkgData
if (pkgFile) {
// print a message if we are using a descendant package file
var relPathToPackage = path.resolve(pkgFile);
if (relPathToPackage !== pkgFileName) {
print('Using ' + relPathToPackage);
}
if (!fs.existsSync(pkgFile)) {
programError(chalk.red(relPathToPackage + ' not found'));
}
return pkgData.then(function (_pkgData) {
print('wtf', _pkgData);
return analyzeProjectDependencies(_pkgData, pkgFile);
});
}

pkgData = readPackageFile(pkgFile, null, false);
function getPackageDataFromFile(pkgFile, pkgFileName) {
// print a message if we are using a descendant package file
var relPathToPackage = path.resolve(pkgFile);
if (relPathToPackage !== pkgFileName) {
print('Using ' + relPathToPackage);
}

// no package data!
if (!pkgData) {
programError(chalk.red('No ' + pkgFileName) + '\n\nPlease add a ' + pkgFileName + ' to the current directory, specify the ' + chalk.blue('--packageFile') + ' or ' + chalk.blue('--packageData') + ' options, or pipe a ' + pkgFileName + ' to stdin.');
if (!fs.existsSync(pkgFile)) {
programError(chalk.red(relPathToPackage + ' not found'));
}

return pkgData.then(_.partial(analyzeProjectDependencies, _, pkgFile));
return readPackageFile(pkgFile);
}

module.exports = _.merge({
Expand Down
12 changes: 12 additions & 0 deletions test/test-ncu.js
Expand Up @@ -3,6 +3,7 @@ var chai = require("chai");
var fs = require('fs');
var spawn = require('spawn-please')
var BluebirdPromise = require('bluebird')
var child = require('child_process')

chai.use(require("chai-as-promised"));
chai.use(require('chai-string'))
Expand Down Expand Up @@ -93,6 +94,17 @@ describe('npm-check-updates', function () {
});
});

it('should fall back to package.json search when receiving empty content on stdin', function (done) {
var childProcess = child.exec('node bin/ncu', function (error, stdout) {
if (error) {
throw new Error(error);
}
stdout.toString().trim().should.match(/^Using .+package.json/);
done();
});
childProcess.stdin.end();
});

it('should output json with --jsonAll', function() {
return spawn('node', ['bin/ncu', '--jsonAll'], '{ "dependencies": { "express": "1" } }')
.then(JSON.parse)
Expand Down

0 comments on commit ba355cb

Please sign in to comment.