Sha256: 4c9f110cdfe8879169ead46e295cb5cc77b96baacc4930114217958ad71dd707

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require "json"

KEYS_TO_CHECK = %w[engines lint-staged].freeze

module NeetoCompliance
  class PackageJsonVerifier < Base
    def valid?
      @common_lint_staged = JSON.parse(open(common_packages_latest_file).read)
      project_lint_staged = JSON.parse(open("package.json").read)

      @results = KEYS_TO_CHECK.map { |key| @common_lint_staged[key].to_json === project_lint_staged[key].to_json }
      @results.all?
    end

    def autofix_command
      "echo '#{updated_package.to_json}' > package.json"
    end

    def updated_package
      package = JSON.parse(open("package.json").read)
      KEYS_TO_CHECK.map { |key| package[key] = @common_lint_staged[key] }
      package
    end

    def autofix_suggestion
      return "" if @results.all?

      command = ""
      KEYS_TO_CHECK.each_with_index do |key, index|
        next if @results[index]

        command += "Replace the value of \"#{key}\" in package.json with:\n\n#{JSON.pretty_generate(@common_lint_staged[key])}\n\n"
      end

      command.yellow
    end

    private

      def common_packages_latest_file
        NeetoCompliance::NeetoCommons.path.join "common_files/package-common.json"
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
neeto-compliance-1.0.58 lib/neeto_compliance/verifiers/package_json_verifier.rb