Skip to content

Commit 80cfeb8

Browse files
committed
feat: added "resolve" option for smartRead
1 parent 320b1c5 commit 80cfeb8

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Will only return entries where `fs.lstatSync(...).isFile()` evaluates to true (t
3838

3939
Delete file and all empty parent directories.
4040

41-
### smartRead(filepath, options = { treatAs = null })
41+
### smartRead(filepath, options = { treatAs = null, resolve = true })
4242

4343
Read and parse file based on file extension.
4444

@@ -54,6 +54,8 @@ Note that the [required cache](https://nodejs.org/api/modules.html#modules_requi
5454

5555
To ignore file extension and force treat the file as a certain type, you can pass the option `treatAs` as e.g. `json`.
5656

57+
To simply load yml files without resolving them pass `resolve` as `false`
58+
5759
### smartWrite(filepath. content, options = { treatAs = null, mergeStrategy = (existing, changeset) => changeset, create = true, pretty = false, keepOrder = true })
5860

5961
Serialize and write content to file based on file extension.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
],
108108
"dependencies": {
109109
"fs-extra": "8.1.0",
110+
"js-yaml": "3.13.1",
110111
"json-stringify-pretty-compact": "2.0.0",
111112
"lodash.clonedeep": "4.5.0",
112113
"lodash.isequal": "4.5.0",

src/logic/smart-read.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const assert = require('assert');
22
const fs = require('fs');
3-
const yaml = require('yaml-boost');
3+
const yamlBoost = require('yaml-boost');
4+
const yaml = require('js-yaml');
45
const xmlParser = require('../util/xml-parser');
56
const getExt = require('../util/get-ext');
67

@@ -9,9 +10,10 @@ module.exports = (filepath, options = {}) => {
910
assert(typeof filepath === 'string');
1011
assert(options instanceof Object && !Array.isArray(options));
1112

12-
const ctx = { treatAs: null, ...options };
13-
assert(Object.keys(ctx).length === 1, 'Unexpected Option provided!');
13+
const ctx = { treatAs: null, resolve: true, ...options };
14+
assert(Object.keys(ctx).length === 2, 'Unexpected Option provided!');
1415
assert(ctx.treatAs === null || typeof ctx.treatAs === 'string');
16+
assert(typeof ctx.resolve === 'boolean');
1517

1618
let result;
1719
switch (ctx.treatAs || getExt(filepath)) {
@@ -23,7 +25,9 @@ module.exports = (filepath, options = {}) => {
2325
break;
2426
case 'yml':
2527
case 'yaml':
26-
result = yaml.load(filepath, {});
28+
result = ctx.resolve
29+
? yamlBoost.load(filepath, {})
30+
: yaml.load(fs.readFileSync(filepath, 'utf8'));
2731
break;
2832
case 'js':
2933
// eslint-disable-next-line import/no-dynamic-require,global-require

test/logic/smart-read.spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ describe('Testing smartRead', { useTmpDir: true }, () => {
4444
);
4545
});
4646

47-
it('Testing .yml', () => {
48-
executeTest('file.yml', 'key: value', { key: 'value' });
47+
it('Testing .yml (resolve)', () => {
48+
executeTest('file.yml', '<<<:\n - key: value', { key: 'value' });
49+
});
50+
51+
it('Testing .yml (no resolve)', () => {
52+
executeTest('file.yml', '<<<:\n - key: value', { '<<<': [{ key: 'value' }] }, { resolve: false });
4953
});
5054

5155
it('Testing .js', () => {

0 commit comments

Comments
 (0)