-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathyarn_check.rb
37 lines (31 loc) · 1.37 KB
/
yarn_check.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true
module Overcommit::Hook::PreCommit
# Check if local yarn.lock matches package.json when either changes, unless
# yarn.lock is ignored by git.
#
# @see https://yarnpkg.com/en/docs/cli/check
class YarnCheck < Base
LOCK_FILE = 'yarn.lock'
# 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
ACTIONABLE_ERRORS = [
'Lockfile does not contain pattern',
].freeze
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
end
end