next_rails_scaffold 0.1.1 → 0.1.3

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: 6c927e1fa3e4aedbe0f200329d7940ad2762339b18a40cab4c9ab96e5d3ccf37
4
+ data.tar.gz: 48aef6d4c24f14d83ac02b762e27f74f1536051daf101d2eabc23ee064d13cbb
5
5
  SHA512:
6
- metadata.gz: 160dfded6868506ab551eb3b02fa6ffc56d6b7d061095aee9ae432bdc42bff0fe1d0225988e12721d253ffd80b87f549874b7c751128d0831a51c6752c7f06aa
7
- data.tar.gz: 0e1bf40f9b3d107cecef54719756999cc4ca3916004aec2470eccae24a6e26be13db88c999f9a7ff7f2e1a6604bdc76075531218e72409d510fc51f4912a15ca
6
+ metadata.gz: ad57f6aaddc7318aaa6d6fe1a64001475813e267207a287c2645cd0345690f512d7987801f54102b95005aa9f9b47072f170d83718e72b548ae36b02187a9be8
7
+ data.tar.gz: 93bf210d3f8a2f9fa7387f1f3fc64bd3dba00aae2df3c7710ce3700145222f97e3687820011506c9e852dcf21fb3dbe55f208da4714f55ba0e18a3daa62f41ac
@@ -4,6 +4,8 @@ require "next_rails"
4
4
 
5
5
  NextRails.setup do |config|
6
6
  config.generators do |g|
7
+ g.api_only = true
8
+ g.resource_route false
7
9
  g.helper :next_rails
8
10
  end
9
11
  end
@@ -1,56 +1,116 @@
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
+ include NextRails::Actions
5
5
 
6
- NODE_REQUIRED_VERSION = ">= 18.17.0"
7
- YARN_VERSION = "1.22.19"
8
- NEXT_VERSION = "14.0.2"
6
+ class NextRailsGenerator < Rails::Generators::NamedBase
7
+ source_root File.expand_path("templates", __dir__)
9
8
 
10
- argument :attributes, type: :array, default: [], banner: "field:type field:type"
9
+ NODE_REQUIRED_VERSION = ">= 18.17.0"
10
+ YARN_VERSION = "1.22.19"
11
+ NEXT_VERSION = "14.0.2"
12
+ NODULES_MODULES = [
13
+ "@hookform/resolvers",
14
+ "@tanstack/react-query",
15
+ "axios",
16
+ "react-hook-form",
17
+ "zod"
18
+ ].freeze
11
19
 
12
- def initialize(args, *options) # :nodoc:
13
- super
14
- self.attributes = shell.base.attributes
15
- end
20
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
21
+
22
+ def initialize(args, *options) # :nodoc:
23
+ super
24
+ self.attributes = shell.base.attributes
25
+ end
16
26
 
17
- def create_frontend_project
18
- node_version = run("node --version", capture: true).gsub(/[^0-9.]/, "")
27
+ # Properly nests namespaces passed into a generator
28
+ #
29
+ # $ bin/rails generate resource admin/users/products
30
+ #
31
+ # should give you
32
+ #
33
+ # scope '/api' do
34
+ # namespace :admin do
35
+ # namespace :users do
36
+ # resources :products
37
+ # end
38
+ # end
39
+ # end
40
+ def add_resource_route
41
+ return if options[:actions].present?
19
42
 
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
43
+ route "resources :#{file_name.pluralize}", namespace: regular_class_path, scope: "/api"
25
44
  end
26
45
 
27
- append_to_file ".gitignore", "\n# Ingoring node modules for Rails and Next.js projects\nnode_modules/\n"
28
- empty_directory "frontend"
46
+ # Check Javascript depencies and create a new Next.js project. Install the the usefull packages and create the
47
+ # scaffold code for frontend application.
48
+ def create_frontend_project
49
+ return say_status :remove, "skip frontend folder", :yellow if shell.base.behavior == :revoke
50
+
51
+ check_node!
52
+ append_gitignore!
53
+
54
+ empty_directory "frontend"
55
+
56
+ inside("frontend") do
57
+ create_next_app!
58
+ install_dependencies!
29
59
 
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 \"@/*\"")
60
+ run("npx hygen generate scaffold #{name} #{mapped_attributes.join(" ")}")
61
+ run("yarn build")
35
62
  end
63
+ end
64
+
65
+ private
66
+
67
+ def check_node!
68
+ node_version = run("node --version", capture: true).gsub(/[^0-9.]/, "")
36
69
 
