Skip to content

Commit

Permalink
feat: check PACKAGE_JSON_FALLBACK_MANAGER for default fallback manager
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Aug 13, 2023
1 parent eb9df09 commit d737e2e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ does not exist or does not have the `packageManager` property:
PackageJson.new(fallback_manager: :yarn_classic)
```

The default fallback is `:npm`; supported package managers are `:npm`,
`:yarn_classic`, and `:pnpm`.
If a fallback manager is not defined, then the value of the env variable
`PACKAGE_JSON_FALLBACK_MANAGER` will be used if set, or otherwise falling back
to `npm`.

Supported package managers are `:npm`, `:yarn_classic`, and `:pnpm`.

If the `package.json` does not exist, then the `packageManager` property will be
included based on this value, but it will _not_ be updated if the file already
Expand Down
8 changes: 6 additions & 2 deletions lib/package_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ class NotImplementedError < Error; end

attr_reader :manager, :path

def self.read(path_to_directory = Dir.pwd, fallback_manager: :npm)
def self.fetch_default_fallback_manager
ENV.fetch("PACKAGE_JSON_FALLBACK_MANAGER", "npm").to_sym
end

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

def initialize(path_to_directory = Dir.pwd, fallback_manager: :npm)
def initialize(path_to_directory = Dir.pwd, fallback_manager: PackageJson.fetch_default_fallback_manager)
@path = path_to_directory

ensure_package_json_exists(fallback_manager)
Expand Down
2 changes: 2 additions & 0 deletions sig/package_json.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class PackageJson
attr_reader manager: Managers::Base
attr_reader path: String

def self.fetch_default_fallback_manager: () -> Symbol

def self.read: (?String path_to_directory, ?package_manager: (:npm | :yarn_classic | :pnpm)) -> PackageJson

def initialize: (?String path_to_directory, ?package_manager: (:npm | :yarn_classic | :pnpm)) -> PackageJson
Expand Down

0 comments on commit d737e2e

Please sign in to comment.