Class: Greeenboii::WebsiteBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/greeenboii.rb

Class Method Summary collapse

Class Method Details

.build_websiteObject



25
26
27
28
29
30
31
32
33
# File 'lib/greeenboii.rb', line 25

def self.build_website
  CLI::UI::Frame.open("{{v}} Website Builder") do
    CLI::UI::Prompt.ask("Choose a template:") do |handler|
      handler.option("{{green:NextJs-CoolStack}}") { |selection| create_nextjs(selection) }
      handler.option("{{yellow:Hono-API}}") { |selection| create_hono(selection) }
      # handler.option("{{blue:Rails}}") { |selection| create_rails(selection) }

    end
  end
end

.create_hono(_selection) ⇒ Object



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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/greeenboii.rb', line 136

def self.create_hono(_selection)
  # Ask for a relative path instead

  location = CLI::UI.ask("Where to install? (relative path)", default: "hono-api")
  CLI::UI.puts("Checking prerequisites...")

  # Check prerequisites

  node_version = begin
    `node -v`.strip
  rescue StandardError
    nil
  end
  unless node_version
    CLI::UI.puts(CLI::UI.fmt("{{x}} Node.js not found. Please install Node.js first"))
    return
  end

  git_version = begin
    `git --version`.strip
  rescue StandardError
    nil
  end
  unless git_version
    CLI::UI.puts(CLI::UI.fmt("{{x}} Git not found. Please install Git first"))
    return
  end

  # Check for Deno

  deno_version = begin
    `deno --version`.strip
  rescue StandardError
    nil
  end
  unless deno_version
    CLI::UI.puts(CLI::UI.fmt("{{x}} Deno not found. Please install Deno first"))
    return
  end

  # Create a path relative to current directory

  full_path = File.expand_path(location, Dir.pwd)

  # Check if directory already exists

  if Dir.exist?(full_path)
    # If directory exists, check if it's empty

    unless Dir.empty?(full_path) # . and .. entries

      CLI::UI.puts(CLI::UI.fmt("{{x}} Directory not empty: #{full_path}"))
      return
    end
  else
    # Try to create the directory

    begin
      FileUtils.mkdir_p(full_path)
    rescue StandardError => e
      CLI::UI.puts(CLI::UI.fmt("{{x}} Cannot create directory: #{e.message}"))
      return
    end
  end

  success = false
  CLI::UI::Spinner.spin("Setting up Hono Deno backend template") do |spinner|
    spinner.update_title("Cloning repository...")

    # Clone directly into the target directory

    clone_cmd = "git clone https://github.com/greeenboi/hono-deno-backend-template.git \"#{full_path}\""
    result = system(clone_cmd)

    unless result
      # Try alternative approach if direct cloning fails

      spinner.update_title("Direct clone failed, trying alternative...")

      # Clone to a temporary directory first

      temp_dir = "#{Dir.pwd}/temp_clone_#{Time.now.to_i}"
      FileUtils.mkdir_p(temp_dir)
      result = system("git clone https://github.com/greeenboi/hono-deno-backend-template.git \"#{temp_dir}\"")

      if result
        # Copy files to destination

        FileUtils.cp_r(Dir.glob("#{temp_dir}/*"), full_path)
        FileUtils.cp_r(Dir.glob("#{temp_dir}/.*").reject { |f| f =~ /\/\.\.?$/ }, full_path)
        FileUtils.rm_rf(temp_dir)
      else
        spinner.update_title("Clone failed")
        next
      end
    end

    success = true
    spinner.update_title("Setup complete!")
  rescue StandardError => e
    spinner.update_title("Error: #{e.message}")
  end

  if success
    CLI::UI.puts(CLI::UI.fmt("{{v}} Hono API template installed successfully in #{full_path}"))
    CLI::UI.puts(CLI::UI.fmt("{{*}} To start: cd \"#{location}\" && deno task start"))
  else
    CLI::UI.puts(CLI::UI.fmt("{{x}} Installation failed"))
  end
end

.create_nextjs(_selection) ⇒ Object



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
# File 'lib/greeenboii.rb', line 35

def self.create_nextjs(_selection)
  # Ask for a relative path instead

  location = CLI::UI.ask("Where to install? (relative path)", default: "nextjs-project")
  CLI::UI.puts("Checking prerequisites...")

  # Check prerequisites

  node_version = begin
    `node -v`.strip
  rescue StandardError
    nil
  end
  unless node_version
    CLI::UI.puts(CLI::UI.fmt("{{x}} Node.js not found. Please install Node.js first"))
    return
  end

  git_version = begin
    `git --version`.strip
  rescue StandardError
    nil
  end
  unless git_version
    CLI::UI.puts(CLI::UI.fmt("{{x}} Git not found. Please install Git first"))
    return
  end

  # Create a path relative to current directory

  full_path = File.expand_path(location, Dir.pwd)

  # Check if directory already exists

  if Dir.exist?(full_path)
    # If directory exists, check if it's empty

    unless Dir.empty?(full_path) # . and .. entries

      CLI::UI.puts(CLI::UI.fmt("{{x}} Directory not empty: #{full_path}"))
      return
    end
  else
    # Try to create the directory

    begin
      FileUtils.mkdir_p(full_path)
    rescue StandardError => e
      CLI::UI.puts(CLI::UI.fmt("{{x}} Cannot create directory: #{e.message}"))
      return
    end
  end

  success = false
  CLI::UI::Spinner.spin("Setting up NextJS + Supabase template") do |spinner|
    spinner.update_title("Cloning repository...")

    # Clone directly into the target directory

    clone_cmd = "git clone https://github.com/greeenboi/nextjs-supabase-template.git \"#{full_path}\""
    result = system(clone_cmd)

    unless result
      # Try alternative approach if direct cloning fails

      spinner.update_title("Direct clone failed, trying alternative...")

      # Clone to a temporary directory first

      temp_dir = "#{Dir.pwd}/temp_clone_#{Time.now.to_i}"
      FileUtils.mkdir_p(temp_dir)
      result = system("git clone https://github.com/greeenboi/nextjs-supabase-template.git \"#{temp_dir}\"")

      if result
        # Copy files to destination

        FileUtils.cp_r(Dir.glob("#{temp_dir}/*"), full_path)
        FileUtils.cp_r(Dir.glob("#{temp_dir}/.*").reject { |f| f =~ /\/\.\.?$/ }, full_path)
        FileUtils.rm_rf(temp_dir)
      else
        spinner.update_title("Clone failed")
        next
      end
    end

    spinner.update_title("Installing dependencies...")
    Dir.chdir(full_path) do
      package_manager = if system("bun -v > /dev/null 2>&1")
                          "bun"
                        elsif system("pnpm -v > /dev/null 2>&1")
                          "pnpm"
                        else
                          "npm"
                        end
      spinner.update_title("Installing dependencies with #{package_manager}...")
      system("#{package_manager} install")
    end

    success = true
    spinner.update_title("Setup complete!")
  rescue StandardError => e
    spinner.update_title("Error: #{e.message}")
  end

  if success
    CLI::UI.puts(CLI::UI.fmt("{{v}} NextJS template installed successfully in #{full_path}"))
    CLI::UI.puts(CLI::UI.fmt("{{*}} To start: cd \"#{location}\" && #{system} run dev"))
  else
    CLI::UI.puts(CLI::UI.fmt("{{x}} Installation failed"))
  end
end