lib/svelte_on_rails/lib/development_utils.rb



module SvelteOnRails
  class DevelopmentUtils

    def self.local_npm_package_url
      str = contributor_config('local_npm_package_path', required: false)
      if !str.present?
        nil
      elsif !Dir.exist?(str)
        raise "Invalid path given on local_npm_package_path: «#{str}»"
      elsif !File.exist?(Pathname.new(str).join('package.json'))
        raise "Given local_npm_package_path does not seem to be a valid npm package as it does not contain a package.json"
      else
        return Pathname.new(str)
      end
    end

    def self.gem_folder
      Pathname.new(File.expand_path('../../..', __dir__))
    end

    def self.contributor_config(key, required: true)
      config_file = File.expand_path('../svelte_on_rails_contributor_configs.yml', gem_folder)
      if !File.exist?(config_file)
        if required
          raise "Missing configuration file, searched at: «#{config_file}»"
        end
      else
        yml = YAML.load_file(config_file)
        value = yml[key.to_s]
        if required && !value.present?
          raise "Missing value «#{key}» in configuration file: «#{config_file}»"
        end
        value
      end
    end

  end
end