Skip to content

Commit 70c081f

Browse files
dignifiedquiretmcw
authored andcommitted
fix(config): only strip comments on json files (#611)
* fix(config): only strip comments on json files Fixes #608 * refactor(config): use path.extname instead of custom logic
1 parent 98beb6d commit 70c081f

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

lib/load_config.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ var yaml = require('js-yaml'),
1515
* @throws {Error} if the file cannot be read.
1616
*/
1717
function loadConfig(filePath) {
18+
var ext = path.extname(filePath);
19+
var rawFile = fs.readFileSync(
20+
path.resolve(process.cwd(), filePath), 'utf8'
21+
);
22+
1823
try {
19-
return yaml.safeLoad(
20-
stripComments(
21-
fs.readFileSync(
22-
path.resolve(process.cwd(), filePath), 'utf8')));
24+
if (ext === '.json') {
25+
return JSON.parse(stripComments(rawFile));
26+
}
27+
28+
return yaml.safeLoad(rawFile);
2329
} catch (e) {
2430
e.message = 'Cannot read config file: ' +
2531
filePath +

test/config_fixture/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo: bar

test/config_fixture/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo: bar

test/config_fixture/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo: bar

test/config_fixture/config_links.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo: >-
2+
hello [link](https://github.com/my/link) world

test/lib/load_config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,17 @@ test('loadConfig', function (t) {
1616
t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config_comments.json')),
1717
{ foo: 'bar' }, 'config with comments');
1818

19+
t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config.yaml')),
20+
{ foo: 'bar' }, 'config.yaml');
21+
22+
t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config.yml')),
23+
{ foo: 'bar' }, 'config.yml');
24+
25+
t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config')),
26+
{ foo: 'bar' }, 'config in yaml without extension');
27+
28+
t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config_links.yml')),
29+
{ foo: 'hello [link](https://github.com/my/link) world' }, 'config with markdown link');
30+
1931
t.end();
2032
});

0 commit comments

Comments
 (0)