Skip to content

Detect and preserve npm package-lock.json file indentation #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions detect-indent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const DEFAULT_INDENT = ' '

function detectIndent(text) {
let indentRegexp = /^(\s*)"/m
try {
let indent = indentRegexp.exec(text)[1]
return indent || DEFAULT_INDENT
} catch (e) {
return DEFAULT_INDENT
}
}

module.exports = detectIndent
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ let pico = require('picocolors')
let path = require('path')
let fs = require('fs')

let detectIndent = require('./detect-indent')

function BrowserslistUpdateError(message) {
this.name = 'BrowserslistUpdateError'
this.message = message
Expand Down Expand Up @@ -104,7 +106,8 @@ function diffBrowsers(old, current) {
function updateNpmLockfile(lock, latest) {
let metadata = { latest, versions: [] }
let content = deletePackage(JSON.parse(lock.content), metadata)
metadata.content = JSON.stringify(content, null, ' ')
let indent = detectIndent(lock.content)
metadata.content = JSON.stringify(content, null, indent)
return metadata
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"unit": "uvu test .test.js"
},
"dependencies": {
"detect-indent": "^6.1.0",
"escalade": "^3.1.1",
"picocolors": "^1.0.0"
},
Expand Down