Skip to content

Commit f76fb25

Browse files
Lint Generator: Handling missing package.json (#1179)
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
1 parent a40d457 commit f76fb25

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

lib/generators/suspenders/lint_generator.rb

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ class LintGenerator < Rails::Generators::Base
66
source_root File.expand_path("../../templates/lint", __FILE__)
77
desc "Creates a holistic linting solution that covers JavaScript, CSS, Ruby and ERB."
88

9+
def check_package_json
10+
unless File.exist? Rails.root.join("package.json")
11+
copy_file "package.json", "package.json"
12+
end
13+
end
14+
915
def install_dependencies
1016
run "yarn add stylelint eslint @thoughtbot/stylelint-config @thoughtbot/eslint-config npm-run-all prettier --dev"
1117
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "app",
3+
"private": "true"
4+
}

test/generators/suspenders/lint_generator_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ class LintGeneratorTest < Rails::Generators::TestCase
248248
assert_equal desc, generator_class.desc
249249
end
250250

251+
test "created package.json if one does not exist" do
252+
remove_file_if_exists "package.json"
253+
254+
run_generator
255+
256+
assert_file app_root("package.json")
257+
end
258+
251259
private
252260

253261
def prepare_destination

0 commit comments

Comments
 (0)