-
Notifications
You must be signed in to change notification settings - Fork 5
/
elm-rails-graphql-generator.rb
162 lines (135 loc) · 4.68 KB
/
elm-rails-graphql-generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ruby
#!/usr/bin/env ruby
require 'optparse'
require 'io/console'
require_relative 'wait_for_it'
require_relative 'utils'
require_relative 'elm_graphql_administrator'
options = {}
elm_boiler_plate = File.read('boiler_plate.elm')
abort = false
OptionParser.new do |parser|
parser.on('-n', '--name NAME', 'The name of your project') do |name|
options[:name] = name
end
parser.on('-p', '--path PATH', 'The path of your project') do |path|
options[:path] = path
Dir.mkdir options[:path] unless Dir.exist?(options[:path])
end
end.parse!
clear_console
Dir.chdir options[:path] unless options[:path].blank?
loop do
if options[:name].blank?
puts 'What is the name of your project?'
options[:name] = gets.chomp
end
options[:name] = to_valid_file_name options[:name]
if File.exist?(options[:name])
clear_console
puts "The directory #{options[:name]} already exists"
puts "in : #{options[:path]}" unless options[:path].blank?
options[:name] = nil
next
end
puts 'The directory created will be ' + options[:name]
puts 'Is that what you want? Type Y for yes, N for no, A for abort'
case yesno
when 't' then break
when 'f' then
clear_console
puts 'Old name : ' + options[:name]
options[:name] = nil
when 'a' then
abort = true
break
else raise 'A problem occured, please try launching the script again'
end
end
if abort
puts '...Aborting generation...'
return
end
clear_console
show_and_do("Generating #{options[:name]} api..") do
Dir.mkdir options[:name]
Dir.chdir options[:name]
system("rails new #{options[:name]}-api --api --database=postgresql &> /dev/null")
end
show_and_do('Adding graphql, graphql-rails-api and rack-cors to the Gemfile...') do
Dir.chdir options[:name] + '-api'
system('bundle add graphql --skip-install &> /dev/null')
system('bundle add graphql-rails-api --skip-install &> /dev/null')
system('bundle add rack-cors &> /dev/null')
end
show_and_do('Creating database...') do
system('rails db:create &> /dev/null')
end
show_and_do('Installing graphql-rails-api...') do
system('spring stop &> /dev/null')
system('rails generate graphql_rails_api:install &> /dev/null')
end
show_and_do('Configuring cors (Cross-Origin Resource Sharing)...') do
cors_content =
%(Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: %i[get post options]
end
end)
File.open('config/initializers/cors.rb', 'a+') { |f| f.write(cors_content) }
end
show_and_do('Launch rails server on port 3123...') do
WaitForIt.new('rails s -p 3123', wait_for: 'Listening on tcp')
end
show_and_do("Generating #{options[:name]} front in elm...") do
Dir.mkdir "../#{options[:name]}-front"
Dir.chdir "../#{options[:name]}-front"
system("printf 'y' | elm init &> /dev/null")
end
show_and_do('Installing dillonkearns/elm-graphql...') do
system("printf 'y' | elm install dillonkearns/elm-graphql &> /dev/null")
system("printf 'y' | elm install elm/json &> /dev/null")
end
show_and_do('Installing elm-athlete/athlete...') do
system("printf 'y' | elm install elm-athlete/athlete &> /dev/null")
system("printf 'y' | elm install elm/time &> /dev/null")
system("printf 'y' | elm install elm/url &> /dev/null")
end
show_and_do('Installing dillonkearns/elm-graphql CLI...') do
system('npm install --save-dev @dillonkearns/elm-graphql &> /dev/null')
end
show_and_do('Installing elm-live CLI...') do
system('npm install --save-dev elm-live@next &> /dev/null')
end
camelname = camelcase options[:name]
show_and_do('Configuring package.json...') do
elm_package_content =
%({
"name": "#{options[:name]}",
"version": "1.0.0",
"scripts": {
"api": "elm-graphql http://localhost:3000/graphql --base #{camelname}",
"rails-graphql-api": "elm-graphql http://localhost:3123/graphql --base #{camelname}",
"live": "elm-live src/Main.elm -u --open",
"lived": "elm-live src/Main.elm -u --open -- --debug"
}
})
File.open('package.json', 'w') { |f| f.write(elm_package_content) }
end
show_and_do('Generating elm with dillonkearns/elm-graphql...') do
system('npm run rails-graphql-api &> /dev/null')
end
show_and_do('Generating admin...') do
generate_admin
end
show_and_do('Copying boiler_plate.elm...') do
File.open('src/Main.elm', 'w') { |f| f.write(elm_boiler_plate) }
end
show_and_do('Stopping rails server on port 3123...') do
system("lsof -i :3123 -sTCP:LISTEN | awk 'NR > 1 {print $2}' | xargs kill -9 &> /dev/null")
end
puts "\nSuccessful installation!".green
puts 'You can now, run your rails server and front server:'.green
puts ' rails s'.yellow + " in #{options[:name]}-api".green
puts ' npm run live'.yellow + " in #{options[:name]}-front".green