11# frozen_string_literal: true
22
3+ PackageManager = Struct . new ( :fetch , :run , :add , :lock_file ) do
4+ def to_s = fetch
5+ end
6+
37module Rails
48 class NextRailsScaffoldGenerator < Rails ::Generators ::NamedBase
59 include ::NextRailsScaffold ::Actions
610
711 source_root File . expand_path ( "templates" , __dir__ )
812
9- NODE_REQUIRED_VERSION = ENV . fetch ( "NODE_REQUIRED_VERSION" , ">= 18.20.6" )
10- YARN_VERSION = ENV . fetch ( "YARN_VERSION" , "1.22.22" )
13+ PACKAGE_MANAGERS = {
14+ "npm" => PackageManager . new ( "npx" , "npm run" , "npm install" , "package-lock.json" ) ,
15+ "yarn" => PackageManager . new ( "yarn dlx" , "yarn" , "yarn add" , "yarn.lock" ) ,
16+ "pnpm" => PackageManager . new ( "pnpm dlx" , "pnpm" , "pnpm add" , "pnpm-lock.yaml" ) ,
17+ "bun" => PackageManager . new ( "bunx" , "bun run" , "bun add" , "bun.lock.json" )
18+ } . freeze
19+ NODE_REQUIRED_VERSION = ENV . fetch ( "NODE_REQUIRED_VERSION" , ">= 18.20" )
1120 NEXT_VERSION = ENV . fetch ( "NEXT_VERSION" , "15.1.6" )
1221
1322 argument :attributes , type : :array , default : [ ] , banner : "field:type field:type"
23+ class_option :package_manager , type : :string , desc : "Package manager to use for frontend project"
24+ class_option :skip_build , type : :boolean , default : false , desc : "Skip running Next.js build"
25+ class_option :skip_routes , type : :boolean , default : false , desc : "Skip adding resources to routes.rb"
26+
27+ attr_accessor :selected_package_manager
1428
1529 def initialize ( args , *options ) # :nodoc:
1630 super
31+
1732 self . attributes = shell . base . attributes
33+
34+ package_manager = shell . base . options [ :package_manager ]
35+ until PACKAGE_MANAGERS . keys . include? ( package_manager )
36+ puts "Invalid package manager" unless package_manager . nil?
37+ package_manager = ask (
38+ "Which package manager do you want to use? " \
39+ "(#{ PACKAGE_MANAGERS . keys . to_sentence ( words_connector : " or " ) } ): "
40+ )
41+ end
42+
43+ self . selected_package_manager = PACKAGE_MANAGERS [ package_manager ]
1844 end
1945
2046 # Properly nests namespaces passed into a generator
@@ -31,12 +57,12 @@ def initialize(args, *options) # :nodoc:
3157 # end
3258 # end
3359 def add_resource_route
34- return if options [ :actions ] . present?
60+ return if options [ :actions ] . present? || options [ :skip_routes ]
3561
3662 route "resources :#{ file_name . pluralize } " , namespace : regular_class_path , scope : "/api"
3763 end
3864
39- # Check Javascript depencies and create a new Next.js project. Install the the usefull packages and create the
65+ # Check Javascript dependencies and create a new Next.js project. Install the the useful packages and create the
4066 # scaffold code for frontend application.
4167 def create_frontend_project
4268 return say_status :remove , "skip frontend folder" , :yellow if shell . base . behavior == :revoke
@@ -52,8 +78,10 @@ def create_frontend_project
5278
5379 language = File . exist? ( "tsconfig.json" ) ? "typescript" : "javascript"
5480
55- run ( "npx hygen scaffold #{ language } #{ name } #{ mapped_attributes . join ( " " ) } " )
56- run ( "yarn build" )
81+ run ( "#{ selected_package_manager } hygen scaffold #{ language } #{ name } #{ mapped_attributes . join ( " " ) } " )
82+ if !options [ :skip_build ] && yes? ( "Do you want to build your Next.js project? (y/N)" )
83+ run ( "#{ selected_package_manager . run } build" )
84+ end
5785 end
5886 end
5987
@@ -83,16 +111,17 @@ def append_gitignore!
83111 def create_next_app!
84112 return if File . exist? ( "package.json" )
85113
86- run ( "npm install --global yarn@#{ YARN_VERSION } " )
87- run ( "yarn global add create-next-app@#{ NEXT_VERSION } " )
88- run ( "yarn create next-app . --no-app --src-dir --import-alias \" @/*\" " )
114+ run (
115+ "#{ selected_package_manager . fetch } create-next-app@#{ NEXT_VERSION } . " \
116+ "--no-app --src-dir --import-alias \" @/*\" "
117+ )
89118 end
90119
91120 def install_hygen!
92121 return if Dir . exist? ( "_templates" )
93122
94- run ( "yarn add -D hygen hygen-add" )
95- run ( "npx hygen-add next-rails-scaffold" )
123+ run ( "#{ selected_package_manager . add } -D hygen hygen-add" )
124+ run ( "#{ selected_package_manager } hygen-add next-rails-scaffold" )
96125 end
97126
98127 def mapped_attributes
0 commit comments