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