Class: Npm::Rails::PackageManager

Inherits:
Object
  • Object
show all
Defined in:
lib/npm/rails/package_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packages, root_path, env) ⇒ PackageManager

Returns a new instance of PackageManager.



11
12
13
14
15
# File 'lib/npm/rails/package_manager.rb', line 11

def initialize(packages, root_path, env)
  @packages = packages
  @root_path = root_path
  @env = env
end

Class Method Details

.build(root_path, package_file, env) ⇒ Object



5
6
7
8
9
# File 'lib/npm/rails/package_manager.rb', line 5

def self.build(root_path, package_file, env)
  package_file_path = "#{ root_path }/#{ package_file }"
  packages = PackageFileParser.parse(package_file_path)
  new(packages, root_path, env)
end

Instance Method Details

#to_npm_formatObject

Return string of packages for ‘npm install’ command



29
30
31
32
33
34
35
36
37
38
# File 'lib/npm/rails/package_manager.rb', line 29

def to_npm_format
  @packages.inject "" do |string, package|
    # do not add development packages in production environment
    if (@env.production? and package.development?)
      string
    else
      string << "#{ package.name }@\"#{ package.version }\" "
    end
  end
end

#write_bundle_fileObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/npm/rails/package_manager.rb', line 17

def write_bundle_file
  bundle_file_path = "#{ @root_path }/tmp/npm-rails/bundle.js"
  Dir.mkdir("tmp/npm-rails") unless File.exist?("tmp/npm-rails")
  File.open(bundle_file_path, "w") do |file|
    packages_for_bundle_file.each do |package|
      file.write "window.#{ package.build_name } = require('#{ package.name }')\n"
    end
  end
  bundle_file_path
end