Skip to content

Commit

Permalink
feat: switch to using package_json for interacting with package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Aug 12, 2023
1 parent bb11f1b commit f207fd8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 27 deletions.
56 changes: 36 additions & 20 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ def apply_template! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Met
# but also after `shakapacker:install` and after Rails has initialized the git
# repo
after_bundle do
require "bundler/inline"

gemfile do
source "https://rubygems.org"
gem "package_json", github: "G-Rath/package_json"
end

require "package_json"

puts "using package_json v#{PackageJson::VERSION}"

# Remove the `test/` directory because we always use RSpec which creates
# its own `spec/` directory
remove_dir "test"
Expand All @@ -133,7 +144,7 @@ def apply_template! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Met
apply "variants/frontend-bootstrap-typescript/template.rb" if TEMPLATE_CONFIG.apply_variant_bootstrap?
apply "variants/frontend-react-typescript/template.rb" if TEMPLATE_CONFIG.apply_variant_react?

run "yarn run typecheck"
package_json.manager.run("typecheck")
end

create_initial_migration
Expand Down Expand Up @@ -190,10 +201,14 @@ def apply_template! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Met
# Run the README template at the end because it introspects the app to
# discover rake tasks etc.
template "variants/backend-base/README.md.tt", "README.md", force: true
run "yarn run prettier --write ./README.md"
package_json.manager.run("prettier", ["--write ./README.md"])
end
end

def package_json
@package_json ||= PackageJson.new(:yarn_classic)
end

# Normalizes the constraints of the given hash of dependencies so that they
# all have an explicit constraint and define a minor & patch version
#
Expand All @@ -211,44 +226,45 @@ def normalize_dependency_constraints(deps)
def build_engines_field
node_version = File.read("./.node-version").strip
{
node: "^#{node_version}",
yarn: "^1.0.0"
"node" => "^#{node_version}",
"yarn" => "^1.0.0"
}
end

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

# ensure that the package name is set based on the folder
package_json["name"] = File.basename(__dir__)

# set engines constraint in package.json
package_json["engines"] = build_engines_field

# ensure that all dependency constraints are normalized
%w[dependencies devDependencies].each { |k| package_json[k] = normalize_dependency_constraints(package_json[k]) }

File.write("./package.json", JSON.pretty_generate(package_json))
package_json.merge! do |pj|
{
"name" => File.basename(__dir__),
"engines" => pj.fetch("engines", {}).merge(build_engines_field),
"dependencies" => normalize_dependency_constraints(pj.fetch("dependencies", {})),
"devDependencies" => normalize_dependency_constraints(pj.fetch("devDependencies", {}))
}
end

run "npx -y sort-package-json"
run "yarn run prettier --write ./package.json"

package_json.manager.run("prettier", ["--write", "./package.json"])

# ensure the yarn.lock is up to date with any changes we've made to package.json
run "yarn install"
package_json.manager.install
end

# Adds the given <code>packages</code> as dependencies using <code>yarn add</code>
#
# @param [Array<String>] packages
def yarn_add_dependencies(packages)
run "yarn add #{packages.join " "}"
puts "adding #{packages.join(" ")} as dependencies"

package_json.manager.add(packages)
end

# Adds the given <code>packages</code> as devDependencies using <code>yarn add --dev</code>
#
# @param [Array<String>] packages
def yarn_add_dev_dependencies(packages)
run "yarn add --dev #{packages.join " "}"
puts "adding #{packages.join(" ")} as dev dependencies"

package_json.manager.add(packages, type: :dev)
end

# Add this template directory to source_paths so that Thor actions like
Expand Down
2 changes: 1 addition & 1 deletion variants/frontend-base-typescript/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def rename_js_file_to_ts(file)
@typescript-eslint/eslint-plugin@5
]

run "yarn install"
package_json.manager.install

rename_js_file_to_ts "app/frontend/packs/application"

Expand Down
4 changes: 2 additions & 2 deletions variants/frontend-base/js-lint/fixes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

# must be run after prettier is installed and has been configured by setting
# the 'prettier' key in package.json
run "yarn run js-lint-fix"
run "yarn run format-fix"
package_json.manager.run("js-lint-fix")
package_json.manager.run("format-fix")
2 changes: 1 addition & 1 deletion variants/frontend-bootstrap-typescript/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

yarn_add_dependencies %w[@types/bootstrap]

run "yarn install"
package_json.manager.install

rename_js_file_to_ts "app/frontend/js/bootstrap"
2 changes: 1 addition & 1 deletion variants/frontend-bootstrap/template.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source_paths.unshift(File.dirname(__FILE__))

yarn_add_dependencies(["bootstrap", "@popperjs/core"])
run "yarn install"
package_json.manager.install

copy_file "app/frontend/js/bootstrap.js", force: true
insert_into_file "app/frontend/packs/application.js", "import '../js/bootstrap';", before: "import '../stylesheets/application.scss';"
Expand Down
4 changes: 2 additions & 2 deletions variants/frontend-react-typescript/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

installed_jest_major_version = JSON.parse(File.read("node_modules/jest/package.json")).fetch("version").split(".").first

run "yarn remove prop-types"
package_json.manager.remove(["prop-types"])
yarn_add_dependencies %w[@types/react @types/react-dom]
yarn_add_dev_dependencies [
"@jest/types@#{installed_jest_major_version}",
"ts-jest@#{installed_jest_major_version}",
"ts-node"
]
run "yarn install"
package_json.manager.install

rename_js_file_to_ts "app/frontend/packs/server_rendering"

Expand Down

0 comments on commit f207fd8

Please sign in to comment.