1 | # read-package-json-fast
|
---|
2 |
|
---|
3 | Like [`read-package-json`](http://npm.im/read-package-json), but faster and
|
---|
4 | more accepting of "missing" data.
|
---|
5 |
|
---|
6 | This is only suitable for reading package.json files in a node_modules
|
---|
7 | tree, since it doesn't do the various cleanups, normalization, and warnings
|
---|
8 | that are beneficial at the root level in a package being published.
|
---|
9 |
|
---|
10 | ## USAGE
|
---|
11 |
|
---|
12 | ```js
|
---|
13 | const rpj = require('read-package-json-fast')
|
---|
14 |
|
---|
15 | // typical promisey type API
|
---|
16 | rpj('/path/to/package.json')
|
---|
17 | .then(data => ...)
|
---|
18 | .catch(er => ...)
|
---|
19 |
|
---|
20 | // or just normalize a package manifest
|
---|
21 | const normalized = rpj.normalize(packageJsonObject)
|
---|
22 | ```
|
---|
23 |
|
---|
24 | Errors raised from parsing will use
|
---|
25 | [`json-parse-even-better-errors`](http://npm.im/json-parse-even-better-errors),
|
---|
26 | so they'll be of type `JSONParseError` and have a `code: 'EJSONPARSE'`
|
---|
27 | property. Errors will also always have a `path` member referring to the
|
---|
28 | path originally passed into the function.
|
---|
29 |
|
---|
30 | ## Indentation
|
---|
31 |
|
---|
32 | To preserve indentation when the file is saved back to disk, use
|
---|
33 | `data[Symbol.for('indent')]` as the third argument to `JSON.stringify`, and
|
---|
34 | if you want to preserve windows `\r\n` newlines, replace the `\n` chars in
|
---|
35 | the string with `data[Symbol.for('newline')]`.
|
---|
36 |
|
---|
37 | For example:
|
---|
38 |
|
---|
39 | ```js
|
---|
40 | const data = await readPackageJsonFast('./package.json')
|
---|
41 | const indent = Symbol.for('indent')
|
---|
42 | const newline = Symbol.for('newline')
|
---|
43 | // .. do some stuff to the data ..
|
---|
44 | const string = JSON.stringify(data, null, data[indent]) + '\n'
|
---|
45 | const eolFixed = data[newline] === '\n' ? string
|
---|
46 | : string.replace(/\n/g, data[newline])
|
---|
47 | await writeFile('./package.json', eolFixed)
|
---|
48 | ```
|
---|
49 |
|
---|
50 | Indentation is determined by looking at the whitespace between the initial
|
---|
51 | `{` and the first `"` that follows it. If you have lots of weird
|
---|
52 | inconsistent indentation, then it won't track that or give you any way to
|
---|
53 | preserve it. Whether this is a bug or a feature is debatable ;)
|
---|
54 |
|
---|
55 | ## WHAT THIS MODULE DOES
|
---|
56 |
|
---|
57 | - Parse JSON
|
---|
58 | - Normalize `bundledDependencies`/`bundleDependencies` naming to just
|
---|
59 | `bundleDependencies` (without the extra `d`)
|
---|
60 | - Handle `true`, `false`, or object values passed to `bundleDependencies`
|
---|
61 | - Normalize `funding: <string>` to `funding: { url: <string> }`
|
---|
62 | - Remove any `scripts` members that are not a string value.
|
---|
63 | - Normalize a string `bin` member to `{ [name]: bin }`.
|
---|
64 | - Fold `optionalDependencies` into `dependencies`.
|
---|
65 | - Set the `_id` property if name and version are set. (This is
|
---|
66 | load-bearing in a few places within the npm CLI.)
|
---|
67 |
|
---|
68 | ## WHAT THIS MODULE DOES NOT DO
|
---|
69 |
|
---|
70 | - Warn about invalid/missing name, version, repository, etc.
|
---|
71 | - Extract a description from the `README.md` file, or attach the readme to
|
---|
72 | the parsed data object.
|
---|
73 | - Read the `HEAD` value out of the `.git` folder.
|
---|
74 | - Warn about potentially typo'ed scripts (eg, `tset` instead of `test`)
|
---|
75 | - Check to make sure that all the files in the `files` field exist and are
|
---|
76 | valid files.
|
---|
77 | - Fix bundleDependencies that are not listed in `dependencies`.
|
---|
78 | - Fix `dependencies` fields that are not strictly objects of string values.
|
---|
79 | - Anything involving the `directories` field (ie, bins, mans, and so on).
|
---|