Skip to content

Commit

Permalink
fix(config): handle missing config better, close #111
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Aug 1, 2017
1 parent fc06129 commit b49a5a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/pre-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const log = require('debug')(packageName);
var Promise = require('bluebird');

var label = 'pre-commit:';
// magic value, meaning we found package.json but it has no "config" object
const MISSING_GIT_CONFIG = 'MISSING_GIT_CONFIG';

var gitPrefix = process.env.GIT_PREFIX || '';
log('git prefix env', process.env.GIT_PREFIX);
Expand Down Expand Up @@ -67,7 +69,7 @@ function findPackage(dir) {
if (hasPreGitInFile(filename)) {
log('found pre-git dependency in %s', filename);
log('but no pre-git config');
return;
return MISSING_GIT_CONFIG;
}
}

Expand All @@ -84,6 +86,9 @@ function findPackage(dir) {
function getPackage() {
var filename = findPackage();
la(check.unemptyString(filename), 'could not find package');
if (filename === MISSING_GIT_CONFIG) {
return MISSING_GIT_CONFIG;
}
var pkg = require(filename);
return pkg;
}
Expand Down Expand Up @@ -180,6 +185,9 @@ function failure(label, err) {

function getConfig() {
const pkg = getPackage();
if (pkg === MISSING_GIT_CONFIG) {
return;
}
return pkg.config && pkg.config[packageName];
}

Expand Down Expand Up @@ -212,14 +220,14 @@ function hasEnabledOption(config) {

function getTasks(label) {
log('getting tasks with label "%s"', label);
var pkg = getPackage();
la(check.object(pkg), 'missing package', pkg);

const config = getConfig();
if (!config) {
return;
}

var pkg = getPackage();
la(check.object(pkg), 'missing package', pkg);

if (hasEnabledOption(config) && !config.enabled) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions test/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ echo "Installing git hooks"
# or we can install current dev source
npm install $preGitFolder
echo "Package.json after installing pre-git"

# add pre-commit sample command
sed 's/"pre-commit": \[\]/"pre-commit": \["DEBUG=pre-git echo pre-commit testing..."\]/g' package.json > package.out
mv package.out package.json
cat package.json

# let us commit the code
Expand Down

0 comments on commit b49a5a3

Please sign in to comment.