read-package-json-fast
Advanced tools
Comparing version 1.2.0 to 1.2.1
12
index.js
@@ -5,14 +5,4 @@ const {promisify} = require('util') | ||
const parse = require('json-parse-even-better-errors') | ||
const _indent = Symbol.for('indent') | ||
const rpj = path => readFile(path, 'utf8') | ||
.then(data => { | ||
// get the indentation so that we can save it back nicely | ||
// if the file starts with {" then we have an indent of '', ie, none | ||
// otherwise, pick the indentation of the next line after the first \n | ||
// JSON.stringify ignores symbols, so this is reasonably safe. | ||
const indent = data.match(/^\{\n?([\s\t]*)"/) | ||
const result = normalize(parse(data)) | ||
result[_indent] = indent[1] | ||
return result | ||
}) | ||
.then(data => normalize(parse(data))) | ||
.catch(er => { | ||
@@ -19,0 +9,0 @@ er.path = path |
{ | ||
"name": "read-package-json-fast", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Like read-package-json, but faster", | ||
@@ -21,3 +21,3 @@ "author": "Isaac Z. Schlueter <i@izs.me> (https://izs.me)", | ||
"dependencies": { | ||
"json-parse-even-better-errors": "^2.0.1", | ||
"json-parse-even-better-errors": "^2.3.0", | ||
"npm-normalize-package-bin": "^1.0.1" | ||
@@ -24,0 +24,0 @@ }, |
@@ -33,3 +33,5 @@ # read-package-json-fast | ||
To preserve indentation when the file is saved back to disk, use | ||
`data[Symbol.for('indent')]` as the third argument to `JSON.stringify`. | ||
`data[Symbol.for('indent')]` as the third argument to `JSON.stringify`, and | ||
if you want to preserve windows `\r\n` newlines, replace the `\n` chars in | ||
the string with `data[Symbol.for('newline')]`. | ||
@@ -41,5 +43,8 @@ For example: | ||
const indent = Symbol.for('indent') | ||
const newline = Symbol.for('newline') | ||
// .. do some stuff to the data .. | ||
const string = JSON.stringify(data, null, data[indent]) + '\n' | ||
await writeFile('./package.json', string) | ||
const eolFixed = data[newline] === '\n' ? string | ||
: string.replace(/\n/g, data[newline]) | ||
await writeFile('./package.json', eolFixed) | ||
``` | ||
@@ -50,3 +55,3 @@ | ||
inconsistent indentation, then it won't track that or give you any way to | ||
preserve it. | ||
preserve it. Whether this is a bug or a feature is debatable ;) | ||
@@ -53,0 +58,0 @@ ## WHAT THIS MODULE DOES |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80
6506
71