Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion src/commitizen/adapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import childProcess from 'child_process';
import path from 'path';
import fs from 'fs';
import os from 'os';
import findNodeModules from 'find-node-modules';
import _ from 'lodash';
import detectIndent from 'detect-indent';
Expand Down Expand Up @@ -151,6 +152,16 @@ function getPrompter (adapterPath) {
}
}

/**
* Expands a path that starts with ~ to the user's home directory
*/
function expandHome(p) {
if (p.startsWith("~/")) {
return path.join(os.homedir(), p.slice(1));
}
return p;
}

/**
* Given a resolvable module name or path, which can be a directory or file, will
* return a located adapter path or will throw.
Expand All @@ -162,7 +173,7 @@ function resolveAdapterPath (inboundAdapterPath) {

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

try {
Expand Down
41 changes: 40 additions & 1 deletion test/tests/adapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai';
import { assert, expect } from 'chai';
import path from 'path';

// TODO: augment these tests with tests using the actual cli call
Expand Down Expand Up @@ -103,6 +103,45 @@ describe('adapter', function () {
expect(function () { adapter.resolveAdapterPath(path.join(adapterConfig.path, 'index.js')); }).not.to.throw(Error);
});

it('resolves adapter path started with ~', function () {

this.timeout(config.maxTimeout); // this could take a while

// SETUP

// Describe a repo and some files to add and commit
let repoConfig = {
path: config.paths.endUserRepo,
files: {
dummyfile: {
contents: `duck-duck-goose`,
filename: `mydummyfile.txt`,
},
gitignore: {
contents: `node_modules/`,
filename: `.gitignore`
}
}
};

// Describe an adapter
let adapterConfig = {
path: path.join(repoConfig.path, '/node_modules/@commitizen/cz-conventional-changelog'),
npmName: '@commitizen/cz-conventional-changelog'
};
adapterConfig.path = adapterConfig.path.replace(process.env.HOME, '~');
assert(adapterConfig.path.startsWith('~'), 'adapterConfig.path should start with ~');

// TEST

expect(function () { adapter.resolveAdapterPath('~/non/existent/path'); }).to.throw(Error);
expect(function () { adapter.resolveAdapterPath(adapterConfig.path); }).not.to.throw(Error);

expect(adapter.resolveAdapterPath(adapterConfig.path)).to.equal(
path.join(repoConfig.path, '/node_modules/@commitizen/cz-conventional-changelog/index.js')
);
});

it.skip('gets adapter prompter functions', function () {

this.timeout(config.maxTimeout); // this could take a while
Expand Down
Loading