-
-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce 'suspenders:stylelint` generator
- Assumes we are using `.css` files based on #1146 - Improve test helper - Configure eslint - Configure prettier - Capture yarn warnings
- Loading branch information
1 parent
6bed72e
commit 29ec91b
Showing
7 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": [ | ||
"@thoughtbot/eslint-config/base", | ||
"@thoughtbot/eslint-config/prettier" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": [ | ||
"@thoughtbot/eslint-config/base", | ||
"@thoughtbot/eslint-config/prettier" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@thoughtbot/stylelint-config" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
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 | ||
output = capture(:stderr) { run_generator } | ||
|
||
assert_match(/yarn add stylelint eslint @thoughtbot\/stylelint-config @thoughtbot\/eslint-config --dev/, output) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters