update-browserslist-db 1.0.6 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +9 -13
  2. package/package.json +1 -1
  3. package/utils.js +22 -0
package/index.js CHANGED
@@ -4,17 +4,7 @@ let pico = require('picocolors')
4
4
  let path = require('path')
5
5
  let fs = require('fs')
6
6
 
7
- const DEFAULT_INDENT = ' '
8
- const INDENT_REGEXP = /^(\s*)[^\s]/m
9
-
10
- function detectIndent(text) {
11
- try {
12
- return INDENT_REGEXP.exec(text)[1] || DEFAULT_INDENT
13
- } catch (e) {
14
- /* c8 ignore next 2 */
15
- return DEFAULT_INDENT
16
- }
17
- }
7
+ const { detectIndent, detectEOL } = require('./utils')
18
8
 
19
9
  function BrowserslistUpdateError(message) {
20
10
  this.name = 'BrowserslistUpdateError'
@@ -172,11 +162,17 @@ function updateYarnLockfile(lock, latest) {
172
162
  function updateLockfile(lock, latest) {
173
163
  if (!lock.content) lock.content = fs.readFileSync(lock.file).toString()
174
164
 
165
+ let updatedLockFile
175
166
  if (lock.mode === 'yarn') {
176
- return updateYarnLockfile(lock, latest)
167
+ updatedLockFile = updateYarnLockfile(lock, latest)
177
168
  } else {
178
- return updateNpmLockfile(lock, latest)
169
+ updatedLockFile = updateNpmLockfile(lock, latest)
179
170
  }
171
+ updatedLockFile.content = updatedLockFile.content.replace(
172
+ /\n/g,
173
+ detectEOL(lock.content)
174
+ )
175
+ return updatedLockFile
180
176
  }
181
177
 
182
178
  function updatePackageManually(print, lock, latest) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "update-browserslist-db",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
4
4
  "description": "CLI tool to update caniuse-lite to refresh target browsers from Browserslist config",
5
5
  "keywords": [
6
6
  "caniuse",
package/utils.js ADDED
@@ -0,0 +1,22 @@
1
+ const { EOL } = require('os')
2
+
3
+ const getFirstRegexpMatchOrDefault = (text, regexp, defaultValue) => {
4
+ regexp.lastIndex = 0 // https://stackoverflow.com/a/11477448/4536543
5
+ let match = regexp.exec(text)
6
+ if (match !== null) return match[1]
7
+ return defaultValue
8
+ }
9
+
10
+ const DEFAULT_INDENT = ' '
11
+ const INDENT_REGEXP = /^([ \t]+)[^\s]/m
12
+
13
+ module.exports.detectIndent = text =>
14
+ getFirstRegexpMatchOrDefault(text, INDENT_REGEXP, DEFAULT_INDENT)
15
+ module.exports.DEFAULT_INDENT = DEFAULT_INDENT
16
+
17
+ const DEFAULT_EOL = EOL
18
+ const EOL_REGEXP = /(\r\n|\n|\r)/g
19
+
20
+ module.exports.detectEOL = text =>
21
+ getFirstRegexpMatchOrDefault(text, EOL_REGEXP, DEFAULT_EOL)
22
+ module.exports.DEFAULT_EOL = DEFAULT_EOL