Class: Overcommit::Hook::PreCommit::YarnCheck

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook/pre_commit/yarn_check.rb

Overview

Check if local yarn.lock matches package.json when either changes, unless yarn.lock is ignored by git.

Constant Summary collapse

LOCK_FILE =
'yarn.lock'
ACTIONABLE_ERRORS =

A lot of the errors returned by ‘yarn check` are outside the developer’s control (are caused by bad package specification, in the hands of the upstream maintainer) So limit reporting to errors the developer can do something about

[
  'Lockfile does not contain pattern',
].freeze

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#applicable_files, #command, #description, #enabled?, #excluded?, #execute, #execute_in_background, #flags, #in_path?, #included_files, #initialize, #name, #parallelize?, #processors, #quiet?, #required?, #required_executable, #required_libraries, #run?, #run_and_transform, #skip?

Constructor Details

This class inherits a constructor from Overcommit::Hook::Base

Instance Method Details

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/overcommit/hook/pre_commit/yarn_check.rb', line 18

def run
  # Ignore if yarn.lock is not tracked by git
  ignored_files = execute(%w[git ls-files -o -i --exclude-standard]).stdout.split("\n")
  return :pass if ignored_files.include?(LOCK_FILE)

  previous_lockfile = File.exist?(LOCK_FILE) ? File.read(LOCK_FILE) : nil
  result = execute(command)
  new_lockfile = File.exist?(LOCK_FILE) ? File.read(LOCK_FILE) : nil

  # `yarn check` also throws many warnings, which should be ignored here
  errors_regex = Regexp.new("^error (.*)(#{ACTIONABLE_ERRORS.join('|')})(.*)$")
  errors = errors_regex.match(result.stderr)
  unless errors.nil? && previous_lockfile == new_lockfile
    return :fail, "#{LOCK_FILE} is not up-to-date -- run `yarn install`"
  end

  :pass
end