browserslist / update-db

CLI tool to update caniuse-lite to refresh target browsers from Browserslist config
https://browsersl.ist/
MIT License
364 stars 70 forks source link

Messed up lock file formatting when using CRLF EOL #6

Closed Maxim-Mazurok closed 2 years ago

Maxim-Mazurok commented 2 years ago

After the first run of npx -y update-browserslist-db@latest in our project, it messed up package-lock.json formatting, see:

This is on my colleague's PC (windows, I believe)

image

This is on my PC (Ubuntu, WSL2): image

This is my output:

image

After that, I changed nothing re-running the command worked just fine. Can't reproduce anymore.

Maxim-Mazurok commented 2 years ago

Here's a reproduction:

const DEFAULT_INDENT = "  ";
const INDENT_REGEXP = /^(\s+)[^\s]/m;

function detectIndent(text) {
  try {
    return INDENT_REGEXP.exec(text)[1] || DEFAULT_INDENT;
  } catch (e) {
    /* c8 ignore next 2 */
    return DEFAULT_INDENT;
  }
}

const text = `{\r\n  \"name\": \"my-project\",\r\n`;

console.log(JSON.stringify(detectIndent(text)));

will print "\n "

As you can see, it includes \n into the detected indentation.

The problem is with this function: https://github1s.com/browserslist/update-db/blob/HEAD/index.js#L10-L17

Maxim-Mazurok commented 2 years ago

My colleague and I would like to take on this issue if that's ok