From 1cac5633490b70dc844be583547a08807a8fb63b Mon Sep 17 00:00:00 2001 From: Steve Polito Date: Tue, 26 Mar 2024 12:26:30 -0400 Subject: [PATCH] Lint Generator: Handling missing `package.json` 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 --- lib/generators/suspenders/lint_generator.rb | 6 ++++++ lib/generators/templates/lint/package.json | 4 ++++ test/generators/suspenders/lint_generator_test.rb | 8 ++++++++ 3 files changed, 18 insertions(+) create mode 100644 lib/generators/templates/lint/package.json diff --git a/lib/generators/suspenders/lint_generator.rb b/lib/generators/suspenders/lint_generator.rb index 96aa0533f..21c95e4ff 100644 --- a/lib/generators/suspenders/lint_generator.rb +++ b/lib/generators/suspenders/lint_generator.rb @@ -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 diff --git a/lib/generators/templates/lint/package.json b/lib/generators/templates/lint/package.json new file mode 100644 index 000000000..dcd25e6e3 --- /dev/null +++ b/lib/generators/templates/lint/package.json @@ -0,0 +1,4 @@ +{ + "name": "app", + "private": "true" +} diff --git a/test/generators/suspenders/lint_generator_test.rb b/test/generators/suspenders/lint_generator_test.rb index 30520f7c4..6a3681014 100644 --- a/test/generators/suspenders/lint_generator_test.rb +++ b/test/generators/suspenders/lint_generator_test.rb @@ -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