Class: Dependabot::NpmAndYarn::FileUpdater::PackageJsonPreparer
- Inherits:
-
Object
- Object
- Dependabot::NpmAndYarn::FileUpdater::PackageJsonPreparer
- Extended by:
- T::Sig
- 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
- #replace_ssh_sources(content) ⇒ Object
- #swapped_ssh_requirements ⇒ Object
Constructor Details
#initialize(package_json_content:) ⇒ PackageJsonPreparer
Returns a new instance of PackageJsonPreparer.
16 17 18 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 16 def initialize(package_json_content:) @package_json_content = package_json_content end |
Instance Method Details
#prepared_content ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 21 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
64 65 66 67 68 69 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 64 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 44 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
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 30 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
72 73 74 |
# File 'lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb', line 72 def swapped_ssh_requirements git_ssh_requirements_to_swap end |