Skip to content

Commit

Permalink
refactor: create a dedicated method for updating package.json (#527)
Browse files Browse the repository at this point in the history
I'm aware of how much conflict this will create with #466 but oh well 🤷
  • Loading branch information
G-Rath authored Jan 26, 2024
1 parent 71ca3a4 commit c6801a5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 46 deletions.
22 changes: 13 additions & 9 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,23 @@ def build_engines_field
}
end

def cleanup_package_json
package_json = JSON.parse(File.read("./package.json"))
def update_package_json(&block)
package_json = JSON.load_file("./package.json").tap(&block)

# ensure that the package name is set based on the folder
package_json["name"] = File.basename(__dir__)
File.write("./package.json", "#{JSON.pretty_generate(package_json)}\n")
end

# set engines constraint in package.json
package_json["engines"] = build_engines_field
def cleanup_package_json
update_package_json do |package_json|
# ensure that the package name is set based on the folder
package_json["name"] = File.basename(__dir__)

# ensure that all dependency constraints are normalized
%w[dependencies devDependencies].each { |k| package_json[k] = normalize_dependency_constraints(package_json[k]) }
# set engines constraint in package.json
package_json["engines"] = build_engines_field

File.write("./package.json", JSON.pretty_generate(package_json))
# ensure that all dependency constraints are normalized
%w[dependencies devDependencies].each { |k| package_json[k] = normalize_dependency_constraints(package_json[k]) }
end

run "npx -y sort-package-json"

Expand Down
6 changes: 3 additions & 3 deletions variants/frontend-base-typescript/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def rename_js_file_to_ts(file)
EO_TS_ENABLE_IMAGES
gsub_file("app/frontend/packs/application.ts", js_load_images_chunk, ts_load_images_chunk, force: true)

package_json = JSON.parse(File.read("./package.json"))
package_json["scripts"]["typecheck"] = "tsc -p . --noEmit"
File.write("./package.json", JSON.generate(package_json))
update_package_json do |package_json|
package_json["scripts"]["typecheck"] = "tsc -p . --noEmit"
end

append_to_file "bin/ci-run" do
<<~TYPECHECK
Expand Down
25 changes: 12 additions & 13 deletions variants/frontend-base/js-lint/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
copy_file "variants/frontend-base/.eslintrc.js", ".eslintrc.js"
template "variants/frontend-base/.prettierignore.tt", ".prettierignore"

package_json = JSON.parse(File.read("./package.json"))
package_json["prettier"] = "prettier-config-ackama"
package_json["browserslist"] = ["defaults"]
package_json["scripts"] = {
"js-lint" => "eslint . --ignore-pattern '!.eslintrc.js' --ext js,ts,tsx,jsx",
"js-lint-fix" => "eslint . --ignore-pattern '!.eslintrc.js' --ext js,ts,tsx,jsx --fix",
"format-check" => "prettier --check .",
"format-fix" => "prettier --write .",
"scss-lint" => "stylelint '**/*.{css,scss}'",
"scss-lint-fix" => "stylelint '**/*.{css,scss}' --fix"
}

File.write("./package.json", JSON.generate(package_json))
update_package_json do |package_json|
package_json["prettier"] = "prettier-config-ackama"
package_json["browserslist"] = ["defaults"]
package_json["scripts"] = {
"js-lint" => "eslint . --ignore-pattern '!.eslintrc.js' --ext js,ts,tsx,jsx",
"js-lint-fix" => "eslint . --ignore-pattern '!.eslintrc.js' --ext js,ts,tsx,jsx --fix",
"format-check" => "prettier --check .",
"format-fix" => "prettier --write .",
"scss-lint" => "stylelint '**/*.{css,scss}'",
"scss-lint-fix" => "stylelint '**/*.{css,scss}' --fix"
}
end

append_to_file "bin/ci-run" do
<<~ESLINT
Expand Down
10 changes: 4 additions & 6 deletions variants/frontend-react/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@
ERB
end

package_json = JSON.parse(File.read("./package.json"))

# we've replaced this with a babel.config.js
package_json.delete "babel"

File.write("./package.json", JSON.generate(package_json))
update_package_json do |package_json|
# we've replaced this with a babel.config.js
package_json.delete "babel"
end
10 changes: 4 additions & 6 deletions variants/frontend-stimulus-typescript/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

copy_file "babel.config.js", force: true

package_json = JSON.parse(File.read("./package.json"))

# we've replaced this with a babel.config.js
package_json.delete "babel"

File.write("./package.json", JSON.generate(package_json))
update_package_json do |package_json|
# we've replaced this with a babel.config.js
package_json.delete "babel"
end

remove_file "app/frontend/stimulus/controllers/hello_controller.js", force: true
copy_file "app/frontend/stimulus/controllers/hello_controller.ts"
Expand Down
17 changes: 8 additions & 9 deletions variants/frontend-stimulus/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@
copy_file ".eslintrc.js", force: true
copy_file "jest.config.js"

package_json = JSON.parse(File.read("./package.json"))
package_json["scripts"] = package_json["scripts"].merge(
{
"test" => "jest",
"watch-tests" => "jest --watch"
}
)

File.write("./package.json", JSON.generate(package_json))
update_package_json do |package_json|
package_json["scripts"] = package_json["scripts"].merge(
{
"test" => "jest",
"watch-tests" => "jest --watch"
}
)
end

append_to_file "bin/ci-run" do
<<~JEST
Expand Down

0 comments on commit c6801a5

Please sign in to comment.