Skip to content

Commit 2c8014b

Browse files
committed
Install npm dependencies on Gem.pre_install
1 parent 47d7896 commit 2c8014b

File tree

7 files changed

+717
-3
lines changed

7 files changed

+717
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ test/version_tmp
1717
tmp
1818

1919
.DS_Store
20+
/node_modules/

Gemfile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
# A sample Gemfile
2-
source "https://rubygems.org"
1+
source 'https://rubygems.org'
32

4-
gem 'rubocop', require: false
3+
gemspec
4+
5+
gem 'rubocop', require: false
6+
7+
# Gem hooks are not triggered for the gemspec gem
8+
# https://github.com/bundler/bundler/issues/2354
9+
# Work around this by requiring the hooks here
10+
require_relative './lib/rubygems_plugin'

Rakefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
desc 'Update the vendored version of penthouse'
2+
task :update_penthouse do # rubocop:disable Metrics/BlockLength
3+
require 'net/http'
4+
require 'uri'
5+
fetch = lambda do |url|
6+
uri = URI.parse(url)
7+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
8+
http.request(Net::HTTP::Get.new(uri.path, 'User-Agent' => 'Ruby'))
9+
end
10+
case response
11+
when Net::HTTPSuccess
12+
response.body
13+
when Net::HTTPRedirection
14+
# unpkg.com returns a relative URL in the `location` field.
15+
location = uri.clone
16+
location.path = URI.parse(response['location']).path
17+
location
18+
else
19+
response.error!
20+
end
21+
end
22+
local_root = 'lib/penthouse'
23+
remote_root = "#{fetch['https://unpkg.com/penthouse']}/lib"
24+
25+
File.write File.join(local_root, 'timeago.js'),
26+
fetch["#{remote_root}/dist/timeago.js"]
27+
File.write File.join(local_root, 'timeago.locales.js'),
28+
fetch["#{remote_root}/dist/timeago.locales.min.js"]
29+
src = fetch.call.body
30+
# Remove the source mapping comment as this gem does not bundle source maps:
31+
src.sub!(%r{^//# sourceMappingURL=.*\n\z}, '')
32+
File.write(File.join('lib/penthouse/penthouse.js'), src)
33+
34+
version_path = File.join('lib/popper_js/version.rb')
35+
File.write version_path,
36+
File.read(version_path)
37+
.sub(/VERSION = '.*?'/,
38+
"VERSION = '#{uri.path.split('@')[-1]}'")
39+
40+
STDERR.puts "Updated from #{uri}"
41+
end

lib/npm_commands.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
# NPM wrapper with helpful error messages
4+
class NpmCommands
5+
6+
# @return [Boolean] whether the installation succeeded
7+
def install # rubocop:disable Metrics/MethodLength
8+
return false unless check_nodejs_installed
9+
STDERR.puts
10+
install_status = system('npm install')
11+
STDERR.puts(
12+
*if install_status
13+
['npm dependencies installed']
14+
else
15+
['-' * 60,
16+
'Error: npm dependencies installation failed',
17+
'-' * 60]
18+
end
19+
)
20+
install_status
21+
end
22+
23+
private
24+
25+
def check_nodejs_installed # rubocop:disable Metrics/MethodLength
26+
return true if executable?('node')
27+
STDERR.puts(
28+
'-' * 60,
29+
'Error: critical-path-css-rails requires NodeJS and NPM.',
30+
*if executable?('brew')
31+
[' To install NodeJS and NPM, run:',
32+
' brew install node']
33+
elsif Gem.win_platform?
34+
[' To install NodeJS and NPM, we recommend:',
35+
' https://github.com/coreybutler/nvm-windows/releases']
36+
else
37+
[' To install NodeJS and NPM, we recommend:',
38+
' https://github.com/creationix/nvm']
39+
end,
40+
'-' * 60
41+
)
42+
end
43+
44+
def executable?(cmd)
45+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
46+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
47+
exts.each do |ext|
48+
exe = File.join(path, "#{cmd}#{ext}")
49+
return exe if File.executable?(exe) && !File.directory?(exe)
50+
end
51+
end
52+
nil
53+
end
54+
end

lib/rubygems_plugin.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Gem.pre_install do
2+
require_relative './npm_commands'
3+
NpmCommands.new.install
4+
end

0 commit comments

Comments
 (0)