Class: DiscourseTheme::Scaffold

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

Constant Summary collapse

ABOUT_JSON =
{
  about_url: "TODO: Put your theme's public repo or Meta topic URL here",
  license_url: "TODO: Put your theme's LICENSE URL here",
  assets: {
  },
}
HELP =
<<~STR
  Are you a bit lost? Be sure to read https://meta.discourse.org/t/how-to-develop-custom-themes/60848
STR
LICENSE =
<<~STR
  Copyright #YEAR #AUTHOR

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
STR
GIT_IGNORE =
<<~STR
  .discourse-site
  node_modules
  HELP
STR
API_INITIALIZER =
<<~STR
  import { apiInitializer } from "discourse/lib/api";

  export default apiInitializer("1.8.0", (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
SETTINGS_YML =
<<~YAML
  todo_rename_and_use_setting:
    default: ""
YAML

Class Method Summary collapse

Class Method Details

.generate(dir) ⇒ Object



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

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

    about_template = ABOUT_JSON.dup
    about_template[:name] = name
    if is_component
      about_template[:component] = true
    else
      about_template[:color_schemes] = {}
    end

    encoded_name = name.downcase.gsub(/[^a-zA-Z0-9_-]+/, "_")

    write("about.json", JSON.pretty_generate(about_template))
    write("HELP", HELP)
    write("LICENSE", LICENSE.sub("#YEAR", "#{Date.today.year}").sub("#AUTHOR", author))
    write(".eslintrc", ESLINT_RC)
    write(".gitignore", GIT_IGNORE)
    write(".template-lintrc.js", TEMPLATE_LINT_RC)
    write("package.json", PACKAGE_JSON.sub("#AUTHOR", author))
    write("settings.yml", SETTINGS_YML)
    write("common/common.scss", "")
    write("javascripts/discourse/api-initializers/#{encoded_name}.js", API_INITIALIZER)
    write("locales/en.yml", EN_YML.sub("#DESCRIPTION", description))

    UI.info "Initializing git repo"
    puts `git init && git symbolic-ref HEAD refs/heads/main`

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