Class: FPM::Package::NPM

Inherits:
FPM::Package show all
Extended by:
Util
Defined in:
lib/fpm/package/npm.rb

Instance Attribute Summary

Attributes inherited from FPM::Package

#architecture, #attributes, #attrs, #category, #config_files, #conflicts, #dependencies, #description, #directories, #epoch, #iteration, #license, #maintainer, #name, #provides, #replaces, #scripts, #url, #vendor, #version

Instance Method Summary collapse

Methods included from Util

ar_cmd, ar_cmd_deterministic?, copied_entries, copy_entry, copy_metadata, default_shell, erbnew, execmd, expand_pessimistic_constraints, logger, program_exists?, program_in_path?, safesystem, safesystemout, tar_cmd, tar_cmd_supports_sort_names_and_set_mtime?

Methods inherited from FPM::Package

apply_options, #build_path, #cleanup, #cleanup_build, #cleanup_staging, #convert, #converted_from, default_attributes, #edit_file, #files, inherited, #initialize, option, #output, #script, #staging_path, #to_s, #type, type, types

Constructor Details

This class inherits a constructor from FPM::Package

Instance Method Details

#input(package) ⇒ Object



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fpm/package/npm.rb', line 21

def input(package)
  # Notes:
  # * npm respects PREFIX
  settings = {
    "cache" => build_path("npm_cache"),
    "loglevel" => "warn",
    "global" => "true"
  }

  settings["registry"] = attributes[:npm_registry] if attributes[:npm_registry_given?]
  set_default_prefix unless attributes[:prefix_given?]

  settings["prefix"] = staging_path(attributes[:prefix])
  FileUtils.mkdir_p(settings["prefix"])

  npm_flags = []
  settings.each do |key, value|
    # npm lets you specify settings in a .npmrc but the same key=value settings
    # are valid as '--key value' command arguments to npm. Woo!
    logger.debug("Configuring npm", key => value)
    npm_flags += [ "--#{key}", value ]
  end

  install_args = [
    attributes[:npm_bin],
    "install",
    # use 'package' or 'package@version'
   (version ? "#{package}@#{version}" : package)
  ]

  # The only way to get npm to respect the 'prefix' setting appears to
  # be to set the --global flag.
  #install_args << "--global"
  install_args += npm_flags

  safesystem(*install_args)

  # Query details about our now-installed package.
  # We do this by using 'npm ls' with json + long enabled to query details
  # about the installed package.
  npm_ls_out = safesystemout(attributes[:npm_bin], "ls", "--json", "--long", *npm_flags)
  npm_ls = JSON.parse(npm_ls_out)
  name, info = npm_ls["dependencies"].first

  self.name = [attributes[:npm_package_name_prefix], name].join("-")
  self.version = info.fetch("version", "0.0.0")

  if info.include?("repository")
    self.url = info["repository"]["url"]
  else
    self.url = "https://npmjs.org/package/#{self.name}"
  end

  self.description = info["description"]
  # Supposedly you can upload a package for npm with no author/author email
  # so I'm being safer with this. Author can also be a hash or a string
  self.vendor = "Unknown <[email protected]>"
  if info.include?("author")
    author_info = info["author"]
    # If a hash, assemble into a string
    if author_info.respond_to? :fetch
      self.vendor = sprintf("%s <%s>", author_info.fetch("name", "unknown"),
                            author_info.fetch("email", "[email protected]"))
    else
      # Probably will need a better check for validity here
      self.vendor = author_info unless author_info == ""
    end
  end

  # npm installs dependencies in the module itself, so if you do
  # 'npm install express' it installs dependencies (like 'connect')
  # to: node_modules/express/node_modules/connect/...
  #
  # To that end, I don't think we necessarily need to include
  # any automatic dependency information since every 'npm install'
  # is fully self-contained. That's why you don't see any bother, yet,
  # to include the package's dependencies in here.
  #
  # It's possible someone will want to decouple that in the future,
  # but I will wait for that feature request.
end