next_rails_scaffold 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 18755bfa18f40bbdb7ee12fc01dbe7ab74795d5474a3549422e70a773e219545
4
- data.tar.gz: a8fa5f99891d25e12c291541c0873e03dee707bdf44e39f93d6f1faaede8b500
3
+ metadata.gz: abd99131dfb6cd8584a12c366b039b743168e744b41f471c902596e10b77e138
4
+ data.tar.gz: 9523b8f173ba5373678d946497a7344e36afbd11505187153eeb79d2223e6540
5
5
  SHA512:
6
- metadata.gz: 160dfded6868506ab551eb3b02fa6ffc56d6b7d061095aee9ae432bdc42bff0fe1d0225988e12721d253ffd80b87f549874b7c751128d0831a51c6752c7f06aa
7
- data.tar.gz: 0e1bf40f9b3d107cecef54719756999cc4ca3916004aec2470eccae24a6e26be13db88c999f9a7ff7f2e1a6604bdc76075531218e72409d510fc51f4912a15ca
6
+ metadata.gz: d630cd3b07ce3c38b0411638def67bf59f5e2c1d79fbcc358a2ba681cf8318ca7bce8d8ef3a6b2f6929ec51c3019a49bdcbac8b36300f95e5c7c3fa1de311d0a
7
+ data.tar.gz: 49a4fb1332d41bfe302c91ab814177e748ff152fa4ed0f3ad43154d73a30e8137ee7ab3e96c6fc638ad48b5119f5e2973ef5380e139269b7f0f41717469c9c9f
@@ -1,56 +1,153 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Rails::NextRailsGenerator < Rails::Generators::NamedBase
4
- source_root File.expand_path("templates", __dir__)
3
+ module Rails
4
+ class NextRailsGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path("templates", __dir__)
5
6
 
6
- NODE_REQUIRED_VERSION = ">= 18.17.0"
7
- YARN_VERSION = "1.22.19"
8
- NEXT_VERSION = "14.0.2"
7
+ NODE_REQUIRED_VERSION = ">= 18.17.0"
8
+ YARN_VERSION = "1.22.19"
9
+ NEXT_VERSION = "14.0.2"
10
+ NODULES_MODULES = [
11
+ "@hookform/resolvers",
12
+ "@tanstack/react-query",
13
+ "axios",
14
+ "react-hook-form",
15
+ "zod"
16
+ ].freeze
9
17
 
10
- argument :attributes, type: :array, default: [], banner: "field:type field:type"
18
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
11
19
 
12
- def initialize(args, *options) # :nodoc:
13
- super
14
- self.attributes = shell.base.attributes
15
- end
20
+ def initialize(args, *options) # :nodoc:
21
+ super
22
+ self.attributes = shell.base.attributes
23
+ end
16
24
 
17
- def create_frontend_project
18
- node_version = run("node --version", capture: true).gsub(/[^0-9.]/, "")
25
+ # Properly nests namespaces passed into a generator
26
+ #
27
+ # $ bin/rails generate resource admin/users/products
28
+ #
29
+ # should give you
30
+ #
31
+ # scope '/api' do
32
+ # namespace :admin do
33
+ # namespace :users do
34
+ # resources :products
35
+ # end
36
+ # end
37
+ # end
38
+ def add_resource_route
39
+ return if options[:actions].present?
19
40
 
20
- if Gem::Dependency.new("", NODE_REQUIRED_VERSION).match?("", node_version)
21
- say "Your Node version is '#{node_version}'", :green
22
- else
23
- say_error "You need to have a Node version '#{NODE_REQUIRED_VERSION}'", :red
24
- abort
41
+ route "resources :#{file_name.pluralize}", namespace: regular_class_path, scope: "/api"
25
42
  end
26
43
 
27
- append_to_file ".gitignore", "\n# Ingoring node modules for Rails and Next.js projects\nnode_modules/\n"
28
- empty_directory "frontend"
44
+ # Check Javascript depencies and create a new Next.js project, install the the usefull packages and create the
45
+ # scaffold code for frontend application.
46
+ def create_frontend_project
47
+ check_node!
48
+ append_gitignore!
49
+
50
+ empty_directory "frontend"
51
+
52
+ inside("frontend") do
53
+ create_next_app!
29
54
 
30
- inside("frontend") do
31
- unless File.exist?("package.json")
32
- system("npm install --global yarn@#{YARN_VERSION}")
33
- system("yarn global add create-next-app@#{NEXT_VERSION}")
34
- run("yarn create next-app . --no-app --src-dir --import-alias \"@/*\"")
55
+ install_hygen!
56
+ install_dependencies!
57
+
58
+ run("npx hygen generate scaffold #{name} #{mapped_attributes.join(" ")}")
59
+ run("yarn build")
35
60
  end
