Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
fix(updateContributors): fix handling of unspecified package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Dec 11, 2018
1 parent 510af0c commit 2239d4f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 14 deletions.
82 changes: 74 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"dependencies": {
"log-symbols": "^2.2.0",
"parse-author": "^2.0.0",
"read-pkg-up": "^4.0.0",
"pkg-up": "^2.0.0",
"write-pkg": "^3.2.0",
"yargs": "^12.0.5"
},
Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const childProcess = require('child_process');
const symbols = require('log-symbols');
const readPkgUp = require('read-pkg-up');
const pkgUp = require('pkg-up');
const {dirname} = require('path');
const writePkg = require('write-pkg');
const parseAuthor = require('parse-author');
Expand Down Expand Up @@ -48,7 +48,11 @@ exports.getContributors = ({
localeOpts = DEFAULT_LOCALE_OPTS
} = {}) => {
const output = childProcess.execSync('git log --format="%aN <%aE>"', {cwd});
const uniqueContributors = new Set(output.trim().split(/\r?\n/));
const uniqueContributors = new Set(
String(output)
.trim()
.split(/\r?\n/)
);
return Array.from(uniqueContributors)
.filter(contributor => !new Set(exclude).has(contributor))
.sort((a, b) => a.localeCompare(b, locale, localeOpts));
Expand All @@ -70,7 +74,11 @@ exports.updateContributors = ({
property = 'contributors'
} = {}) => {
const cwd = pkg ? dirname(pkg) : process.cwd();
const pkgJson = readPkgUp.sync(cwd);
pkg = pkg || pkgUp.sync(cwd);
if (!pkg) {
throw new Error(`Cannot find a package.json from ${cwd}`);
}
const pkgJson = JSON.parse(fs.readFileSync(pkg, 'utf8'));
const currentCount = Array.isArray(pkgJson[property])
? pkgJson[property].length
: 0;
Expand Down
25 changes: 23 additions & 2 deletions test/contributors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const sinon = require('sinon');
const childProcess = require('child_process');
const {readFileSync} = require('fs');
const contributors = require('..');
const readPkgUp = require('read-pkg-up');
const pkgUp = require('pkg-up');
const writePkg = require('write-pkg');
const fs = require('fs');

Expand Down Expand Up @@ -53,12 +53,13 @@ describe('contributors', function() {
processedContributors = expectedContributors.filter(
contributor => !/boneskull/.test(contributor)
);
sandbox.stub(readPkgUp, 'sync').returns(pkgJson);
sandbox.stub(pkgUp, 'sync').returns('/some/path/to/package.json');
sandbox.stub(writePkg, 'sync');
sandbox
.stub(contributors, 'getContributors')
.returns(processedContributors);
sandbox.stub(fs, 'readFileSync').returns(JSON.stringify(pkgJson));
sandbox.stub(process, 'cwd').returns('/some/dir');
});

it('should filter out author by default', function() {
Expand Down Expand Up @@ -88,5 +89,25 @@ describe('contributors', function() {
'to be a string'
);
});

it('should attempt to find a package.json if none given', function() {
contributors.updateContributors();
expect(pkgUp.sync, 'to have a call satisfying', ['/some/dir']);
});

it('should throw when package.json cannot be found', function() {
pkgUp.sync.restore();
sandbox.stub(pkgUp, 'sync').returns(null);
expect(
() => contributors.updateContributors(),
'to throw',
/cannot find/i
);
});

it('should throw when package.json cannot be read', function() {
sandbox.stub(JSON, 'parse').throws();
expect(() => contributors.updateContributors(), 'to throw');
});
});
});

0 comments on commit 2239d4f

Please sign in to comment.