Skip to content

Commit 507fae0

Browse files
Move Importmap::Update to ImportmapUpdate (#17)
We don't want to touch the `Importmap` namespace.
1 parent f33ae5d commit 507fae0

24 files changed

Lines changed: 1202 additions & 1226 deletions

exe/importmap-update

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ OptionParser.new do |opts|
4747
end.parse!
4848

4949
begin
50-
config = Importmap::Update::Config.load(options[:config_path])
51-
rescue Importmap::Update::Config::ConfigError => e
50+
config = ImportmapUpdate::Config.load(options[:config_path])
51+
rescue ImportmapUpdate::Config::ConfigError => e
5252
warn "Config error: #{e.message}"
5353
exit 2
5454
end
@@ -66,24 +66,24 @@ if token.nil? || token.empty?
6666
end
6767

6868
rails_root = ENV.fetch("RAILS_ROOT", ".")
69-
runner = Importmap::Update::Commands::ShellRunner.new(cwd: rails_root)
70-
gh = Importmap::Update::GitHubClient.new(repo:, token:)
71-
git = Importmap::Update::GitClient.new(
69+
runner = ImportmapUpdate::Commands::ShellRunner.new(cwd: rails_root)
70+
gh = ImportmapUpdate::GitHubClient.new(repo:, token:)
71+
git = ImportmapUpdate::GitClient.new(
7272
repo: Git.open(rails_root),
7373
author_name: ENV.fetch("IMPORTMAP_AUTHOR_NAME", "github-actions[bot]"),
7474
author_email: ENV.fetch("IMPORTMAP_AUTHOR_EMAIL", "github-actions[bot]@users.noreply.github.com")
7575
)
7676

77-
outdated = Importmap::Update::Parsers::OutdatedParser.parse(ENV.fetch("IMPORTMAP_OUTDATED_OUTPUT"))
78-
vulnerabilities = Importmap::Update::Parsers::AuditParser.parse(ENV.fetch("IMPORTMAP_AUDIT_OUTPUT"))
79-
plan = Importmap::Update::Planner.new(
77+
outdated = ImportmapUpdate::Parsers::OutdatedParser.parse(ENV.fetch("IMPORTMAP_OUTDATED_OUTPUT"))
78+
vulnerabilities = ImportmapUpdate::Parsers::AuditParser.parse(ENV.fetch("IMPORTMAP_AUDIT_OUTPUT"))
79+
plan = ImportmapUpdate::Planner.new(
8080
outdated:, vulnerabilities:, config:
8181
).call
8282

8383
existing_prs = gh.list_open_prs(branch_prefix: config.branch_prefix)
84-
reconciled = Importmap::Update::Reconciler.new(plan:, existing_prs:).call
84+
reconciled = ImportmapUpdate::Reconciler.new(plan:, existing_prs:).call
8585

86-
executor = Importmap::Update::Executor.new(
86+
executor = ImportmapUpdate::Executor.new(
8787
gh:, git:, runner:,
8888
base_branch: ENV.fetch("IMPORTMAP_BASE_BRANCH", "main"),
8989
commit_message_prefix: config.commit_message.prefix,

lib/commands.rb

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,59 @@
33
require "open3"
44
require "json"
55

6-
module Importmap
7-
module Update
8-
# Abstracts execution of external commands (bin/importmap) so the rest
9-
# of the codebase doesn't shell out directly. This is the seam tests hook
10-
# into — production code runs commands for real, tests inject a
11-
# FixtureRunner that replays pre-recorded (argv → stdout, exit) tuples.
12-
#
13-
# The interface deliberately mirrors what Open3.capture3 returns:
14-
#
15-
# runner.run("bin/importmap", "outdated")
16-
# # => Result(stdout: "...", stderr: "...", success: true, exit: 0)
17-
#
18-
# Commands are passed as an argv array, not a shell string. That's both
19-
# safer (no shell-injection surprises from package names) and easier to
20-
# match against fixture keys.
21-
module Commands
22-
Result = Data.define(:stdout, :stderr, :exit_code) do
23-
def success?
24-
exit_code == 0
25-
end
6+
module ImportmapUpdate
7+
# Abstracts execution of external commands (bin/importmap) so the rest
8+
# of the codebase doesn't shell out directly. This is the seam tests hook
9+
# into — production code runs commands for real, tests inject a
10+
# FixtureRunner that replays pre-recorded (argv → stdout, exit) tuples.
11+
#
12+
# The interface deliberately mirrors what Open3.capture3 returns:
13+
#
14+
# runner.run("bin/importmap", "outdated")
15+
# # => Result(stdout: "...", stderr: "...", success: true, exit: 0)
16+
#
17+
# Commands are passed as an argv array, not a shell string. That's both
18+
# safer (no shell-injection surprises from package names) and easier to
19+
# match against fixture keys.
20+
module Commands
21+
Result = Data.define(:stdout, :stderr, :exit_code) do
22+
def success?
23+
exit_code == 0
2624
end
25+
end
2726

28-
class CommandError < StandardError
29-
attr_reader :argv, :result
27+
class CommandError < StandardError
28+
attr_reader :argv, :result
3029

31-
def initialize(argv, result)
32-
@argv = argv
33-
@result = result
34-
super("`#{argv.join(" ")}` exited #{result.exit_code}: #{result.stderr.strip}")
35-
end
30+
def initialize(argv, result)
31+
@argv = argv
32+
@result = result
33+
super("`#{argv.join(" ")}` exited #{result.exit_code}: #{result.stderr.strip}")
3634
end
35+
end
3736

38-
# Production runner: actually executes the command.
39-
class ShellRunner
40-
# @param cwd [String, nil] working directory (defaults to current)
41-
def initialize(cwd: nil)
42-
@cwd = cwd
43-
end
37+
# Production runner: actually executes the command.
38+
class ShellRunner
39+
# @param cwd [String, nil] working directory (defaults to current)
40+
def initialize(cwd: nil)
41+
@cwd = cwd
42+
end
4443

45-
def run(*argv)
46-
opts = {}
47-
opts[:chdir] = @cwd if @cwd
48-
Bundler.with_unbundled_env do
49-
stdout, stderr, status = Open3.capture3(*argv, opts)
50-
Result.new(stdout:, stderr:, exit_code: status.exitstatus)
51-
end
44+
def run(*argv)
45+
opts = {}
46+
opts[:chdir] = @cwd if @cwd
47+
Bundler.with_unbundled_env do
48+
stdout, stderr, status = Open3.capture3(*argv, opts)
49+
Result.new(stdout:, stderr:, exit_code: status.exitstatus)
5250
end
51+
end
5352

54-
# Raises on non-zero exit. Use when you have no recovery strategy
55-
# and just want to surface the error to the caller.
56-
def run!(*argv)
57-
result = run(*argv)
58-
raise CommandError.new(argv, result) unless result.success?
59-
result
60-
end
53+
# Raises on non-zero exit. Use when you have no recovery strategy
54+
# and just want to surface the error to the caller.
55+
def run!(*argv)
56+
result = run(*argv)
57+
raise CommandError.new(argv, result) unless result.success?
58+
result
6159
end
6260
end
6361
end

0 commit comments

Comments
 (0)