Skip to content

Commit

Permalink
Respect package.json indentation when updating the lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
jurre committed Feb 24, 2022
1 parent ca3c64a commit 36dfd7c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ def write_temporary_dependency_files(update_package_json: true)
updated_content = lock_deps_with_latest_reqs(updated_content)

updated_content = sanitized_package_json_content(updated_content)

File.write(file.name, updated_content)
end
end
Expand All @@ -495,6 +496,16 @@ def write_lockfiles
end
end

# Takes a JSON string and detects if it is spaces or tabs and how many
# levels deep it is indented.
def detect_indentation(json)
indentation = json.scan(/^\s+/).min_by(&:length)
indentation_size = indentation.length
indentation_type = indentation.scan(/\t/).any? ? "\t" : " "

indentation_type * indentation_size
end

def lock_git_deps(content)
return content if git_dependencies_to_lock.empty?

Expand All @@ -508,7 +519,8 @@ def lock_git_deps(content)
end
end

json.to_json
indent = detect_indentation(content)
JSON.pretty_generate(json, indent: indent)
end

def git_dependencies_to_lock
Expand Down Expand Up @@ -549,7 +561,8 @@ def lock_deps_with_latest_reqs(content)
end
end

json.to_json
indent = detect_indentation(content)
JSON.pretty_generate(json, indent: indent)
end

def replace_ssh_sources(content)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "npm-sub-dep-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"test-old-npm-sub-dependency": "github:dependabot-fixtures/test-old-npm-sub-dependency"
}
"name": "npm-sub-dep-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"test-old-npm-sub-dependency": "github:dependabot-fixtures/test-old-npm-sub-dependency"
}
}

0 comments on commit 36dfd7c

Please sign in to comment.