class DiscourseTheme::Scaffold

Constants

ABOUT_JSON
API_INITIALIZER
BLANK_FILES
EN_YML
ESLINT_RC
GIT_IGNORE
HELP
PACKAGE_JSON
TEMPLATE_LINT_RC

Public Class Methods

generate(dir) click to toggle source
# File lib/discourse_theme/scaffold.rb, line 67
def self.generate(dir)
  UI.progress "Generating a scaffold theme at #{dir}"

  name = loop do
    input = UI.ask("What would you like to call your theme?").to_s.strip
    if input.empty?
      UI.error("Theme name cannot be empty")
    else
      break input
    end
  end

  is_component = UI.yes?("Is this a component?")

  FileUtils.mkdir_p dir
  Dir.chdir dir do
    author = loop do
      input = UI.ask("Who is authoring the theme?", default: ENV['USER']).to_s.strip
      if input.empty?
        UI.error("Author cannot be empty")
      else
        break input
      end
    end

    description = UI.ask("How would you describe this theme?").to_s.strip

    UI.info "Creating about.json"
    about_template = ABOUT_JSON.dup
    about_template[:name] = name
    if is_component
      about_template[:component] = true
    else
      about_template[:color_schemes] = {}
    end
    File.write('about.json', JSON.pretty_generate(about_template))

    UI.info "Creating HELP"
    File.write('HELP', HELP)

    UI.info "Creating package.json"
    File.write('package.json', PACKAGE_JSON.sub("#AUTHOR", author))

    UI.info "Creating .template-lintrc.js"
    File.write('.template-lintrc.js', TEMPLATE_LINT_RC)

    UI.info "Creating .eslintrc"
    File.write('.eslintrc', ESLINT_RC)

    UI.info "Creating .gitignore"
    File.write('.gitignore', GIT_IGNORE)

    locale = "locales/en.yml"
    UI.info "Creating #{locale}"
    FileUtils.mkdir_p(File.dirname(locale))
    File.write(locale, EN_YML.sub("#DESCRIPTION", description))

    encoded_name = name.downcase.gsub(/[^\w_-]+/, '_')
    initializer = "javascripts/discourse/api-initializers/#{encoded_name}.js"
    UI.info "Creating #{initializer}"
    FileUtils.mkdir_p(File.dirname(initializer))
    File.write(initializer, API_INITIALIZER)

    BLANK_FILES.each do |f|
      UI.info "Creating #{f}"
      FileUtils.mkdir_p File.dirname(f)
      FileUtils.touch f
    end

    UI.info "Initializing git repo"
    puts `git init . --initial-branch=main`

    UI.info "Installing dependencies"
    puts `yarn`
  end
end