Skip to content

Commit

Permalink
fix(adapter): fix package.json spaces being removed
Browse files Browse the repository at this point in the history
To fix windows issues in the json npm package, we switched to using JSON.parse and writeFileSync. In
this switch I forgot about package.json formatting. Luckily @sindresorhus has already created
detect-indent so I've switched to using that.

Closes #72
  • Loading branch information
jimthedev committed Nov 16, 2015
1 parent 7bd963f commit 30c7b55
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"chalk": "1.1.1",
"cz-conventional-changelog": "1.1.4",
"dedent": "0.6.0",
"detect-indent": "4.0.0",
"find-node-modules": "1.0.1",
"glob": "5.0.15",
"gulp": "3.9.0",
Expand Down
7 changes: 5 additions & 2 deletions src/commitizen/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import fs from 'fs';
import findNodeModules from 'find-node-modules';
import _ from 'lodash';
import detectIndent from 'detect-indent';

import {isFunction} from '../common/util';

Expand Down Expand Up @@ -39,13 +40,15 @@ function addPathToAdapterConfig(sh, cliPath, repoPath, adapterNpmName) {
};

let packageJsonPath = path.join(getNearestProjectRootDirectory(), 'package.json');
let packageJsonString = fs.readFileSync(packageJsonPath);
let packageJsonString = fs.readFileSync(packageJsonPath, 'utf-8');
// tries to detect the indentation and falls back to a default if it can't
let indent = detectIndent(packageJsonString).indent || ' ';
let packageJsonContent = JSON.parse(packageJsonString);
let newPackageJsonContent = '';
if(_.get(packageJsonContent,'config.commitizen.path') !== adapterNpmName) {
newPackageJsonContent = _.merge(packageJsonContent, commitizenAdapterConfig);
}
fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJsonContent));
fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJsonContent, null, indent));
}

/**
Expand Down

0 comments on commit 30c7b55

Please sign in to comment.