Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(adapter): looks for adapter relative to git root #327

Merged
merged 1 commit into from
Aug 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}