Sha256: 8dd02ced3254a10625de03bd55c6a793041ebf90fdd750a3135c6b334101070a

Contents?: true

Size: 802 Bytes

Versions: 1

Compression:

Stored size: 802 Bytes

Contents

require "json"

module Grundler
  class PackageJsonWriter
    def initialize(path)
      @path = path
    end

    def add(packages)
      write(existing_packages.merge(packages))
    end

    def remove(packages)
      write(
        existing_packages.delete_if { |k, _v| packages.include?(k) }
      )
    end

    def existing_packages
      existing_content["dependencies"]
    end

    private

    def write(packages)
      File.write(
        @path,
        JSON.pretty_generate(
          existing_content.merge({ "dependencies" => packages })
        )
      )
    end

    def existing_content
      @existing_content ||= load_existing_content
    end

    def load_existing_content
      JSON.parse(File.read(@path))
    rescue StandardError
      { "dependencies" => {} }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grundler-0.1.0 lib/grundler/package_json_writer.rb