Skip to content

Commit

Permalink
fix(adapter): looks for adapter relative to git root (#327)
Browse files Browse the repository at this point in the history
instead of npm root, which may not exist

fixes #324
  • Loading branch information
Patrick McElhaney authored and LinusU committed Aug 16, 2016
1 parent 4f883d7 commit 45bdd76
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/commitizen/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs';
import findNodeModules from 'find-node-modules';
import _ from 'lodash';
import detectIndent from 'detect-indent';
import sh from 'shelljs';

import {isFunction} from '../common/util';

Expand Down Expand Up @@ -30,15 +31,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 +56,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 @@ -128,12 +129,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

// Resolve from the root of the git repo if inboundAdapterPath is a path
let absoluteAdapterPath = isPath ?
path.resolve(getNearestProjectRootDirectory(), inboundAdapterPath) :
path.resolve(getGitRootPath(), inboundAdapterPath) :
inboundAdapterPath;

try {
// try to resolve the given path
return require.resolve(absoluteAdapterPath);
Expand All @@ -142,3 +143,7 @@ function resolveAdapterPath(inboundAdapterPath) {
throw error;
}
}

function getGitRootPath() {
return sh.exec('git rev-parse --show-toplevel').output.trim();
}

0 comments on commit 45bdd76

Please sign in to comment.