Skip to content

Commit 45bdd76

Browse files
Patrick McElhaneyLinusU
authored andcommitted
fix(adapter): looks for adapter relative to git root (#327)
instead of npm root, which may not exist fixes #324
1 parent 4f883d7 commit 45bdd76

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/commitizen/adapter.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs';
33
import findNodeModules from 'find-node-modules';
44
import _ from 'lodash';
55
import detectIndent from 'detect-indent';
6+
import sh from 'shelljs';
67

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

@@ -30,15 +31,15 @@ export {
3031
* Must be passed an absolute path to the cli's root
3132
*/
3233
function addPathToAdapterConfig(sh, cliPath, repoPath, adapterNpmName) {
33-
34+
3435
let commitizenAdapterConfig = {
3536
config: {
3637
commitizen: {
3738
path: `./node_modules/${adapterNpmName}`
3839
}
3940
}
4041
};
41-
42+
4243
let packageJsonPath = path.join(getNearestProjectRootDirectory(), 'package.json');
4344
let packageJsonString = fs.readFileSync(packageJsonPath, 'utf-8');
4445
// tries to detect the indentation and falls back to a default if it can't
@@ -55,28 +56,28 @@ function addPathToAdapterConfig(sh, cliPath, repoPath, adapterNpmName) {
5556
* Generates an npm install command given a map of strings and a package name
5657
*/
5758
function generateNpmInstallAdapterCommand(stringMappings, adapterNpmName) {
58-
59+
5960
// Start with an initial npm install command
6061
let installAdapterCommand = `npm install ${adapterNpmName}`;
61-
62+
6263
// Append the neccesary arguments to it based on user preferences
6364
for(let [key, value] of stringMappings.entries()) {
6465
if(value) {
6566
installAdapterCommand = installAdapterCommand + ' ' + value;
6667
}
6768
}
68-
69+
6970
return installAdapterCommand;
7071
}
7172

7273
/**
7374
* Gets the nearest npm_modules directory
7475
*/
7576
function getNearestNodeModulesDirectory(options) {
76-
77+
7778
// Get the nearest node_modules directories to the current working directory
7879
let nodeModulesDirectories = findNodeModules(options);
79-
80+
8081
// Make sure we find a node_modules folder
8182
if(nodeModulesDirectories && nodeModulesDirectories.length > 0) {
8283
return nodeModulesDirectories[0];
@@ -128,12 +129,12 @@ function resolveAdapterPath(inboundAdapterPath) {
128129
// Check if inboundAdapterPath is a path or node module name
129130
let parsed = path.parse(inboundAdapterPath);
130131
let isPath = parsed.dir.length > 0;
131-
132-
// Resolve from process.cwd() if inboundAdapterPath is a path
132+
133+
// Resolve from the root of the git repo if inboundAdapterPath is a path
133134
let absoluteAdapterPath = isPath ?
134-
path.resolve(getNearestProjectRootDirectory(), inboundAdapterPath) :
135+
path.resolve(getGitRootPath(), inboundAdapterPath) :
135136
inboundAdapterPath;
136-
137+
137138
try {
138139
// try to resolve the given path
139140
return require.resolve(absoluteAdapterPath);
@@ -142,3 +143,7 @@ function resolveAdapterPath(inboundAdapterPath) {
142143
throw error;
143144
}
144145
}
146+
147+
function getGitRootPath() {
148+
return sh.exec('git rev-parse --show-toplevel').output.trim();
149+
}

0 commit comments

Comments
 (0)