Skip to content

Commit

Permalink
feat(config): walk up the folders, until find package.json with config,
Browse files Browse the repository at this point in the history
close #106
  • Loading branch information
bahmutov committed Jun 2, 2017
1 parent e172298 commit a9dfc52
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ them to the `config > pre-git` object manually.
Related project: [post-merge-make](https://github.com/bahmutov/post-merge-make)
runs `make post-merge` after pull or merge.

### Subprojects

If you have large repo, it is possible that there might be nested files with
different `package.json` files. In this case, the search will proceed up
from the current working directory until it finds `package.json` with valis
`config.pre-git` object inside.

## Windows

Thanks to [ybiquitous](https://github.com/ybiquitous) for
Expand Down Expand Up @@ -243,7 +250,7 @@ You can verify the git hooks are not running when the `pre-git` is disabled
via a config option by running `npm run e2e-pre-git-disabled` which allows
the commit to go through.

To see how `allow-untracked-files` option lets the commit go through,
To see how `allow-untracked-files` option lets the commit go through,
run `npm run test-allow-untracked-files`

## Debugging
Expand Down
24 changes: 22 additions & 2 deletions src/pre-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var Promise = require('bluebird');
var label = 'pre-commit:';

var gitPrefix = process.env.GIT_PREFIX || '';
log('git prefix env', process.env.GIT_PREFIX);

function isAtRoot(dir) {
return dir === '/';
Expand All @@ -37,15 +38,21 @@ function verifyValidDirectory(dir) {
}
}

// finds package.json with config by going up the folder chain
function findPackage(dir) {
var cwd = process.cwd();
if (! dir) {
dir = path.join(cwd, gitPrefix);
log('set dir to %s for cwd %s and git prefix %s', dir, cwd, gitPrefix);
}

if (isPackageAmongFiles(dir)) {
log('found package in folder', dir);
return path.join(dir, 'package.json');
const filename = path.join(dir, 'package.json');
log('found package file %s', filename);
if (hasConfigInFile(filename)) {
log('file %s has %s config', filename, packageName);
return filename;
}
}

verifyValidDirectory(dir);
Expand Down Expand Up @@ -162,6 +169,11 @@ function hasConfig(pkg) {
return Boolean(pkg && pkg.config && pkg.config[packageName]);
}

function hasConfigInFile(filename) {
const pkg = require(filename);
return hasConfig(pkg);
}

function getConfigProperty(propertyName) {
const config = getConfig();
if (!config) {
Expand Down Expand Up @@ -305,11 +317,19 @@ function runAtRoot(root, label) {
return Promise.each(tasks, runTaskAt);
}

if (root !== process.cwd()) {
log('switching current folder from %s to %s',
process.cwd(), root);
} else {
log('cwd %s', process.cwd());
}

if (label === 'pre-commit') {
return hasUntrackedFiles()
.then(noUntrackedFiles)
.then(runTasksForLabel);
}

return runTasksForLabel();
}

Expand Down

0 comments on commit a9dfc52

Please sign in to comment.