Skip to content

Commit

Permalink
fix(adapter): allows git-cz to run from subdirectories
Browse files Browse the repository at this point in the history
looks for a local cz adapter relative to the project root rather than the current directory

closes commitizen#187
  • Loading branch information
Patrick McElhaney authored and Patrick McElhaney committed Jul 24, 2016
1 parent ec1c686 commit 6869c9c
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/commitizen/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export {
* Must be passed an absolute path to the cli's root
*/
function addPathToAdapterConfig(sh, cliPath, repoPath, adapterNpmName) {

let commitizenAdapterConfig = {
config: {
commitizen: {
path: `./node_modules/${adapterNpmName}`
}
}
};

let packageJsonPath = path.join(getNearestProjectRootDirectory(), 'package.json');
let packageJsonString = fs.readFileSync(packageJsonPath, 'utf-8');
// tries to detect the indentation and falls back to a default if it can't
Expand All @@ -55,28 +55,28 @@ function addPathToAdapterConfig(sh, cliPath, repoPath, adapterNpmName) {
* Generates an npm install command given a map of strings and a package name
*/
function generateNpmInstallAdapterCommand(stringMappings, adapterNpmName) {

// Start with an initial npm install command
let installAdapterCommand = `npm install ${adapterNpmName}`;

// Append the neccesary arguments to it based on user preferences
for(let [key, value] of stringMappings.entries()) {
if(value) {
installAdapterCommand = installAdapterCommand + ' ' + value;
}
}

return installAdapterCommand;
}

/**
* Gets the nearest npm_modules directory
*/
function getNearestNodeModulesDirectory(options) {

// Get the nearest node_modules directories to the current working directory
let nodeModulesDirectories = findNodeModules(options);

// Make sure we find a node_modules folder
if(nodeModulesDirectories && nodeModulesDirectories.length > 0) {
return nodeModulesDirectories[0];
Expand Down Expand Up @@ -112,7 +112,7 @@ function getPrompter(adapterPath) {

// Load the adapter
let adapter = require(resolvedAdapterPath);

if(adapter && adapter.prompter && isFunction(adapter.prompter)) {
return adapter.prompter;
} else {
Expand All @@ -128,12 +128,12 @@ function resolveAdapterPath(inboundAdapterPath) {
// Check if inboundAdapterPath is a path or node module name
let parsed = path.parse(inboundAdapterPath);
let isPath = parsed.dir.length > 0;

// Resolve from process.cwd() if inboundAdapterPath is a path
let absoluteAdapterPath = isPath ?
path.resolve(process.cwd(), inboundAdapterPath) :
path.resolve(getNearestProjectRootDirectory(), inboundAdapterPath) :
inboundAdapterPath;

try {
// try to resolve the given path
return require.resolve(absoluteAdapterPath);
Expand Down

0 comments on commit 6869c9c

Please sign in to comment.