Class: Dependabot::NpmAndYarn::FileUpdater::PackageJsonPreparer
- Inherits:
-
Object
- Object
- Dependabot::NpmAndYarn::FileUpdater::PackageJsonPreparer
- Defined in:
- lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb
Instance Method Summary collapse
-
#initialize(package_json_content:) ⇒ PackageJsonPreparer
constructor
A new instance of PackageJsonPreparer.
- #prepared_content ⇒ Object
- #remove_invalid_characters(content) ⇒ Object
-
#remove_workspace_path_prefixes(content) ⇒ Object
A bug prevents Yarn recognising that a directory is part of a workspace if it is specified with a ‘./` prefix.
- #replace_ssh_sources(content) ⇒ Object
- #swapped_ssh_requirements ⇒ Object
Constructor Details
#initialize(package_json_content:) ⇒ PackageJsonPreparer
Returns a new instance of PackageJsonPreparer.
11 12 13 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 11 def initialize(package_json_content:) @package_json_content = package_json_content end |
Instance Method Details
#prepared_content ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 15 def prepared_content content = package_json_content content = replace_ssh_sources(content) content = remove_workspace_path_prefixes(content) content = remove_invalid_characters(content) content end |
#remove_invalid_characters(content) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 55 def remove_invalid_characters(content) content .gsub(/\{\{[^\}]*?\}\}/, "something") # {{ nm }} syntax not allowed .gsub(/(?<!\\)\\ /, " ") # escaped whitespace not allowed .gsub(%r{^\s*//.*}, " ") # comments are not allowed end |
#remove_workspace_path_prefixes(content) ⇒ Object
A bug prevents Yarn recognising that a directory is part of a workspace if it is specified with a ‘./` prefix.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 36 def remove_workspace_path_prefixes(content) json = JSON.parse(content) return content unless json.key?("workspaces") workspace_object = json.fetch("workspaces") paths_array = if workspace_object.is_a?(Hash) workspace_object.values_at("packages", "nohoist") .flatten.compact elsif workspace_object.is_a?(Array) then workspace_object else raise "Unexpected workspace object" end paths_array.each { |path| path.gsub!(%r{^\./}, "") } JSON.pretty_generate(json) end |
#replace_ssh_sources(content) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 23 def replace_ssh_sources(content) updated_content = content git_ssh_requirements_to_swap.each do |req| new_req = req.gsub(%r{git\+ssh://git@(.*?)[:/]}, 'https://\1/') updated_content = updated_content.gsub(req, new_req) end updated_content end |
#swapped_ssh_requirements ⇒ Object
62 63 64 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 62 def swapped_ssh_requirements git_ssh_requirements_to_swap end |