Skip to content

Commit

Permalink
Introduce 'suspenders:stylelint` generator
Browse files Browse the repository at this point in the history
- Assumes we are using `.css` files based on #1146
- Improve test helper
- Configure eslint
- Configure prettier
- Capture yarn warnings
  • Loading branch information
stevepolitodesign committed Dec 1, 2023
1 parent 6bed72e commit 360773a
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 1 deletion.
41 changes: 41 additions & 0 deletions lib/generators/suspenders/stylelint_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Suspenders
module Generators
class StylelintGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates/stylelint", __FILE__)

def install_dependencies
run "yarn add stylelint eslint @thoughtbot/stylelint-config @thoughtbot/eslint-config --dev"
end

def configure_stylelint
copy_file "stylelintrc.json", ".stylelintrc.json"
end

def configure_eslint
copy_file "eslintrc.json", ".eslintrc.json"
end

def configure_prettier
copy_file "prettierrc", ".prettierrc"
end

# TODO: Consider extracting this into Rails
def update_package_json
content = File.read package_json
json = JSON.parse content
json["scripts"] ||= {}

json["scripts"]["eslint"] = "npx eslint 'app/javascript/**/*.js'"
json["scripts"]["stylelint"] = "npx stylelint 'app/assets/stylesheets/**/*.css'"

File.write package_json, JSON.pretty_generate(json)
end

private

def package_json
Rails.root.join("package.json")
end
end
end
end
6 changes: 6 additions & 0 deletions lib/generators/templates/stylelint/eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"@thoughtbot/eslint-config/base",
"@thoughtbot/eslint-config/prettier"
]
}
6 changes: 6 additions & 0 deletions lib/generators/templates/stylelint/eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"@thoughtbot/eslint-config/base",
"@thoughtbot/eslint-config/prettier"
]
}
3 changes: 3 additions & 0 deletions lib/generators/templates/stylelint/prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
3 changes: 3 additions & 0 deletions lib/generators/templates/stylelint/stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@thoughtbot/stylelint-config"
}
135 changes: 135 additions & 0 deletions test/generators/suspenders/stylelint_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
require "test_helper"
require "generators/suspenders/stylelint_generator"

module Suspenders
module Generators
class StylelintGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::StylelintGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "installs dependencies" do
capture(:stderr) do
output = run_generator

assert_match(/yarn add stylelint eslint @thoughtbot\/stylelint-config @thoughtbot\/eslint-config --dev/, output)
end
end

test "configures stylelint" do
expected_content = <<~TEXT
{
"extends": "@thoughtbot/stylelint-config"
}
TEXT

capture(:stderr) { run_generator }

assert_file app_root(".stylelintrc.json") do |file|
assert_equal expected_content, file
end
end

test "configures eslint" do
expected_content = <<~TEXT
{
"extends": [
"@thoughtbot/eslint-config/base",
"@thoughtbot/eslint-config/prettier"
]
}
TEXT

capture(:stderr) { run_generator }

assert_file app_root(".eslintrc.json") do |file|
assert_equal expected_content, file
end
end

test "configures prettier" do
expected_content = <<~TEXT
{
"singleQuote": true
}
TEXT

capture(:stderr) { run_generator }

assert_file app_root(".prettierrc") do |file|
assert_equal expected_content, file
end
end

test "updates package.json" do
touch "package.json", content: package_json

capture(:stderr) { run_generator }

assert_file "package.json" do |file|
assert_equal expected_package_json, file
end
end

test "updates package.json if script key does not exist" do
touch "package.json", content: package_json(empty: true)

capture(:stderr) { run_generator }

assert_file "package.json" do |file|
assert_equal expected_package_json, file
end
end

test "description" do
skip
end

private

def prepare_destination
touch "Gemfile"
touch "package.json", content: package_json(empty: true)
end

def restore_destination
remove_file_if_exists "Gemfile"
remove_file_if_exists ".stylelintrc.json"
remove_file_if_exists ".eslintrc.json"
remove_file_if_exists ".prettierrc"
remove_file_if_exists "package.json"
remove_file_if_exists "package.json", root: true
remove_file_if_exists "yarn.lock", root: true
end

def package_json(empty: false)
if empty
<<~JSON.chomp
{
}
JSON
else
<<~JSON.chomp
{
"scripts": {}
}
JSON
end
end

def expected_package_json
<<~JSON.chomp
{
"scripts": {
"eslint": "npx eslint 'app/javascript/**/*.js'",
"stylelint": "npx stylelint 'app/assets/stylesheets/**/*.css'"
}
}
JSON
end
end
end
end
7 changes: 6 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ def mkdir(dir)
FileUtils.mkdir path
end

def touch(file)
def touch(file, **options)
content = options[:content]
path = app_root file

FileUtils.touch path

if content
File.write app_root(path), content
end
end

def within_api_only_app(**options, &block)
Expand Down

0 comments on commit 360773a

Please sign in to comment.