|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require 'json'
|
2 | 4 |
|
3 | 5 | module Helpers
|
| 6 | + TOOLS_COMMANDS = { |
| 7 | + yarn: { cmd: 'yarn', run: 'yarn', x: 'npx' }, |
| 8 | + bun: { cmd: 'bun', run: 'bun run', x: 'bunx' } |
| 9 | + pnpm: { cmd: 'pnpm', run: 'pnpm run', x: 'pnpx' }, |
| 10 | + npm: { cmd: 'npm', run: 'npm run', x: 'npx' }, |
| 11 | + }.freeze |
| 12 | + |
| 13 | + SUPPORTED_TOOLS = TOOLS_COMMANDS.keys.freeze |
| 14 | + DEFAULT_TOOL = :yarn |
| 15 | + |
| 16 | + SCRIPT_STRATEGIES = { |
| 17 | + yarn: lambda do |helper, name, script, run_script| |
| 18 | + npx_version = `npx -v`.to_f |
| 19 | + |
| 20 | + if npx_version >= 7.1 && npx_version < 8.0 |
| 21 | + helper.say "Add #{name} script" |
| 22 | + helper.run %(npm set-script #{name} "#{script}") |
| 23 | + helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script |
| 24 | + elsif npx_version >= 8.0 |
| 25 | + helper.say "Add #{name} script" |
| 26 | + helper.run %(npm pkg set scripts.#{name}="#{script}") |
| 27 | + helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script |
| 28 | + else |
| 29 | + helper.say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green |
| 30 | + end |
| 31 | + end, |
| 32 | + |
| 33 | + bun: lambda do |helper, name, script, run_script| |
| 34 | + package_json = JSON.parse(File.read("package.json")) |
| 35 | + package_json["scripts"] ||= {} |
| 36 | + package_json["scripts"][name] = script.gsub('\\"', '"') |
| 37 | + File.write("package.json", JSON.pretty_generate(package_json)) |
| 38 | + helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script |
| 39 | + end, |
| 40 | + |
| 41 | + pnpm: lambda do |helper, name, script, run_script| |
| 42 | + helper.say "Add #{name} script" |
| 43 | + helper.run %(pnpm pkg set scripts.#{name}="#{script}") |
| 44 | + helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script |
| 45 | + end, |
| 46 | + |
| 47 | + npm: lambda do |helper, name, script, run_script| |
| 48 | + helper.say "Add #{name} script" |
| 49 | + npx_version = `npx -v`.to_f |
| 50 | + |
| 51 | + if npx_version >= 7.1 && npx_version < 8.0 |
| 52 | + helper.run %(npm set-script #{name} "#{script}") |
| 53 | + else |
| 54 | + helper.run %(npm pkg set scripts.#{name}="#{script}") |
| 55 | + end |
| 56 | + |
| 57 | + helper.run %(#{helper.bundler_run_cmd} #{name}) if run_script |
| 58 | + end |
| 59 | + }.freeze |
| 60 | + |
4 | 61 | def bundler_cmd
|
5 |
| - using_bun? ? "bun" : "yarn" |
| 62 | + TOOLS_COMMANDS.dig(package_manager, :cmd) |
6 | 63 | end
|
7 | 64 |
|
8 | 65 | def bundler_run_cmd
|
9 |
| - using_bun? ? "bun run" : "yarn" |
| 66 | + TOOLS_COMMANDS.dig(package_manager, :run) |
10 | 67 | end
|
11 | 68 |
|
12 | 69 | def bundler_x_cmd
|
13 |
| - using_bun? ? "bunx" : "npx" |
| 70 | + TOOLS_COMMANDS.dig(package_manager, :x) |
14 | 71 | end
|
15 | 72 |
|
16 | 73 | def using_bun?
|
17 |
| - tool_exists?('bun') && (File.exist?('bun.lockb') || |
18 |
| - File.exist?('bun.lock') || |
19 |
| - File.exist?('yarn.lock')) |
| 74 | + package_manager == :bun |
| 75 | + end |
| 76 | + |
| 77 | + def package_manager |
| 78 | + @package_manager ||= tool_determined_by_config_file || tool_determined_by_executable || DEFAULT_TOOL |
20 | 79 | end
|
21 | 80 |
|
| 81 | + def add_package_json_script(name, script, run_script: true) |
| 82 | + SCRIPT_STRATEGIES[package_manager].call(self, name, script, run_script) |
| 83 | + end |
| 84 | + |
| 85 | + private |
| 86 | + |
22 | 87 | def tool_exists?(tool)
|
23 | 88 | system "command -v #{tool} > /dev/null"
|
24 | 89 | end
|
25 | 90 |
|
26 |
| - def add_package_json_script(name, script, run_script=true) |
27 |
| - if using_bun? |
28 |
| - package_json = JSON.parse(File.read("package.json")) |
29 |
| - package_json["scripts"] ||= {} |
30 |
| - package_json["scripts"][name] = script.gsub('\\"', '"') |
31 |
| - File.write("package.json", JSON.pretty_generate(package_json)) |
32 |
| - run %(bun run #{name}) if run_script |
33 |
| - else |
34 |
| - case `npx -v`.to_f |
35 |
| - when 7.1...8.0 |
36 |
| - say "Add #{name} script" |
37 |
| - run %(npm set-script #{name} "#{script}") |
38 |
| - run %(yarn #{name}) if run_script |
39 |
| - when (8.0..) |
40 |
| - say "Add #{name} script" |
41 |
| - run %(npm pkg set scripts.#{name}="#{script}") |
42 |
| - run %(yarn #{name}) if run_script |
43 |
| - else |
44 |
| - say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green |
45 |
| - end |
| 91 | + def tool_determined_by_config_file |
| 92 | + case |
| 93 | + when File.exist?("yarn.lock") then :yarn |
| 94 | + when File.exist?("bun.lockb") then :bun |
| 95 | + when File.exist?("bun.lock") then :bun |
| 96 | + when File.exist?("pnpm-lock.yaml") then :pnpm |
| 97 | + when File.exist?("package-lock.json") then :npm |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + def tool_determined_by_executable |
| 102 | + SUPPORTED_TOOLS.keys.each do |tool| |
| 103 | + return tool if tool_exists?(tool) |
46 | 104 | end
|
47 | 105 | end
|
48 | 106 | end
|
0 commit comments