1
0
mirror of https://github.com/jordansissel/fpm synced 2025-12-20 00:35:15 +01:00
fpm/bin/fpm-npm

96 lines
2.7 KiB
Ruby
Executable File

#!/usr/bin/env ruby
#
require "optparse"
require "ostruct"
require "json"
settings = OpenStruct.new
opts = OptionParser.new do |opts|
opts.on("-n PACKAGENAME", "--name PACKAGENAME",
"What name to give to the package") do |name|
settings.name = name
end
opts.on("-v VERSION", "--version VERSION",
"version to give the package") do |version|
settings.version = version
end
opts.on("-t OUTPUTTYPE", "--type OUTPUTTYPE",
"what kind of package you want as output (deb, etc)") do |packagetype|
settings.packagetype = packagetype
end
end
args = opts.parse(ARGV)
if !["deb"].include?(settings.packagetype)
$stderr.puts "Unsupported output package type '#{settings.packagetype}'." \
"Only supports 'deb' right now."
exit 1
end
if !settings.name
$stderr.puts "No npm package name given (missing -n?)"
exit 1
end
builddir="#{Dir.pwd}/npm2pkg"
# Prefix package names with 'nodejs-'
PACKAGEPREFIX = "nodejs-"
INSTALLPATH="/usr/lib/node"
Dir.mkdir(builddir) if !File.exists?(builddir)
File.open("#{builddir}/.npmrc", "w") do |file|
file.puts "root = #{builddir}#{INSTALLPATH}"
end
## Trick npm into using a custom .npmrc
system("env - PATH=$PATH HOME=#{builddir} npm install #{settings.name} #{settings.version}")
# Find all installed npms in builddir, make packages.
Dir.glob("#{builddir}#{INSTALLPATH}/.npm/*/*") do |path|
next if File.symlink?(path)
puts path
# Load the package.json and glean any information from it, then invoke pkg.rb
package = JSON.parse(File.new("#{path}/package/package.json").read())
# TODO(sissel): Ideally we want to say any version with the same 'release' number, like
# So we'll specify deps of {v}-1 <= x <= {v}-999999....
depends = Dir.glob("#{path}/dependson/*@*") \
.collect { |p| PACKAGEPREFIX + File.basename(p) } \
.collect { |p| n,v = p.split("@");
["#{n} (>= #{v}-1)", "#{n} (<= #{v}-99999999999999)"]
}.flatten
if package["author"]
maintainer = package["author"]
else
m = package["maintainers"][0] \
rescue { "name" => "missing upstream author", "email" => ENV["USER"] }
maintainer = "#{m["name"]} <#{m["email"]}>"
end
pkgcmd = [ "fpm",
"-n", "#{PACKAGEPREFIX}#{package["name"]}",
"-v", package["version"],
"-m", maintainer,
"-a", "all",
]
depends.each do |dep|
pkgcmd += ["-d", dep]
end
pkgcmd += ["-p", "#{PACKAGEPREFIX}#{package["name"]}-VERSION_ARCH.deb"]
pkgcmd += ["-C", builddir]
pkgcmd << "#{INSTALLPATH[1..-1]}/.npm/#{package["name"]}/active"
pkgcmd << "#{INSTALLPATH[1..-1]}/#{package["name"]}"
pkgcmd << "#{INSTALLPATH[1..-1]}/#{package["name"]}@#{package["version"]}"
puts pkgcmd.map { |x| "\"#{x}\"" }.join(" ")
system *pkgcmd
end