Skip to content

Instantly share code, notes, and snippets.

@mikz
Created June 17, 2016 09:34
Show Gist options
  • Select an option

  • Save mikz/33cf8799ea5cac6c8fc92e04dfa71be9 to your computer and use it in GitHub Desktop.

Select an option

Save mikz/33cf8799ea5cac6c8fc92e04dfa71be9 to your computer and use it in GitHub Desktop.

Revisions

  1. mikz created this gist Jun 17, 2016.
    51 changes: 51 additions & 0 deletions jspm.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/usr/bin/env ruby

    require 'open-uri'
    require 'json'


    require 'shellwords'

    ENV['NODE_PATH'] = '/usr/local/lib/node_modules'
    # npm install -g licenses

    data = DATA.read

    define_method :autodetect do |name|
    `node -e "#{data}" #{name}`.strip
    end

    packages = `jspm inspect`.scan(/\s+(?<repo>\w+):(?<name>\S+)\s(?<version>\d[\.\d\w\-\s]+\S)\n/).map{|(repo, name, versions)| [repo, name, versions.split(' ')] }

    def each_version(packages, name, &block)
    packages.select{|(repo,_,_)| repo == name }.each{|(_,name,versions)| versions.each{|v| yield(name, v) } }
    end

    each_version(packages, 'github') do |name, version|
    puts "#{name.split(',').last}@#{version},TODO,https://github.com/#{name}"
    end

    each_version(packages, 'npm') do |name, version|
    url = "http://registry.npmjs.org/#{name}/#{version}"
    package = JSON.parse open("http://registry.npmjs.org/#{name}").read
    fallback = autodetect(name)
    license = package.fetch('license') { fallback.empty? ? 'UNKNOWN' : fallback }

    puts "#{name}@#{version},#{license},#{url}"
    end

    __END__
    'use strict';

    var licenses = require('licenses');

    licenses(process.argv[1], function fetched(err, license) {
    if (license) {
    console.log(license.join(','));
    process.exit(0);
    } else {
    console.error(err);
    process.exit(1);
    }
    });