From 5742cf9c7909a26ce6ddf8c21c759b2b35943a41 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 21 Nov 2016 18:32:22 +0100 Subject: [PATCH 1/2] fix(config): only strip comments on json files Fixes #608 --- lib/load_config.js | 17 ++++++++++++----- test/config_fixture/config | 1 + test/config_fixture/config.yaml | 1 + test/config_fixture/config.yml | 1 + test/config_fixture/config_links.yml | 2 ++ test/lib/load_config.js | 12 ++++++++++++ 6 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 test/config_fixture/config create mode 100644 test/config_fixture/config.yaml create mode 100644 test/config_fixture/config.yml create mode 100644 test/config_fixture/config_links.yml diff --git a/lib/load_config.js b/lib/load_config.js index 81c8ea676..0e59040ac 100644 --- a/lib/load_config.js +++ b/lib/load_config.js @@ -3,7 +3,8 @@ var yaml = require('js-yaml'), fs = require('fs'), path = require('path'), - stripComments = require('strip-json-comments'); + stripComments = require('strip-json-comments'), + _ = require('lodash'); /** * Try to load a configuration file: since this is configuration, we're @@ -15,11 +16,17 @@ var yaml = require('js-yaml'), * @throws {Error} if the file cannot be read. */ function loadConfig(filePath) { + var ext = _.last(filePath.split('.')); + 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(); }); From 2159aad1b993831f0c069266e6fb778dff578106 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 21 Nov 2016 18:49:27 +0100 Subject: [PATCH 2/2] refactor(config): use path.extname instead of custom logic --- lib/load_config.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/load_config.js b/lib/load_config.js index 0e59040ac..f57a91d4c 100644 --- a/lib/load_config.js +++ b/lib/load_config.js @@ -3,8 +3,7 @@ var yaml = require('js-yaml'), fs = require('fs'), path = require('path'), - stripComments = require('strip-json-comments'), - _ = require('lodash'); + stripComments = require('strip-json-comments'); /** * Try to load a configuration file: since this is configuration, we're @@ -16,13 +15,13 @@ var yaml = require('js-yaml'), * @throws {Error} if the file cannot be read. */ function loadConfig(filePath) { - var ext = _.last(filePath.split('.')); + var ext = path.extname(filePath); var rawFile = fs.readFileSync( path.resolve(process.cwd(), filePath), 'utf8' ); try { - if (ext === 'json') { + if (ext === '.json') { return JSON.parse(stripComments(rawFile)); } 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