Skip to content

Commit

Permalink
feat: remember last install strategy and cleanup if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 6, 2023
1 parent 0cb01c8 commit 0ab4235
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
6 changes: 5 additions & 1 deletion src/commands/install/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ struct Zap::Commands::Install::Config < Zap::Commands::Config
omit.includes?(Omit::Peer)
end

def merge_lockfile(lockfile : Lockfile)
self.copy_with(strategy: @strategy || lockfile.strategy || InstallStrategy::Classic)
end

def merge_pkg(package : Package)
self.copy_with(
strategy: @strategy || package.zap_config.try(&.strategy) || InstallStrategy::Classic,
strategy: @strategy || package.zap_config.try(&.strategy) || nil,
check_peer_dependencies: @check_peer_dependencies || package.zap_config.try(&.check_peer_dependencies) || false,
)
end
Expand Down
88 changes: 63 additions & 25 deletions src/commands/install/install.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ module Zap::Commands::Install

lockfile = Lockfile.new(config.prefix)

# Merge zap config from package.json
install_config = install_config.merge_pkg(inferred_context.main_package)
# Merge zap config from package.json and lockfile
install_config = install_config
.merge_pkg(inferred_context.main_package)
.merge_lockfile(lockfile)

Log.debug { "Install Configuration: #{install_config.pretty_inspect}" }

# Load .npmrc file
npmrc = Npmrc.new(config.prefix)
Log.debug { "Npmrc: #{npmrc.pretty_inspect}" }
Expand All @@ -59,32 +63,14 @@ module Zap::Commands::Install
# Print info about the install
self.print_info(config, inferred_context, install_config, lockfile, workspaces)

# Remove node_modules / .pnp folder if the install strategy has changed
self.strategy_check(config, install_config, lockfile, reporter)

# Force hoisting if the hoisting options have changed
if lockfile.update_hoisting_shasum(inferred_context.main_package)
if install_config.frozen_lockfile
# If the lockfile is frozen, raise an error
raise "The --frozen-lockfile flag is on but hoisting settings have been modified since the last lockfile update. Run `zap i --frozen-lockfile=false` to regenerate the lockfile and try again."
end
self.hoisting_check(install_config, lockfile, inferred_context, reporter)

if lockfile.read_status.from_disk?
Log.debug { "Detected a change in hoisting options in the package.json file" }
reporter.info("Hoisting options were modified. The packages will be re-installed.")
install_config = install_config.copy_with(refresh_install: true)
end
end
# Force metadata retrieval if the package extensions options have changed
if lockfile.update_package_extensions_shasum(inferred_context.main_package)
if install_config.frozen_lockfile
# If the lockfile is frozen, raise an error
raise "The --frozen-lockfile flag is on but package extensions have been modified since the last lockfile update. Run `zap i --frozen-lockfile=false` to regenerate the lockfile and try again."
end

if lockfile.read_status.from_disk?
Log.debug { "Detected a change in package extensions options in the package.json file" }
reporter.info("Package extensions have been modified. Package metadata will forcefully be fetched from the registry and packages will be re-installed.")
install_config = install_config.copy_with(force_metadata_retrieval: true, refresh_install: true)
end
end
self.package_extensions_check(install_config, lockfile, inferred_context, reporter)

# Init state struct
state = State.new(
Expand Down Expand Up @@ -208,6 +194,58 @@ module Zap::Commands::Install
end
end

private def self.strategy_check(config : Zap::Config, install_config : Install::Config, lockfile : Lockfile, reporter : Reporter)
if !config.global && lockfile.strategy && lockfile.strategy != install_config.strategy
Log.debug { "Install strategy changed from #{lockfile.strategy} to #{install_config.strategy}" if lockfile.strategy}
reporter.info "Install strategy changed from #{lockfile.strategy} to #{install_config.strategy}." if lockfile.strategy
if ::File.exists?(config.node_modules)
reporter.info "Removing the existing `node_modules` folder…"
FileUtils.rm_rf(config.node_modules)
end
if ::File.exists?(config.plug_and_play_modules)
reporter.info "Removing the existing plug'n'play `.pnp` folder…"
FileUtils.rm_rf(config.plug_and_play_modules)
end
if ::File.exists?(Path.new(config.prefix, ".pnp.data.json"))
reporter.info "Removing the plug'n'play runtime files…"
FileUtils.rm_rf(Path.new(config.prefix, ".pnp.data.json"))
FileUtils.rm_rf(Path.new(config.prefix, ".pnp.cjs"))
FileUtils.rm_rf(Path.new(config.prefix, ".pnp.loader.mjs"))
end
end
lockfile.strategy = install_config.strategy
end

private def self.hoisting_check(install_config : Install::Config, lockfile : Lockfile, inferred_context : Zap::Config::InferredContext, reporter : Reporter)
if lockfile.update_hoisting_shasum(inferred_context.main_package)
if install_config.frozen_lockfile
# If the lockfile is frozen, raise an error
raise "The --frozen-lockfile flag is on but hoisting settings have been modified since the last lockfile update. Run `zap i --frozen-lockfile=false` to regenerate the lockfile and try again."
end

if lockfile.read_status.from_disk?
Log.debug { "Detected a change in hoisting options in the package.json file" }
reporter.info("Hoisting options were modified. The packages will be re-installed.")
install_config = install_config.copy_with(refresh_install: true)
end
end
end

private def self.package_extensions_check(install_config : Install::Config, lockfile : Lockfile, inferred_context : Zap::Config::InferredContext, reporter : Reporter)
if lockfile.update_package_extensions_shasum(inferred_context.main_package)
if install_config.frozen_lockfile
# If the lockfile is frozen, raise an error
raise "The --frozen-lockfile flag is on but package extensions have been modified since the last lockfile update. Run `zap i --frozen-lockfile=false` to regenerate the lockfile and try again."
end

if lockfile.read_status.from_disk?
Log.debug { "Detected a change in package extensions options in the package.json file" }
reporter.info("Package extensions have been modified. Package metadata will forcefully be fetched from the registry and packages will be re-installed.")
install_config = install_config.copy_with(force_metadata_retrieval: true, refresh_install: true)
end
end
end

private def self.remove_packages(state : State)
return unless state.install_config.removed_packages.size > 0
Log.debug { "• Removing packages" }
Expand Down
2 changes: 1 addition & 1 deletion src/installer/pnp/pnp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Zap::Installer::PnP < Zap::Installer::Base
def initialize(state : Commands::Install::State)
super(state)
@node_modules = Path.new(state.config.node_modules)
@modules_store = @node_modules / ".pnp"
@modules_store = Path.new(state.config.plug_and_play_modules)
@relative_modules_store = Path.posix(@modules_store).relative_to(state.config.prefix)
Utils::Directories.mkdir_p(@modules_store)
end
Expand Down
1 change: 1 addition & 0 deletions src/lockfile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Zap::Lockfile
property overrides : Package::Overrides? = nil
@hoisting_shasum : String? = nil
@package_extensions_shasum : String? = nil
property strategy : Commands::Install::Config::InstallStrategy? = nil
@[YAML::Field(converter: Zap::Utils::OrderedHashConverter(String, Zap::Package))]
getter packages : Hash(String, Package) do
Hash(String, Package).new
Expand Down

0 comments on commit 0ab4235

Please sign in to comment.