Class: PackageJson
- Inherits:
-
Object
- Object
- PackageJson
- Defined in:
- lib/package_json.rb,
lib/package_json/version.rb,
lib/package_json/managers/base.rb,
lib/package_json/managers/bun_like.rb,
lib/package_json/managers/npm_like.rb,
lib/package_json/managers/pnpm_like.rb,
lib/package_json/managers/yarn_berry_like.rb,
lib/package_json/managers/yarn_classic_like.rb
Defined Under Namespace
Modules: Managers Classes: Error, NotImplementedError
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#manager ⇒ Object
readonly
Returns the value of attribute manager.
Class Method Summary collapse
- .fetch_default_fallback_manager ⇒ Object
- .read(path_to_directory = Dir.pwd, fallback_manager: PackageJson.fetch_default_fallback_manager) ⇒ Object
Instance Method Summary collapse
- #delete!(key) ⇒ Object
- #fetch(key, default = (no_default_set_by_user = true; nil)) ⇒ Object
-
#initialize(path_to_directory = Dir.pwd, fallback_manager: PackageJson.fetch_default_fallback_manager) ⇒ PackageJson
constructor
A new instance of PackageJson.
-
#merge! ⇒ Object
Merges the hash returned by the passed block into the existing content of the ‘package.json`.
- #record_package_manager! ⇒ Object
Constructor Details
#initialize(path_to_directory = Dir.pwd, fallback_manager: PackageJson.fetch_default_fallback_manager) ⇒ PackageJson
Returns a new instance of PackageJson.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/package_json.rb', line 31 def initialize(path_to_directory = Dir.pwd, fallback_manager: PackageJson.fetch_default_fallback_manager) @directory = File.absolute_path(path_to_directory) existed = ensure_package_json_exists @manager = new_package_manager(determine_package_manager(fallback_manager)) # only record the packageManager automatically if we created the package.json record_package_manager! unless existed end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
17 18 19 |
# File 'lib/package_json.rb', line 17 def directory @directory end |
#manager ⇒ Object (readonly)
Returns the value of attribute manager.
17 18 19 |
# File 'lib/package_json.rb', line 17 def manager @manager end |
Class Method Details
.fetch_default_fallback_manager ⇒ Object
19 20 21 |
# File 'lib/package_json.rb', line 19 def self.fetch_default_fallback_manager ENV.fetch("PACKAGE_JSON_FALLBACK_MANAGER", "npm").to_sym end |
.read(path_to_directory = Dir.pwd, fallback_manager: PackageJson.fetch_default_fallback_manager) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/package_json.rb', line 23 def self.read(path_to_directory = Dir.pwd, fallback_manager: PackageJson.fetch_default_fallback_manager) unless File.exist?("#{path_to_directory}/package.json") raise Error, "#{path_to_directory} does not contain a package.json" end new(path_to_directory, fallback_manager: fallback_manager) end |
Instance Method Details
#delete!(key) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/package_json.rb', line 59 def delete!(key) pj = read_package_json value = pj.delete(key) write_package_json(pj) value end |
#fetch(key, default = (no_default_set_by_user = true; nil)) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/package_json.rb', line 42 def fetch(key, default = (no_default_set_by_user = true; nil)) contents = read_package_json if no_default_set_by_user contents.fetch(key) else contents.fetch(key, default) end end |
#merge! ⇒ Object
Merges the hash returned by the passed block into the existing content of the ‘package.json`
53 54 55 56 57 |
# File 'lib/package_json.rb', line 53 def merge! pj = read_package_json write_package_json(pj.merge(yield read_package_json)) end |
#record_package_manager! ⇒ Object
69 70 71 |
# File 'lib/package_json.rb', line 69 def record_package_manager! merge! { { "packageManager" => "#{manager.binary}@#{manager.version}" } } end |