From 66ab19cf7d64d6579423b8f0b4652e3a0d8e09c2 Mon Sep 17 00:00:00 2001 From: Jim Cummins Date: Sun, 8 Nov 2015 19:46:17 -0600 Subject: [PATCH] fix(adapter): fix windows json editing The json parsing that we were using was overly complicated. This is much simpler and solves some of the odd behavior that was happening on windows when installing and dealing with adapters. Closes #42 --- package.json | 1 - src/commitizen/adapter.js | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6152d151..bbf2b418 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,6 @@ "gulp": "3.9.0", "gulp-git": "1.6.0", "inquirer": "0.11.0", - "json": "9.0.3", "minimist": "1.2.0", "node-uuid": "1.4.3", "nodemon": "1.8.1", diff --git a/src/commitizen/adapter.js b/src/commitizen/adapter.js index 7853c40e..0fc7dc89 100644 --- a/src/commitizen/adapter.js +++ b/src/commitizen/adapter.js @@ -1,6 +1,7 @@ import path from 'path'; import fs from 'fs'; import findNodeModules from 'find-node-modules'; +import _ from 'lodash'; import {isFunction} from '../common/util'; @@ -28,8 +29,23 @@ 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'); - sh.exec(`${cliPath}/node_modules/.bin/json -I -f ${packageJsonPath} -e 'if(!this.config) {this.config={};}; if(!this.config.commitizen) { this.config.commitizen={};}; this.config.commitizen.path=\"./node_modules/${adapterNpmName}\"'`); + let packageJsonString = fs.readFileSync(packageJsonPath); + let packageJsonContent = JSON.parse(packageJsonString); + let newPackageJsonContent = ''; + if(_.get(packageJsonContent,'config.commitizen.path') !== adapterNpmName) { + newPackageJsonContent = _.merge(packageJsonContent, commitizenAdapterConfig); + } + fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJsonContent)); } /**