diff --git a/lib/load_config.js b/lib/load_config.js index 81c8ea676..f57a91d4c 100644 --- a/lib/load_config.js +++ b/lib/load_config.js @@ -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 + diff --git a/test/config_fixture/config b/test/config_fixture/config new file mode 100644 index 000000000..7daacd5db --- /dev/null +++ b/test/config_fixture/config @@ -0,0 +1 @@ +foo: bar \ No newline at end of file diff --git a/test/config_fixture/config.yaml b/test/config_fixture/config.yaml new file mode 100644 index 000000000..20e9ff3fe --- /dev/null +++ b/test/config_fixture/config.yaml @@ -0,0 +1 @@ +foo: bar diff --git a/test/config_fixture/config.yml b/test/config_fixture/config.yml new file mode 100644 index 000000000..20e9ff3fe --- /dev/null +++ b/test/config_fixture/config.yml @@ -0,0 +1 @@ +foo: bar diff --git a/test/config_fixture/config_links.yml b/test/config_fixture/config_links.yml new file mode 100644 index 000000000..4f7e477ef --- /dev/null +++ b/test/config_fixture/config_links.yml @@ -0,0 +1,2 @@ +foo: >- + hello [link](https://github.com/my/link) world diff --git a/test/lib/load_config.js b/test/lib/load_config.js index d8ec8ba42..31e5bc737 100644 --- a/test/lib/load_config.js +++ b/test/lib/load_config.js @@ -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(); });