Skip to content

Commit

Permalink
feat: add #native_install_command to managers
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Aug 12, 2023
1 parent 437362c commit a768205
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ package_json.manager.install(frozen: true)
| `legacy_peer_deps` | Have `npm` completely ignore `peerDependencies` when installing; does nothing for other package managers |
| `omit_optional_deps` | Omit optional dependencies when installing |

#### Generating the `install` command for embedding in native scripts

```ruby
native_install_command = package_json.manager.native_install_command

append_to_file "bin/ci-run" do
<<~JEST
echo "* ******************************************************"
echo "* Installing JS dependencies"
echo "* ******************************************************"
#{native_install_command}
JEST
end
```

| Option | Description |
| -------------------- | -------------------------------------------------------------------------------------------------------- |
| `frozen` | Fail if the lockfile needs to be updated |
| `ignore_scripts` | Don't run scripts specified in `package.json` files |
| `legacy_peer_deps` | Have `npm` completely ignore `peerDependencies` when installing; does nothing for other package managers |
| `omit_optional_deps` | Omit optional dependencies when installing |

#### Adding dependencies

```ruby
Expand Down
10 changes: 10 additions & 0 deletions lib/package_json/managers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def install(
raise NotImplementedError
end

# Provides the "native" command for installing dependencies with this package manager for embedding into scripts
def native_install_command(
frozen: false,
ignore_scripts: false,
legacy_peer_deps: false,
omit_optional_deps: false
)
raise NotImplementedError
end

# Adds the given packages
def add(
packages,
Expand Down
21 changes: 20 additions & 1 deletion lib/package_json/managers/npm_like.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PackageJson
module Managers
class NpmLike < Base
class NpmLike < Base # rubocop:disable Metrics/ClassLength
def initialize(package_json, manager_cmd: "npm")
super(package_json, manager_cmd: manager_cmd)
end
Expand All @@ -24,6 +24,25 @@ def install(
raw(cmd, args)
end

# Provides the "native" command for installing dependencies with this package manager for embedding into scripts
def native_install_command(
frozen: false,
ignore_scripts: false,
legacy_peer_deps: false,
omit_optional_deps: false
)
cmd = "install"
cmd = "ci" if frozen

args = with_native_args(
ignore_scripts: ignore_scripts,
legacy_peer_deps: legacy_peer_deps,
omit_optional_deps: omit_optional_deps
)

build_full_cmd(cmd, args)
end

# Adds the given packages
def add(
packages,
Expand Down
19 changes: 18 additions & 1 deletion lib/package_json/managers/pnpm_like.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PackageJson
module Managers
class PnpmLike < Base
class PnpmLike < Base # rubocop:disable Metrics/ClassLength
def initialize(package_json, manager_cmd: "pnpm")
super(package_json, manager_cmd: manager_cmd)
end
Expand All @@ -22,6 +22,23 @@ def install(
raw("install", args)
end

# Provides the "native" command for installing dependencies with this package manager for embedding into scripts
def native_install_command(
frozen: false,
ignore_scripts: false,
legacy_peer_deps: false,
omit_optional_deps: false
)
args = with_native_args(
frozen: frozen,
ignore_scripts: ignore_scripts,
omit_optional_deps: omit_optional_deps,
_unsupported: [legacy_peer_deps]
)

build_full_cmd("install", args)
end

# Adds the given packages
def add(
packages,
Expand Down
19 changes: 18 additions & 1 deletion lib/package_json/managers/yarn_classic_like.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PackageJson
module Managers
class YarnClassicLike < Base
class YarnClassicLike < Base # rubocop:disable Metrics/ClassLength
def initialize(package_json, manager_cmd: "yarn")
super(package_json, manager_cmd: manager_cmd)
end
Expand All @@ -22,6 +22,23 @@ def install(
raw("install", args)
end

# Provides the "native" command for installing dependencies with this package manager for embedding into scripts
def native_install_command(
frozen: false,
ignore_scripts: false,
legacy_peer_deps: false,
omit_optional_deps: false
)
args = with_native_args(
frozen: frozen,
ignore_scripts: ignore_scripts,
omit_optional_deps: omit_optional_deps,
_unsupported: [legacy_peer_deps]
)

build_full_cmd("install", args)
end

# Adds the given packages
def add(
packages,
Expand Down
7 changes: 7 additions & 0 deletions sig/package_json/managers/base.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class PackageJson
?omit_optional_deps: bool
) -> void

def native_install_command: (
?frozen: bool,
?ignore_scripts: bool,
?legacy_peer_deps: bool,
?omit_optional_deps: bool
) -> String

def add: (
Array[String] packages,
?type: :production | :dev | :optional | :peer,
Expand Down
7 changes: 7 additions & 0 deletions sig/package_json/managers/npm_like.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class PackageJson
?omit_optional_deps: bool
) -> void

def native_install_command: (
?frozen: bool,
?ignore_scripts: bool,
?legacy_peer_deps: bool,
?omit_optional_deps: bool
) -> String

def add: (
Array[String] packages,
?type: :production | :dev | :optional | :peer,
Expand Down
7 changes: 7 additions & 0 deletions sig/package_json/managers/pnpm_like.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class PackageJson
?omit_optional_deps: bool
) -> void

def native_install_command: (
?frozen: bool,
?ignore_scripts: bool,
?legacy_peer_deps: bool,
?omit_optional_deps: bool
) -> String

def add: (
Array[String] packages,
?type: :production | :dev | :optional | :peer,
Expand Down
7 changes: 7 additions & 0 deletions sig/package_json/managers/yarn_classic_like.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class PackageJson
?omit_optional_deps: bool
) -> void

def native_install_command: (
?frozen: bool,
?ignore_scripts: bool,
?legacy_peer_deps: bool,
?omit_optional_deps: bool
) -> String

def add: (
Array[String] packages,
?type: :production | :dev | :optional | :peer,
Expand Down
6 changes: 6 additions & 0 deletions spec/package_json/managers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
end
end

describe "#native_install_command" do
it "does not have an implementation" do
expect { base.native_install_command }.to raise_error PackageJson::NotImplementedError
end
end

describe "#add" do
it "does not have an implementation" do
expect { base.add([]) }.to raise_error PackageJson::NotImplementedError
Expand Down
43 changes: 43 additions & 0 deletions spec/package_json/managers/npm_like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,49 @@
end
end

describe "#native_install_command" do
it "returns the full command" do
expect(manager.native_install_command).to eq("#{package_manager_cmd} install")
end

context "when passing the usual options" do
it "supports frozen" do
expect(manager.native_install_command(frozen: true)).to eq(
"#{package_manager_cmd} ci"
)
end

it "supports ignore_scripts" do
expect(manager.native_install_command(ignore_scripts: true)).to eq(
"#{package_manager_cmd} install --ignore-scripts"
)
end

it "supports legacy_peer_deps" do
expect(manager.native_install_command(legacy_peer_deps: true)).to eq(
"#{package_manager_cmd} install --legacy-peer-deps"
)
end

it "supports omit_optional_deps" do
expect(manager.native_install_command(omit_optional_deps: true)).to eq(
"#{package_manager_cmd} install --omit=optional"
)
end

it "supports all the options together" do
expect(
manager.native_install_command(
frozen: true,
ignore_scripts: true,
legacy_peer_deps: true,
omit_optional_deps: true
)
).to eq("#{package_manager_cmd} ci --ignore-scripts --legacy-peer-deps --omit=optional")
end
end
end

describe "#add" do
it "adds dependencies as production by default" do
with_package_json_file do
Expand Down
43 changes: 43 additions & 0 deletions spec/package_json/managers/pnpm_like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,49 @@
end
end

describe "#native_install_command" do
it "returns the full command" do
expect(manager.native_install_command).to eq("#{package_manager_cmd} install")
end

context "when passing the usual options" do
it "supports frozen" do
expect(manager.native_install_command(frozen: true)).to eq(
"#{package_manager_cmd} install --frozen-lockfile"
)
end

it "supports ignore_scripts" do
expect(manager.native_install_command(ignore_scripts: true)).to eq(
"#{package_manager_cmd} install --ignore-scripts"
)
end

it "supports legacy_peer_deps" do
expect(manager.native_install_command(legacy_peer_deps: true)).to eq(
"#{package_manager_cmd} install"
)
end

it "supports omit_optional_deps" do
expect(manager.native_install_command(omit_optional_deps: true)).to eq(
"#{package_manager_cmd} install --no-optional"
)
end

it "supports all the options together" do
expect(
manager.native_install_command(
frozen: true,
ignore_scripts: true,
legacy_peer_deps: true,
omit_optional_deps: true
)
).to eq("#{package_manager_cmd} install --frozen-lockfile --ignore-scripts --no-optional")
end
end
end

describe "#add" do
it "adds dependencies as production by default" do
with_package_json_file do
Expand Down
43 changes: 43 additions & 0 deletions spec/package_json/managers/yarn_classic_like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,49 @@
end
end

describe "#native_install_command" do
it "returns the full command" do
expect(manager.native_install_command).to eq("#{package_manager_cmd} install")
end

context "when passing the usual options" do
it "supports frozen" do
expect(manager.native_install_command(frozen: true)).to eq(
"#{package_manager_cmd} install --frozen-lockfile"
)
end

it "supports ignore_scripts" do
expect(manager.native_install_command(ignore_scripts: true)).to eq(
"#{package_manager_cmd} install --ignore-scripts"
)
end

it "supports legacy_peer_deps" do
expect(manager.native_install_command(legacy_peer_deps: true)).to eq(
"#{package_manager_cmd} install"
)
end

it "supports omit_optional_deps" do
expect(manager.native_install_command(omit_optional_deps: true)).to eq(
"#{package_manager_cmd} install --ignore-optional"
)
end

it "supports all the options together" do
expect(
manager.native_install_command(
frozen: true,
ignore_scripts: true,
legacy_peer_deps: true,
omit_optional_deps: true
)
).to eq("#{package_manager_cmd} install --frozen-lockfile --ignore-scripts --ignore-optional")
end
end
end

describe "#add" do
it "adds dependencies as production by default" do
with_package_json_file do
Expand Down

0 comments on commit a768205

Please sign in to comment.