next_rails_scaffold 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/rails/next_rails/next_rails_generator.rb +133 -36
- data/lib/next_rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abd99131dfb6cd8584a12c366b039b743168e744b41f471c902596e10b77e138
|
4
|
+
data.tar.gz: 9523b8f173ba5373678d946497a7344e36afbd11505187153eeb79d2223e6540
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d630cd3b07ce3c38b0411638def67bf59f5e2c1d79fbcc358a2ba681cf8318ca7bce8d8ef3a6b2f6929ec51c3019a49bdcbac8b36300f95e5c7c3fa1de311d0a
|
7
|
+
data.tar.gz: 49a4fb1332d41bfe302c91ab814177e748ff152fa4ed0f3ad43154d73a30e8137ee7ab3e96c6fc638ad48b5119f5e2973ef5380e139269b7f0f41717469c9c9f
|
@@ -1,56 +1,153 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module Rails
|
4
|
+
class NextRailsGenerator < Rails::Generators::NamedBase
|
5
|
+
source_root File.expand_path("templates", __dir__)
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
18
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
11
19
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
20
|
+
def initialize(args, *options) # :nodoc:
|
21
|
+
super
|
22
|
+
self.attributes = shell.base.attributes
|
23
|
+
end
|
16
24
|
|
17
|
-
|
18
|
-
|
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
|
-
|
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
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
run("yarn
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
43
|
-
|
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
|
-
|
86
|
+
def create_next_app!
|
87
|
+
return if File.exist?("package.json")
|
48
88
|
|
49
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
54
|
-
|
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
|
data/lib/next_rails/version.rb
CHANGED