-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathnpm_commands.rb
55 lines (51 loc) · 1.51 KB
/
npm_commands.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
# NPM wrapper with helpful error messages
class NpmCommands
# @return [Boolean] whether the installation succeeded
def install(*args)
return false unless check_nodejs_installed
STDERR.puts 'Installing npm dependencies...'
install_status = Dir.chdir File.expand_path('..', File.dirname(__FILE__)) do
system('npm', 'install', *args)
end
STDERR.puts(
*if install_status
['npm dependencies installed']
else
['-' * 60,
'Error: npm dependencies installation failed',
'-' * 60]
end
)
install_status
end
private
def check_nodejs_installed
return true if executable?('node')
STDERR.puts(
'-' * 60,
'Error: critical-path-css-rails requires NodeJS and NPM.',
*if executable?('brew')
[' To install NodeJS and NPM, run:',
' brew install node']
elsif Gem.win_platform?
[' To install NodeJS and NPM, we recommend:',
' https://github.com/coreybutler/nvm-windows/releases']
else
[' To install NodeJS and NPM, we recommend:',
' https://github.com/creationix/nvm']
end,
'-' * 60
)
end
def executable?(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
nil
end
end