From 679094236d588c46264483bff3797b049d44c85d Mon Sep 17 00:00:00 2001 From: Arthur Fontaine <0arthur.fontaine@gmail.com> Date: Wed, 27 Aug 2025 10:34:32 +0200 Subject: [PATCH] feat(adapter resolver): resolve '~' to HOME directory --- src/commitizen/adapter.js | 13 ++++++++++++- test/tests/adapter.js | 41 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/commitizen/adapter.js b/src/commitizen/adapter.js index b3150c40..2a8f2448 100644 --- a/src/commitizen/adapter.js +++ b/src/commitizen/adapter.js @@ -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'; @@ -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. @@ -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 { diff --git a/test/tests/adapter.js b/test/tests/adapter.js index 2147f69c..13550d2e 100644 --- a/test/tests/adapter.js +++ b/test/tests/adapter.js @@ -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 @@ -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