Skip to content

Commit 30c7b55

Browse files
committed
fix(adapter): fix package.json spaces being removed
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
1 parent 7bd963f commit 30c7b55

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"chalk": "1.1.1",
6060
"cz-conventional-changelog": "1.1.4",
6161
"dedent": "0.6.0",
62+
"detect-indent": "4.0.0",
6263
"find-node-modules": "1.0.1",
6364
"glob": "5.0.15",
6465
"gulp": "3.9.0",

src/commitizen/adapter.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path';
22
import fs from 'fs';
33
import findNodeModules from 'find-node-modules';
44
import _ from 'lodash';
5+
import detectIndent from 'detect-indent';
56

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

@@ -39,13 +40,15 @@ function addPathToAdapterConfig(sh, cliPath, repoPath, adapterNpmName) {
3940
};
4041

4142
let packageJsonPath = path.join(getNearestProjectRootDirectory(), 'package.json');
42-
let packageJsonString = fs.readFileSync(packageJsonPath);
43+
let packageJsonString = fs.readFileSync(packageJsonPath, 'utf-8');
44+
// tries to detect the indentation and falls back to a default if it can't
45+
let indent = detectIndent(packageJsonString).indent || ' ';
4346
let packageJsonContent = JSON.parse(packageJsonString);
4447
let newPackageJsonContent = '';
4548
if(_.get(packageJsonContent,'config.commitizen.path') !== adapterNpmName) {
4649
newPackageJsonContent = _.merge(packageJsonContent, commitizenAdapterConfig);
4750
}
48-
fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJsonContent));
51+
fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJsonContent, null, indent));
4952
}
5053

5154
/**

0 commit comments

Comments
 (0)