From a1e8cdfc58ed72be34718117953e2b4ec83872bf Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 21 Aug 2020 15:31:53 +0300 Subject: [PATCH] Update a template again --- template/.eslintrc.yaml | 4 +- template/.remarkrc.yaml | 2 + .../{.stylelintrc.yml => .stylelintrc.yaml} | 2 +- template/.toys/.toys.rb.erb | 20 ++-- template/application.rb.erb | 13 +-- template/assets/scripts/.eslintrc.yaml | 4 - template/babel.config.json | 8 -- template/browserslist | 2 + template/config/base.rb.erb | 110 ------------------ template/config/full.rb.erb | 16 --- template/config/main.rb.erb | 68 +++++++++++ template/config/processors/sentry.rb.erb | 2 +- template/config/processors/server.rb.erb | 2 + template/config/puma.rb.erb | 10 +- template/constants.rb.erb | 16 +++ template/exe/setup/node.sh | 8 +- template/filewatchers.yaml | 8 +- template/lib/flame/raven_context.rb | 3 +- template/rollup.config.js.erb | 4 - 19 files changed, 123 insertions(+), 179 deletions(-) create mode 100644 template/.remarkrc.yaml rename template/{.stylelintrc.yml => .stylelintrc.yaml} (99%) delete mode 100644 template/babel.config.json create mode 100644 template/browserslist delete mode 100644 template/config/base.rb.erb delete mode 100644 template/config/full.rb.erb create mode 100644 template/config/main.rb.erb create mode 100644 template/constants.rb.erb diff --git a/template/.eslintrc.yaml b/template/.eslintrc.yaml index 9cc83bb..0f75f9d 100644 --- a/template/.eslintrc.yaml +++ b/template/.eslintrc.yaml @@ -23,7 +23,7 @@ rules: - avoidEscape: true semi: - error - - always + - never no-multi-spaces: - error keyword-spacing: @@ -61,6 +61,8 @@ rules: - allow: - error - warn + no-var: + - error arrow-body-style: - warn arrow-parens: diff --git a/template/.remarkrc.yaml b/template/.remarkrc.yaml new file mode 100644 index 0000000..755a2b1 --- /dev/null +++ b/template/.remarkrc.yaml @@ -0,0 +1,2 @@ +plugins: + - remark-preset-lint-recommended diff --git a/template/.stylelintrc.yml b/template/.stylelintrc.yaml similarity index 99% rename from template/.stylelintrc.yml rename to template/.stylelintrc.yaml index 97ebd81..b433c4e 100644 --- a/template/.stylelintrc.yml +++ b/template/.stylelintrc.yaml @@ -9,7 +9,7 @@ rules: - tab max-line-length: - - 80 + - 100 - ignorePattern: /// https?:/// at-rule-no-unknown: diff --git a/template/.toys/.toys.rb.erb b/template/.toys/.toys.rb.erb index 524e03e..887b89f 100644 --- a/template/.toys/.toys.rb.erb +++ b/template/.toys/.toys.rb.erb @@ -2,19 +2,21 @@ include :bundler, static: true -config_dir = "#{__dir__}/../config" - -require "#{config_dir}/base" +root_dir = "#{__dir__}/.." +config_dir = "#{root_dir}/config" +require "#{root_dir}/constants" require 'benchmark_toys' expand BenchmarkToys::Template alias_tool :b, :benchmark application_proc = proc do - require_relative '../application' + require "#{config_dir}/main" <%= @short_module_name %>::Application end +config_proc = proc { application_proc.call.config } + require 'config_toys' expand ConfigToys::Template, config_dir: config_dir @@ -27,10 +29,7 @@ expand SequelMigrationsToys::Template, db_connection_proc: db_connection_proc require 'psql_toys' expand PSQLToys::Template, - db_config_proc: (proc do - ## For full config, not base - application_proc.call.config[:database] - end), + db_config_proc: proc { config_proc.call[:database] }, db_connection_proc: db_connection_proc, db_extensions: %w[citext pgcrypto].freeze @@ -46,10 +45,7 @@ require 'flame_routes_toys' expand FlameRoutesToys::Template, application_proc: application_proc require 'flame_server_toys' -expand FlameServerToys::Template, - config_proc: (proc do - <%= @short_module_name %>::Config::Base.new - end) +expand FlameServerToys::Template, config_proc: config_proc require 'locales_toys' expand LocalesToys::Template diff --git a/template/application.rb.erb b/template/application.rb.erb index e5afc3c..e3f456f 100644 --- a/template/application.rb.erb +++ b/template/application.rb.erb @@ -1,14 +1,9 @@ # frozen_string_literal: true -require_relative 'config/base' -config = <%= @module_name %>::Config::Base.new - ## Require gems require 'bundler/setup' Bundler.require( - :system, :server, :database, - :translations, :forms, :views, :assets, :mails, :others, - config[:environment] + :system, :server, :database, :translations, :forms, :views, :assets, :mails, :others ) require 'erubi/capture_end' @@ -27,9 +22,7 @@ require 'pp' # require 'money/bank/google_currency' -## Load full application config, with dependencies from Bundler -require_relative 'config/full' -<%= @module_name %>.complete_config config +require_relative 'constants' module <%= @module_name %> ## Class for application @@ -74,5 +67,3 @@ module <%= @module_name %> end end end - -<%= @short_module_name %>::Application.config = config diff --git a/template/assets/scripts/.eslintrc.yaml b/template/assets/scripts/.eslintrc.yaml index 4b47c3e..cf7ae01 100644 --- a/template/assets/scripts/.eslintrc.yaml +++ b/template/assets/scripts/.eslintrc.yaml @@ -7,7 +7,3 @@ parserOptions: ## Libs: # google: true # Cccombo: true - -rules: - no-var: - - error diff --git a/template/babel.config.json b/template/babel.config.json deleted file mode 100644 index d2020cd..0000000 --- a/template/babel.config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "presets": [ - ["@babel/preset-env", { "modules": false }] - ], - "plugins": [ - "@babel/plugin-transform-object-assign" - ] -} diff --git a/template/browserslist b/template/browserslist new file mode 100644 index 0000000..e639fb0 --- /dev/null +++ b/template/browserslist @@ -0,0 +1,2 @@ +> 1% +not IE 11 diff --git a/template/config/base.rb.erb b/template/config/base.rb.erb deleted file mode 100644 index 4972030..0000000 --- a/template/config/base.rb.erb +++ /dev/null @@ -1,110 +0,0 @@ -# frozen_string_literal: true - -require 'yaml' - -module <%= @module_name %> - ::<%= @short_module_name %> = ::<%= @module_name %> - - APP_DIRS = [ - 'lib', - 'config', - 'models', - # 'policies', - 'helpers', - 'mailers', - # 'actions', - 'forms', - # 'view_objects', - 'controllers' - ].freeze - - module Config - ## Class for config like a Hash with helper methods. - ## It was a part of the Flame, but some places, like Puma config, - ## need for this without Bundler (dependecies): - ## https://github.com/puma/puma/issues/2319 - class Base < Hash - ## Create an instance of application config - def initialize - populate_dirs - - self[:stdout_file] = "#{self[:logs_dir]}/out" - self[:stderr_file] = "#{self[:logs_dir]}/err" - - require_relative 'processors/server' - Processors::Server.new self - - %i[session site].each do |config_name| - load_yaml config_name, required: true - end - end - - ## Method for loading YAML-files from config directory - ## @param name [Symbol] - ## file base name (extension is `.yml` or '.yaml') - ## @example Load SMTP file without extension, by Symbol - ## config.load_yaml(:smtp) - def load_yaml(name, required: false) - file_name = "#{name}.y{a,}ml" - - file_path = find_config_file file_name, required: required - - return unless file_path - - self[name] = YAML.load_file(file_path) - end - - private - - def populate_dirs - self[:root_dir] = File.realpath "#{__dir__}/.." - - %i[config logs public tmp views].each do |dir_name| - self[:"#{dir_name}_dir"] = "#{self[:root_dir]}/#{dir_name}" - end - - self[:pids_dir] = "#{self[:tmp_dir]}/pids" - end - - def find_config_file(file_name, required:) - file_path = nil - - loop do - file_path = Dir[File.join(self[:config_dir], file_name)].first - break if file_path - - config_relative_dir = self[:config_dir].sub(self[:root_dir], '') - puts "Config file '#{file_name}' not found in '#{config_relative_dir}'" - - next if ask_to_check_config_files - - required ? abort : break - end - - file_path - end - - def ask_to_check_config_files - highline.choose do |menu| - menu.layout = :one_line - - menu.prompt = 'Do you want to check config files? ' - - menu.choice(:yes) do - system 'toys config check' - true - end - - menu.choice(:no) { false } - end - end - - def highline - @highline ||= begin - require 'highline' - HighLine.new - end - end - end - end -end diff --git a/template/config/full.rb.erb b/template/config/full.rb.erb deleted file mode 100644 index d8cace6..0000000 --- a/template/config/full.rb.erb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -## Config split into `base` and `full` because of `prune_bundler`: -## https://github.com/puma/puma/issues/2319 -module <%= @module_name %> - class << self - using GorillaPatch::Inflections - - def complete_config(config) - %w[Sentry R18n Mail Sequel Shrine].each do |processor_name| - require_relative "processors/#{processor_name.underscore}" - <%= @short_module_name %>::Config::Processors.const_get(processor_name).new config - end - end - end -end diff --git a/template/config/main.rb.erb b/template/config/main.rb.erb new file mode 100644 index 0000000..b6aaa26 --- /dev/null +++ b/template/config/main.rb.erb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require_relative '../application' + +config = <%= @short_module_name %>::Application.config + +class << config + private + + def find_config_file(filename, required:) + file_path = nil + + loop do + file_path = super + break + rescue Flame::Errors::ConfigFileNotFoundError => e + puts e.message + + next if ask_to_check_config_files + + required ? abort : break + end + + file_path + end + + def ask_to_check_config_files + highline.choose do |menu| + menu.layout = :one_line + + menu.prompt = 'Do you want to check config files? ' + + menu.choice(:yes) do + system 'toys config check' + true + end + + menu.choice(:no) { false } + end + end + + def highline + @highline ||= begin + require 'highline' + HighLine.new + end + end +end + +using GorillaPatch::Inflections + +config.instance_exec do + self[:root_dir] = File.realpath "#{__dir__}/.." + + self[:pids_dir] = "#{self[:tmp_dir]}/pids" + + self[:stdout_file] = "#{self[:log_dir]}/out" + self[:stderr_file] = "#{self[:log_dir]}/err" + + %i[session site].each do |config_name| + load_yaml config_name, required: true + end + + %w[Server Sentry R18n Mail Sequel Shrine].each do |processor_name| + require_relative "processors/#{processor_name.underscore}" + <%= @short_module_name %>::Config::Processors.const_get(processor_name).new self + end +end diff --git a/template/config/processors/sentry.rb.erb b/template/config/processors/sentry.rb.erb index 4948fe9..b140f4b 100644 --- a/template/config/processors/sentry.rb.erb +++ b/template/config/processors/sentry.rb.erb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'raven' +require_relative '../../lib/flame/raven_context' module <%= @module_name %> module Config diff --git a/template/config/processors/server.rb.erb b/template/config/processors/server.rb.erb index df2ed70..e7b42ba 100644 --- a/template/config/processors/server.rb.erb +++ b/template/config/processors/server.rb.erb @@ -11,6 +11,8 @@ module <%= @module_name %> environment = config[:environment] = ENV['RACK_ENV'] ||= server_config[:environment] || 'development' + Bundler.require environment + server_config = config[:server] = server_config[environment] server_config[:puma_pid_file] = "#{config[:pids_dir]}/#{server_config[:puma_pid_file]}" diff --git a/template/config/puma.rb.erb b/template/config/puma.rb.erb index 0039af7..d4ff5bd 100644 --- a/template/config/puma.rb.erb +++ b/template/config/puma.rb.erb @@ -3,9 +3,9 @@ require 'fileutils' require 'etc' -require_relative 'base' +require_relative 'main' -config = <%= @module_name %>::Config::Base.new +config = <%= @module_name %>::Application.config server_config = config[:server] @@ -13,7 +13,9 @@ environment config[:environment] directory config[:root_dir] -prune_bundler +## Don't call phase-restart +## https://github.com/puma/puma/issues/2319 +# prune_bundler rackup 'config.ru' @@ -24,7 +26,7 @@ FileUtils.mkdir_p pids_dir pidfile File.join pids_dir, 'puma.pid' state_path File.join pids_dir, 'puma.state' -FileUtils.mkdir_p config[:logs_dir] +FileUtils.mkdir_p config[:log_dir] if server_config[:daemonize] stdout_redirect( diff --git a/template/constants.rb.erb b/template/constants.rb.erb new file mode 100644 index 0000000..4288760 --- /dev/null +++ b/template/constants.rb.erb @@ -0,0 +1,16 @@ +module <%= @module_name %> + ::<%= @short_module_name %> = ::<%= @module_name %> + + APP_DIRS = [ + 'lib', + 'config', + 'models', + # 'policies', + 'helpers', + 'mailers', + # 'actions', + 'forms', + # 'view_objects', + 'controllers' + ].freeze +end diff --git a/template/exe/setup/node.sh b/template/exe/setup/node.sh index cce3cc9..d0398ea 100755 --- a/template/exe/setup/node.sh +++ b/template/exe/setup/node.sh @@ -17,6 +17,10 @@ then fi fi -exe npm install +exe npm install -g pnpm -exe npm run build +exe nodenv rehash + +exe pnpm install + +exe pnpm run build diff --git a/template/filewatchers.yaml b/template/filewatchers.yaml index 2176609..6c9caca 100644 --- a/template/filewatchers.yaml +++ b/template/filewatchers.yaml @@ -3,10 +3,10 @@ :command: bundle exec pumactl restart -F config/puma.rb - :pattern: '{assets/styles/**/*,postcss.config.js}' - :command: npm run build:styles + :command: pnpm run build:styles -- :pattern: '{assets/scripts/**/*,babel.config.json,rollup.config.js}' - :command: npm run build:scripts +- :pattern: '{assets/scripts/**/*,rollup.config.js}' + :command: pnpm run build:scripts - :pattern: 'package.json' - :command: npm install && npm run build + :command: pnpm install && pnpm run build diff --git a/template/lib/flame/raven_context.rb b/template/lib/flame/raven_context.rb index 99a48b9..3fa2c34 100644 --- a/template/lib/flame/raven_context.rb +++ b/template/lib/flame/raven_context.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true -require 'flame/raven_context' +## Required via `Bundler.require` in `application.rb` +# require 'flame/raven_context' module Flame ## Redefine `user` object diff --git a/template/rollup.config.js.erb b/template/rollup.config.js.erb index 7ab4d53..7a36240 100644 --- a/template/rollup.config.js.erb +++ b/template/rollup.config.js.erb @@ -1,7 +1,6 @@ import json from '@rollup/plugin-json'; import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; -import babel from '@rollup/plugin-babel'; export default { output: { @@ -16,8 +15,5 @@ export default { browser: true }), commonjs(), - babel({ - babelHelpers: 'bundled' - }) ] };