Skip to content

Commit

Permalink
Load yarn packages without reading their package files
Browse files Browse the repository at this point in the history
  • Loading branch information
xjunior committed Aug 13, 2022
1 parent 37411bf commit 8d5c86b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 114 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ Rails:

Gemspec/RequireMFA:
Enabled: false

Style/ClassAndModuleChildren:
Enabled: false
2 changes: 1 addition & 1 deletion lib/cobra_commander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module CobraCommander

def self.umbrella(root_path, yarn: false, bundler: false, name: UMBRELLA_APP_NAME)
umbrella = Umbrella.new(name, root_path)
umbrella.add_source(:yarn, Dependencies::YarnWorkspace.new(root_path)) unless bundler
umbrella.add_source(:yarn, Dependencies::Yarn.new(root_path)) unless bundler
umbrella.add_source(:bundler, Dependencies::Bundler.new(root_path)) unless yarn
umbrella
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cobra_commander/dependencies.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true

require_relative "dependencies/yarn_workspace"
require_relative "dependencies/yarn"
require_relative "dependencies/bundler"
49 changes: 49 additions & 0 deletions lib/cobra_commander/dependencies/yarn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require "json"
require "open3"

module CobraCommander
module Dependencies
# Yarn workspace components source for an umbrella
class Yarn
autoload :Package, "cobra_commander/dependencies/yarn/package"

def initialize(root_path)
@root_path = root_path
end

def path
@root_path
end

def dependencies
packages.map(&:name)
end

def components
@components ||= packages.map do |package|
{
path: package.path,
name: package.name,
dependencies: package.dependencies,
}
end
end

private

def packages
@packages ||= begin
output, = Open3.capture2("yarn workspaces --json info", chdir: @root_path)
JSON.parse(JSON.parse(output)["data"]).map do |name, spec|
Package.new(
File.join(@root_path, spec["location"]),
name, spec["workspaceDependencies"]
)
end
end
end
end
end
end
35 changes: 10 additions & 25 deletions lib/cobra_commander/dependencies/yarn/package.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
# frozen_string_literal: true

require "json"
require "pathname"

module CobraCommander
module Dependencies
module Yarn
# Represents an Yarn package.json file
class Package
attr_reader :path
class Yarn::Package
attr_reader :path, :name, :dependencies

def initialize(path)
@path = ::Pathname.new(File.join(path, "package.json")).realpath
end

def project_tag
name.match(%r{^@[\w-]+/}).to_s
end

def name
json["name"]
end

def dependencies
json.fetch("dependencies", {})
.merge(json.fetch("devDependencies", {}))
end
def initialize(path, name, dependencies)
@path = ::Pathname.new(File.join(path, "package.json")).realpath
@name = untag(name)
@dependencies = dependencies.map { |dep| untag(dep) }
end

private
private

def json
@json ||= JSON.parse(File.read(@path))
end
def untag(name)
name.gsub(%r{^@[\w-]+/}, "")
end
end
end
Expand Down
32 changes: 0 additions & 32 deletions lib/cobra_commander/dependencies/yarn/package_repo.rb

This file was deleted.

55 changes: 0 additions & 55 deletions lib/cobra_commander/dependencies/yarn_workspace.rb

This file was deleted.

0 comments on commit 8d5c86b

Please sign in to comment.