61
+ end
62
+
63
+ private
64
+
65
+ def check_node!
66
+ node_version = run("node --version", capture: true).gsub(/[^0-9.]/, "")
36
67
 
37
- unless Dir.exist?("_templates")
38
- run("yarn add -D hygen hygen-add")
39
- run("npx hygen-add next-rails-scaffold")
68
+ if Gem::Dependency.new("", NODE_REQUIRED_VERSION).match?("", node_version)
69
+ say "Your Node version is '#{node_version}'", :green
70
+ else
71
+ say_error "You need to have a Node version '#{NODE_REQUIRED_VERSION}'", :red
72
+ abort
40
73
  end
74
+ end
75
+
76
+ def append_gitignore!
77
+ rows = <<~HEREDOC
41
78
 
42
- run("yarn add axios @tanstack/react-query zod react-hook-form @hookform/resolvers")
43
- run("npx hygen generate scaffold #{name} #{mapped_attributes.join(" ")}")
79
+ # Ingoring node modules for Rails and Next.js projects
80
+ node_modules/
81
+ HEREDOC
82
+
83
+ append_to_file ".gitignore", rows
44
84
  end
45
- end
46
85
 
47
- private
86
+ def create_next_app!
87
+ return if File.exist?("package.json")
48
88
 
49
- def mapped_attributes
50
- attributes.map { |attr| "#{attr.name}:#{attr.type}" }
51
- end
89
+ run("npm install --global yarn@#{YARN_VERSION}")
90
+ run("yarn global add create-next-app@#{NEXT_VERSION}")
91
+ run("yarn create next-app . --no-app --src-dir --import-alias \"@/*\"")
92
+ end
93
+
94
+ def install_hygen!
95
+ return if Dir.exist?("_templates")
96
+
97
+ run("yarn add -D hygen hygen-add")
98
+ run("npx hygen-add next-rails-scaffold")
99
+ end
100
+
101
+ def install_dependencies!
102
+ run("yarn add #{NODULES_MODULES.join(" ")}")
103
+ end
104
+
105
+ def mapped_attributes
106
+ attributes.map { |attr| "#{attr.name}:#{attr.type}" }
107
+ end
108
+
109
+ # Make an entry in \Rails routing file <tt>config/routes.rb</tt>
110
+ #
111
+ # route "root 'welcome#index'"
112
+ # route "root 'admin#index'", namespace: :admin
113
+ # route "root 'admin#index'", namespace: :admin, scope: '/api'
114
+ def route(routing_code, namespace: nil, scope: nil)
115
+ namespace = Array(namespace)
116
+ namespace_pattern = route_namespace_pattern(namespace)
117
+ routing_code = namespace.reverse.reduce(routing_code) do |code, name|
118
+ "namespace :#{name} do\n#{rebase_indentation(code, 2)}end"
119
+ end
120
+
121
+ scope = Array(scope)
122
+ routing_code = scope.reverse.reduce(routing_code) do |code, name|
123
+ "scope '#{name}' do\n#{rebase_indentation(code, 2)}end"
124
+ end
125
+
126
+ log :route, routing_code
127
+
128
+ in_root do
129
+ if namespace_match = match_file("config/routes.rb", namespace_pattern)
130
+ base_indent, *, existing_block_indent = namespace_match.captures.compact.map(&:length)
131
+ existing_line_pattern = /^ {,#{existing_block_indent}}\S.+\n?/
132
+ routing_code = rebase_indentation(routing_code, base_indent + 2).gsub(existing_line_pattern, "")
133
+ namespace_pattern = /#{Regexp.escape namespace_match.to_s}/
134
+ end
135
+
136
+ inject_into_file "config/routes.rb", routing_code, after: namespace_pattern, verbose: false, force: false
52
137
 
53
- def exit_on_failure?
54
- true
138
+ if behavior == :revoke && namespace.any? && namespace_match
139
+ empty_block_pattern = /(#{namespace_pattern})((?:\s*end\n){1,#{namespace.size}})/
140
+ gsub_file "config/routes.rb", empty_block_pattern, verbose: false, force: true do |matched|
141
+ beginning, ending = empty_block_pattern.match(matched).captures
142
+ ending.sub!(/\A\s*end\n/, "") while !ending.empty? && beginning.sub!(/^ *namespace .+ do\n\s*\z/, "")
143
+ beginning + ending
144
+ end
145
+ end
146
+ end
147
+ end
148
+
149
+ def exit_on_failure?
150
+ true
151
+ end
55
152
  end
56
153
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NextRails
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: next_rails_scaffold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raphael Araújo