37
- unless Dir.exist?("_templates")
38
- run("yarn add -D hygen hygen-add")
39
- run("npx hygen-add next-rails-scaffold")
70
+ if Gem::Dependency.new("", NODE_REQUIRED_VERSION).match?("", node_version)
71
+ log :node_version, "Your Node version is '#{node_version}'"
72
+ else
73
+ say_status :node_version, "You need to have a Node version '#{NODE_REQUIRED_VERSION}'", :red
74
+ abort
40
75
  end
76
+ end
77
+
78
+ def append_gitignore!
79
+ rows = <<~HEREDOC
80
+
81
+ # Ingoring node modules for Rails and Next.js projects
82
+ node_modules/
83
+ HEREDOC
41
84
 
42
- run("yarn add axios @tanstack/react-query zod react-hook-form @hookform/resolvers")
43
- run("npx hygen generate scaffold #{name} #{mapped_attributes.join(" ")}")
85
+ append_to_file ".gitignore", rows
44
86
  end
45
- end
46
87
 
47
- private
88
+ def create_next_app!
89
+ return if File.exist?("package.json")
48
90
 
49
- def mapped_attributes
50
- attributes.map { |attr| "#{attr.name}:#{attr.type}" }
51
- end
91
+ run("npm install --global yarn@#{YARN_VERSION}")
92
+ run("yarn global add create-next-app@#{NEXT_VERSION}")
93
+ run("yarn create next-app . --no-app --src-dir --import-alias \"@/*\"")
94
+ end
95
+
96
+ def install_hygen!
97
+ return if Dir.exist?("_templates")
98
+
99
+ run("yarn add -D hygen hygen-add")
100
+ run("npx hygen-add next-rails-scaffold")
101
+ end
52
102
 
53
- def exit_on_failure?
54
- true
103
+ def install_dependencies!
104
+ install_hygen!
105
+ run("yarn add #{NODULES_MODULES.join(" ")}")
106
+ end
107
+
108
+ def mapped_attributes
109
+ attributes.map { |attr| "#{attr.name}:#{attr.type}" }
110
+ end
111
+
112
+ def exit_on_failure?
113
+ true
114
+ end
55
115
  end
56
116
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NextRails
4
+ # Make an entry in Rails routing file <tt>config/routes.rb</tt>
5
+ #
6
+ # route "root 'welcome#index'"
7
+ # route "root 'admin#index'", namespace: :admin
8
+ # route "root 'admin#index'", namespace: :admin, scope: '/api'
9
+ def route(routing_code, namespace: nil, scope: nil)
10
+ namespace = Array(namespace)
11
+ namespace_pattern = route_namespace_pattern(namespace)
12
+ routing_code = namespace.reverse.reduce(routing_code) do |code, name|
13
+ "namespace :#{name} do\n#{rebase_indentation(code, 2)}end"
14
+ end
15
+
16
+ scope = Array(scope)
17
+ routing_code = scope.reverse.reduce(routing_code) do |code, name|
18
+ "scope '#{name}' do\n#{rebase_indentation(code, 2)}end"
19
+ end
20
+
21
+ log :route, routing_code
22
+
23
+ in_root do
24
+ if (namespace_match = match_file("config/routes.rb", namespace_pattern))
25
+ base_indent, *, existing_block_indent = namespace_match.captures.compact.map(&:length)
26
+ existing_line_pattern = /^ {,#{existing_block_indent}}\S.+\n?/
27
+ routing_code = rebase_indentation(routing_code, base_indent + 2).gsub(existing_line_pattern, "")
28
+ namespace_pattern = /#{Regexp.escape namespace_match.to_s}/
29
+ end
30
+
31
+ inject_into_file "config/routes.rb", routing_code, after: namespace_pattern, verbose: false, force: false
32
+
33
+ if behavior == :revoke && namespace.any? && namespace_match
34
+ empty_block_pattern = /(#{namespace_pattern})((?:\s*end\n){1,#{namespace.size}})/
35
+ gsub_file "config/routes.rb", empty_block_pattern, verbose: false, force: true do |matched|
36
+ beginning, ending = empty_block_pattern.match(matched).captures
37
+ ending.sub!(/\A\s*end\n/, "") while !ending.empty? && beginning.sub!(/^ *namespace .+ do\n\s*\z/, "")
38
+ beginning + ending
39
+ end
40
+ end
41
+ end
42
+ end
43
+ 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.3"
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raphael Araújo
@@ -55,6 +55,7 @@ files:
55
55
  - lib/generators/next_rails/templates/config/initializers/next_rails.rb
56
56
  - lib/generators/rails/next_rails/next_rails_generator.rb
57
57
  - lib/next_rails.rb
58
+ - lib/next_rails/actions.rb
58
59
  - lib/next_rails/version.rb
59
60
  - sig/next_rails.rbs
60
61
  homepage: https://github.com/raphox/next-rails#readme