Sha256: 82c5ec100a3c46e445b8c293f22008888627012c3e4af96608234574d5baf54c

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'json'
require 'tempfile'

module LicenseFinder
  class NPM < PackageManager
    def current_packages
      NpmPackage.packages_from_json(npm_json, detected_package_path)
    end

    def package_management_command
      'npm'
    end

    def prepare_command
      'npm install --no-save'
    end

    def possible_package_paths
      [project_path.join('package.json')]
    end

    def prepare
      prep_cmd = "#{prepare_command}#{production_flag}"
      _stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(prep_cmd) }

      return if status.success?

      log_errors stderr
      raise "Prepare command '#{prep_cmd}' failed" unless @prepare_no_fail
    end

    private

    def npm_json
      command = "#{package_management_command} list --json --long#{production_flag}"
      stdout, stderr, status = Dir.chdir(project_path) { Cmd.run(command) }
      # we can try and continue if we got an exit status 1 - unmet peer dependency
      raise "Command '#{command}' failed to execute: #{stderr}" if !status.success? && status.exitstatus != 1

      JSON.parse(stdout)
    end

    def production_flag
      return '' if @ignored_groups.nil?

      @ignored_groups.include?('devDependencies') ? ' --production' : ''
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
license_finder-6.9.0 lib/license_finder/package_managers/npm.rb
license_finder-6.8.2 lib/license_finder/package_managers/npm.rb
license_finder-6.8.1 lib/license_finder/package_managers/npm.rb
license_finder-6.8.0 lib/license_finder/package_managers/npm.rb
license_finder-6.7.0 lib/license_finder/package_managers/npm.rb
license_finder-6.6.2 lib/license_finder/package_managers/npm.rb
license_finder-6.6.1 lib/license_finder/package_managers/npm.rb
license_finder-6.6.0 lib/license_finder/package_managers/npm.rb
license_finder-6.5.0 lib/license_finder/package_managers/npm.rb
license_finder-6.4.0 lib/license_finder/package_managers/npm.rb
license_finder-6.3.0 lib/license_finder/package_managers/npm.rb
license_finder-6.2.0 lib/license_finder/package_managers/npm.rb
license_finder-6.1.2 lib/license_finder/package_managers/npm.rb
license_finder-6.1.0 lib/license_finder/package_managers/npm.rb