提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.0 KiB

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