Skip to content

Instantly share code, notes, and snippets.

@justinweiss
Last active September 6, 2019 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinweiss/8864c537eb254497af89755cd02f9006 to your computer and use it in GitHub Desktop.
Save justinweiss/8864c537eb254497af89755cd02f9006 to your computer and use it in GitHub Desktop.
Publish a version of an npm package to your own scope, without (permanently) changing package.json
#!/usr/bin/env ruby
require "json"
require "optparse"
$options = {}
def system!(*args)
puts "> #{args.join(" ")}"
system(*args) || raise("Error running #{args.join(" ")}: #{$?} ")
end
option_parser = OptionParser.new do |parser|
parser.banner = <<~HELP
Usage: npm-publish-fork [options] npm_scope
Example: npm-publish-fork --set-version 0.10.1 @my-private-scope
HELP
parser.on("--set-version VERSION", "Specify a different published version") { |version| $options[:version] = version}
end
option_parser.parse!
$options[:scope] ||= ARGV[1]
abort("Scope must be specified.\n#{option_parser}") unless $options[:scope]
json = JSON.parse(File.read("package.json"))
previous_name = json["name"]
previous_version = json["version"]
begin
json["name"] = previous_name.sub(/^(@[^\/]+\/)?/, "#{$options[:scope]}/")
json["version"] = $options[:version] if $options[:version]
File.write("package.json", JSON.pretty_generate(json) + "\n")
system!("npm publish")
ensure
json["name"] = previous_name
json["version"] = previous_version if $options[:version]
File.write("package.json", JSON.pretty_generate(json) + "\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment