From 82f955ec7c18b7583fd0db8ca2ef670143087251 Mon Sep 17 00:00:00 2001 From: Colin Rymer Date: Tue, 29 Dec 2015 23:16:35 -0500 Subject: [PATCH] feat(adapter): Load adapter from package.json if path provided has one If the path provided for the adapter is a directory containing a package.json file, load the JS file specified in as the main value within that package.json file. --- src/commitizen/adapter.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/commitizen/adapter.js b/src/commitizen/adapter.js index b5d92b84..837597ab 100644 --- a/src/commitizen/adapter.js +++ b/src/commitizen/adapter.js @@ -130,12 +130,19 @@ function resolveAdapterPath(inboundAdapterPath) { // Try to open the provided path try { - // If we're given a directory, append index.js - if(fs.lstatSync(inboundAdapterPath).isDirectory()) { - - // Modify the path and make sure the modified path exists - outboundAdapterPath = path.join(inboundAdapterPath, 'index.js'); - fs.lstatSync(outboundAdapterPath); + // If we're given a directory, check for a package.json, otherwise append index.js + if (fs.lstatSync(inboundAdapterPath).isDirectory()) { + let inboundAdapterPackageJsonPath = path.join(inboundAdapterPath, 'package.json'); + if (fs.lstatSync(inboundAdapterPackageJsonPath)) { + let packageJsonString = fs.readFileSync(inboundAdapterPackageJsonPath, 'utf-8'); + let packageJsonContent = JSON.parse(packageJsonString); + let inboundAdapterMainJsPath = _.get(packageJsonContent, 'main'); + outboundAdapterPath = path.join(inboundAdapterPath, inboundAdapterMainJsPath); + } else { + // Modify the path and make sure the modified path exists + outboundAdapterPath = path.join(inboundAdapterPath, 'index.js'); + fs.lstatSync(outboundAdapterPath); + } } else { // The file exists and is a file, so just return it @@ -147,4 +154,4 @@ function resolveAdapterPath(inboundAdapterPath) { throw err; } -} \ No newline at end of file +}