Skip to content

Commit

Permalink
#2
Browse files Browse the repository at this point in the history
  • Loading branch information
puneet committed Jan 3, 2017
1 parent e73a896 commit 659166d
Showing 1 changed file with 95 additions and 54 deletions.
149 changes: 95 additions & 54 deletions lib/language_pack/helpers/yarn_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@

class LanguagePack::Helpers::YarnWrapper
include LanguagePack::ShellHelpers

class PackageFileParseError < BuildpackError
def initialize(error)
msg = "There was an error parsing your package.json, we cannot continue\n"
msg << error
super msg
end
end
include YarnConfigFileHelper

VENDOR_DIR_PATH = './vendor'

def initialize
@vendor_dir_path = ENV['VENDOR_DIR_PATH'] || VENDOR_DIR_PATH
@vendor_path = Pathname.new(@vendor_dir_path).realpath
@yarn_fetcher = LanguagePack::Fetcher.new("https://yarnpkg.com/downloads/")
@node_fetcher = LanguagePack::Fetcher.new("https://nodejs.org/dist/")
vendor_dir_path = ENV['VENDOR_DIR_PATH'] || VENDOR_DIR_PATH
@vendor_path = Pathname.new(vendor_dir_path).realpath
end

def install_node_modules_and_dependencies
Expand All @@ -35,76 +26,126 @@ def install_node_modules_and_dependencies

def node_app?
FileUtils.chdir @vendor_path do
return yarn_lock_file_path && yarn_lock_file_path
return yarn_lock_file && package_file
end
end

private

def install_yarn
topic "installing yarn #{yarn_version}"
@yarn_fetcher.fetch_untar("#{yarn_version}/yarn-v#{yarn_version}.tar.gz", "dist/")
FileUtils.cp_r("dist/.", "./yarn")
FileUtils.rm_rf("dist")
YarnInstaller.new.perform
end

def install_node
untar_dir = "node-v#{node_version}-linux-x64"
topic "installing #{untar_dir}"
@node_fetcher.fetch_untar("v#{node_version}/#{untar_dir}.tar.gz", "#{untar_dir}/bin")
FileUtils.mkdir("./node")
FileUtils.cp_r("#{untar_dir}/.", "./node")
FileUtils.rm_rf(untar_dir)
NodeInstaller.new.perform
end

def install_packages
topic "installing node modules"
run! "../bin/yarn install"
topic "installing node packages done"
end

def yarn_version
@yarn_version ||= open("https://semver.herokuapp.com/yarn/resolve/#{provided_yarn_version}")
end
class VersionResolver

def node_version
@node_version ||= open("https://semver.herokuapp.com/node/resolve/#{provided_node_version}")
end
def resolve_node(provided_version)
open("https://semver.herokuapp.com/node/resolve/#{provided_version}")
end

def parsed_package_file
@parsed_package_file ||= begin
json = File.read(package_dot_json_file_path)
JSON.parse(json)
def resolve_yarn(provided_version)
open("https://semver.herokuapp.com/yarn/resolve/#{provided_version}")
end
end

def engine_config
@engine_config ||= parsed_package_file.fetch('engines', {})
end
private

def provided_node_version
engine_config['node']
end
def open uri
super(URI.escape(uri)).read
end

def provided_yarn_version
engine_config['yarn']
end

def yarn_lock_file_path
Dir["yarn.lock"].first
end
module YarnConfigFileHelper

def package_dot_json_file_path
Dir["package.json"].first
def parsed_package_file
@parsed_package_file ||= begin
json = File.read(package_file)
JSON.parse(json)
end
end

def engine_config
@engine_config ||= parsed_package_file.fetch('engines', {})
end

def node_version
engine_config['node']
end

def yarn_version
engine_config['yarn']
end

def yarn_lock_file
Dir["yarn.lock"].first
end

def package_file
Dir["package.json"].first
end
end

def instrument title
topic title
yield
class NodeInstaller
include YarnConfigFileHelper

def initialize
@node_fetcher = LanguagePack::Fetcher.new("https://nodejs.org/dist/")
end

def perform
topic "installing #{binary_name}"
@node_fetcher.fetch_untar(binary_path, "#{binary_name}/bin")
FileUtils.cp_r("#{binary_name}/.", "./node")
FileUtils.rm_rf(binary_name)
end

private

def version
@version ||= VersionResolver.new.resolve_node(node_version)
end

def binary_name
@binary_name ||= "node-v#{version}-linux-x64"
end

def binary_path
"v#{version}/#{binary_name}.tar.gz"
end

end

def open uri
super(URI.escape(uri)).read
class YarnInstaller
include YarnConfigFileHelper

def initialize
@yarn_fetcher = LanguagePack::Fetcher.new("https://yarnpkg.com/downloads/")
end

def perform
topic "installing yarn v#{version}"
@yarn_fetcher.fetch_untar(binary_path, "dist/")
FileUtils.cp_r("dist/.", "./yarn")
FileUtils.rm_rf("dist")
end

private

def version
@version ||= VersionResolver.new.resolve_yarn(yarn_version)
end

def binary_path
"#{version}/yarn-v#{version}.tar.gz"
end

end

end

0 comments on commit 659166d

Please sign in to comment.