Skip to content

Commit

Permalink
Lint Generator: Handling missing package.json
Browse files Browse the repository at this point in the history
In order to avoid raising `No such file or directory`, we create a
`package.json` file if one does not exist. We borrow a template from
[cssbundling-rails][].

[cssbundling-rails]: https://github.com/rails/cssbundling-rails/blob/09d81cb0accf00abb77d8af5b24f5aad0b71a57a/lib/install/package.json#L3
  • Loading branch information
stevepolitodesign committed Mar 26, 2024
1 parent a40d457 commit 1cac563
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/generators/suspenders/lint_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ class LintGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates/lint", __FILE__)
desc "Creates a holistic linting solution that covers JavaScript, CSS, Ruby and ERB."

def check_package_json
unless File.exist? Rails.root.join("package.json")
copy_file "package.json", "package.json"
end
end

def install_dependencies
run "yarn add stylelint eslint @thoughtbot/stylelint-config @thoughtbot/eslint-config npm-run-all prettier --dev"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/generators/templates/lint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "app",
"private": "true"
}
8 changes: 8 additions & 0 deletions test/generators/suspenders/lint_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ class LintGeneratorTest < Rails::Generators::TestCase
assert_equal desc, generator_class.desc
end

test "created package.json if one does not exist" do
remove_file_if_exists "package.json"

run_generator

assert_file app_root("package.json")
end

private

def prepare_destination
Expand Down

0 comments on commit 1cac563

Please sign in to comment.