Class: Jatai::Commands::Init

Inherits:
Base
  • Object
show all
Defined in:
lib/jatai/commands/init.rb

Constant Summary collapse

PRESETS =
{
  "micro" => "Micro — 256MB RAM, 1 réplica (Grátis)",
  "hobby" => "Hobby — 1GB RAM, réplicas ilimitadas (R$ 19/mês)",
  "pro" => "Pro — 2GB RAM, réplicas ilimitadas (R$ 69/mês)"
}.freeze
DEFAULT_REGION =

Região única por enquanto (Sudeste). Quando houver multi-região, volta a ser um prompt.

"br-se1".freeze
STACKS =
{
  "rails" => "Rails",
  "node" => "Node.js",
  "static" => "Estático (HTML/CSS/JS, sem build)",
  "docker" => "Docker (custom Dockerfile)"
}.freeze
DATABASE_TIERS =
{
  "mini" => "Mini — 512MB, 20 conexões (Grátis)",
  "essencial" => "Essencial — 1GB, 50 conexões (R$ 19/mês)",
  "padrao" => "Padrão — 5GB, 100 conexões (R$ 49/mês)"
}.freeze

Instance Method Summary collapse

Methods inherited from Base

#api, #app_name, #app_slug, #error, #info, #pastel, #project_config, #require_auth!, #success

Instance Method Details

#execute(options = {}) ⇒ Object



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
# File 'lib/jatai/commands/init.rb', line 28

def execute(options = {})
  prompt = TTY::Prompt.new
  config_path = File.join(Dir.pwd, Config::PROJECT_FILE)

  if File.exist?(config_path)
    return unless prompt.yes?("Arquivo .jatai.yml já existe. Sobrescrever?")
  end

  stack = detect_stack
  if stack
    info("Stack detectado: #{STACKS[stack]}")
    unless prompt.yes?("Confirmar stack #{STACKS[stack]}?")
      stack = prompt.select("Stack:", STACKS.map { |k, v| { name: v, value: k } })
    end
  else
    info("Não foi possível detectar o stack automaticamente.")
    stack = prompt.select("Stack:", STACKS.map { |k, v| { name: v, value: k } })
  end

  name, slug = ask_name_and_slug(prompt)
  preset = prompt.select("Preset:", PRESETS.map { |k, v| { name: v, value: k } })
  # Sites estáticos não têm banco — não pergunta o tier.
  db_tier = stack == "static" ? nil : prompt.select("Banco de dados:", DATABASE_TIERS.map { |k, v| { name: v, value: k } })
  branch = detect_git_branch || "main"

  # `app` é o IDENTIFICADOR (slug) usado nas URLs da API; `name` é o nome de
  # exibição. Antes um único campo servia aos dois papéis, e um nome com
  # espaço ("Alves Barbearia") ia direto para a URL e estourava no URI.
  config = {
    "app" => slug,
    "name" => name,
    "stack" => stack,
    "preset" => preset,
    "region" => DEFAULT_REGION,
    "branch" => branch
  }
  config["database_tier"] = db_tier if db_tier

  File.write(config_path, YAML.dump(config))
  success("Arquivo .jatai.yml criado com sucesso!")
  info("Identificador: #{slug}") if slug != name
  info("Branch: #{branch}")
  info("Execute `jatai up` para criar e fazer deploy do app.")
end