Skip to content

Commit 86c682e

Browse files
author
Ovidiu Barabula
committed
feat(core): detect config file indentation and keep it
1 parent 696e1e8 commit 86c682e

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"dependencies": {
5555
"chalk": "^2.3.2",
5656
"commander": "^2.15.0",
57+
"detect-indent": "^5.0.0",
5758
"gulp": "^4.0.0",
5859
"inquirer": "^5.2.0"
5960
},
@@ -65,6 +66,7 @@
6566
"@types/chai-as-promised": "^7.1.0",
6667
"@types/chalk": "^2.2.0",
6768
"@types/commander": "^2.12.2",
69+
"@types/detect-indent": "^5.0.0",
6870
"@types/gulp": "^4.0.5",
6971
"@types/inquirer": "^0.0.38",
7072
"@types/lodash": "^4.14.104",

src/util/file-reader.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @since 0.1.0
66
*/
77

8+
import * as detectIndent from 'detect-indent';
89
import * as fs from 'fs';
910
import { Config } from '../config-manager';
1011

@@ -37,6 +38,10 @@ function FileReader(filepath: string): FileReader {
3738
throw new Error(ERRORS.PATH_NOT_STRING);
3839
}
3940

41+
// File indentation will be assigned at first read
42+
let fileIndent: string;
43+
const defaultIndent: string = ' ';
44+
4045

4146
/**
4247
* Get file contents
@@ -49,6 +54,10 @@ function FileReader(filepath: string): FileReader {
4954
return reject(new Error(`${ERRORS.READ_ERROR}: ${readError.message}`));
5055
}
5156

57+
if (typeof fileIndent === 'undefined') {
58+
fileIndent = detectIndent(data).indent || defaultIndent;
59+
}
60+
5261
let parsedJson;
5362
try {
5463
parsedJson = JSON.parse(data);
@@ -68,8 +77,7 @@ function FileReader(filepath: string): FileReader {
6877
*/
6978
function write(object: Config): Promise<boolean|Error> {
7079
return new Promise((resolve, reject) => {
71-
// TODO: replace 2 with 'detect-indent' module's output
72-
const content = JSON.stringify(object, null, 2);
80+
const content = JSON.stringify(object, null, fileIndent);
7381
// Write the file content with an empty line at the end
7482
fs.writeFile(filepath, `${content}\n`, writeError => {
7583
if (writeError) {

webpack.config.babel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const configuration = {
7070
commander: 'commander',
7171
gulp: 'gulp',
7272
inquirer: 'inquirer',
73+
'detect-indent': 'detect-indent',
7374
},
7475

7576
optimization: {

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,10 @@
613613
dependencies:
614614
commander "*"
615615

616+
"@types/detect-indent@^5.0.0":
617+
version "5.0.0"
618+
resolved "https://registry.yarnpkg.com/@types/detect-indent/-/detect-indent-5.0.0.tgz#8b1bbd7891268d5ed20d23ecd23ae333d33934cd"
619+
616620
"@types/events@*":
617621
version "1.2.0"
618622
resolved "https://registry.yarnpkg.com/@types/events/-/events-1.2.0.tgz#81a6731ce4df43619e5c8c945383b3e62a89ea86"
@@ -2929,6 +2933,10 @@ detect-indent@4.0.0, detect-indent@^4.0.0:
29292933
dependencies:
29302934
repeating "^2.0.0"
29312935

2936+
detect-indent@^5.0.0:
2937+
version "5.0.0"
2938+
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
2939+
29322940
detect-libc@^1.0.2:
29332941
version "1.0.3"
29342942
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy