taneliang / Cotton

WIP Dependency management GitHub bot for Yarn projects
8 stars 1 forks source link

Update package.json without changing formatting #8

Open taneliang opened 6 years ago

taneliang commented 6 years ago

Cotton currently generates updated package.json files using JSON.stringify, indented with 2 spaces and with a newline at the bottom. However, repos may have package.json files formatted differently. Cotton will thus mess them up, resulting in ugly first PR diffs and a poor user experience.

Cotton should ideally update the content of the package.json files without affecting the existing formatting. I have thought of some approaches:

  1. Use regex to replace the strings directly in the original JSON string. This will be manual and is likely to be slow.
  2. Found this magical (and 7 year old) package https://github.com/substack/jsup that seems to do exactly what we want.
taneliang commented 6 years ago

Was just told of https://github.com/ljharb/json-file-plus, which is less accurate but should be faster.

However, upon some investigation, it seems like npm and yarn accept if the same package is included as 2 types of dependencies (npm gives a warning, and yarn only warns if the packages have colliding ranges). As such, a regex that upgrades every occurrence of the package should suffice.