Skip to content

fix(config): only strip comments on json files #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ var yaml = require('js-yaml'),
* @throws {Error} if the file cannot be read.
*/
function loadConfig(filePath) {
var ext = path.extname(filePath);
var rawFile = fs.readFileSync(
path.resolve(process.cwd(), filePath), 'utf8'
);

try {
return yaml.safeLoad(
stripComments(
fs.readFileSync(
path.resolve(process.cwd(), filePath), 'utf8')));
if (ext === '.json') {
return JSON.parse(stripComments(rawFile));
}

return yaml.safeLoad(rawFile);
} catch (e) {
e.message = 'Cannot read config file: ' +
filePath +
Expand Down
1 change: 1 addition & 0 deletions test/config_fixture/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
1 change: 1 addition & 0 deletions test/config_fixture/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
1 change: 1 addition & 0 deletions test/config_fixture/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
2 changes: 2 additions & 0 deletions test/config_fixture/config_links.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo: >-
hello [link](https://github.com/my/link) world
12 changes: 12 additions & 0 deletions test/lib/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@ test('loadConfig', function (t) {
t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config_comments.json')),
{ foo: 'bar' }, 'config with comments');

t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config.yaml')),
{ foo: 'bar' }, 'config.yaml');

t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config.yml')),
{ foo: 'bar' }, 'config.yml');

t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config')),
{ foo: 'bar' }, 'config in yaml without extension');

t.deepEqual(loadConfig(path.join(__dirname, '../config_fixture/config_links.yml')),
{ foo: 'hello [link](https://github.com/my/link) world' }, 'config with markdown link');

t.end();
});