Sha256: a9907dbb83f239cf6d84f8d30c81814333de245b43fe62b90839a4963d6f1b0e

Contents?: true

Size: 1.36 KB

Versions: 9

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require "open3"

require_relative "yarn/package"
require_relative "yarn/package_repo"

module CobraCommander
  module Dependencies
    # Yarn workspace components source for an umbrella
    class YarnWorkspace
      attr_reader :packages

      def initialize(root_path)
        @repo = Yarn::PackageRepo.new
        @root_package = Yarn::Package.new(root_path)
        @repo.load_linked_specs(@root_package)
        load_workspace_packages
      end

      def path
        @root_package.path
      end

      def dependencies
        (workspace_spec.keys | @root_package.dependencies.keys).map(&method(:untag))
      end

      def components
        @repo.specs.map do |spec|
          { path: spec.path, name: untag(spec.name), dependencies: spec.dependencies.keys.map(&method(:untag)) }
        end
      end

    private

      def load_workspace_packages
        workspace_spec.map do |_name, spec|
          @repo.load_spec File.expand_path(File.join(@root_package.path, "..", spec["location"]))
        end
      end

      def workspace_spec
        @workspace_spec ||= begin
          output, = Open3.capture2("yarn workspaces --json info", chdir: File.dirname(@root_package.path))
          JSON.parse(JSON.parse(output)["data"])
        end
      end

      def untag(name)
        name.gsub(@root_package.project_tag, "")
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
cobra_commander-0.12.0 lib/cobra_commander/dependencies/yarn_workspace.rb
cobra_commander-0.11.0 lib/cobra_commander/dependencies/yarn_workspace.rb
cobra_commander-0.10.0 lib/cobra_commander/dependencies/yarn_workspace.rb
cobra_commander-0.9.2 lib/cobra_commander/dependencies/yarn_workspace.rb
cobra_commander-0.9.1 lib/cobra_commander/dependencies/yarn_workspace.rb
cobra_commander-0.9.0 lib/cobra_commander/dependencies/yarn_workspace.rb
cobra_commander-0.8.1 lib/cobra_commander/dependencies/yarn_workspace.rb
cobra_commander-0.8.0 lib/cobra_commander/dependencies/yarn_workspace.rb
cobra_commander-0.7.0 lib/cobra_commander/dependencies/yarn_workspace.rb