Class: DiscourseTheme::Scaffold

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

Constant Summary collapse

BLANK_FILES =
%w{
  common/common.scss

  settings.yml
}
ABOUT_JSON =
{
  about_url: nil,
  license_url: nil,
  assets: {}
}
HELP =
<<~STR
  Are you a bit lost? Be sure to read https://meta.discourse.org/t/how-to-develop-custom-themes/60848
STR
GIT_IGNORE =
<<~STR
  .discourse-site
  node_modules
  HELP
STR
API_INITIALIZER =
<<~STR
  import { apiInitializer } from "discourse/lib/api";

  export default apiInitializer("0.11.1", api => {
    console.log("hello world from api initializer!");
  });
STR
PACKAGE_JSON =
<<~STR
  {
    "author": "#AUTHOR",
    "license": "MIT",
    "devDependencies": {
      "eslint-config-discourse": "latest"
    }
  }
STR
ESLINT_RC =
<<~STR
  {
    "extends": "eslint-config-discourse",
    "globals": {
      "settings": "readonly",
      "themePrefix": "readonly"
    }
  }
STR
TEMPLATE_LINT_RC =
<<~STR
  module.exports = {
    plugins: ["ember-template-lint-plugin-discourse"],
    extends: "discourse:recommended",
  };
STR
EN_YML =
<<~YAML
  en:
    theme_metadata:
      description: "#DESCRIPTION"
YAML

Class Method Summary collapse

Class Method Details

.generate(dir) ⇒ Object



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
# File 'lib/discourse_theme/scaffold.rb', line 71

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(/[^a-zA-Z0-9_-]+/, '_')
    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