├── .gitignore ├── lib ├── cdnjs_command │ ├── params.rb │ ├── fetcher.rb │ ├── helpers.rb │ └── package.rb └── cdnjs_command.rb ├── HISTORY.md ├── cdnjs-command.gemspec ├── LICENSE ├── README.md └── bin └── cdnjs /.gitignore: -------------------------------------------------------------------------------- 1 | /pkg 2 | -------------------------------------------------------------------------------- /lib/cdnjs_command/params.rb: -------------------------------------------------------------------------------- 1 | module CdnjsCommand::Params 2 | def extract(what) i = index(what) and slice!(i, 2)[1] end; 3 | def first_is(what) shift if what.include?(self.first); end 4 | def self.[](*what) what.extend Params; end 5 | def ===(argv) argv.first_is(self); end 6 | end 7 | 8 | -------------------------------------------------------------------------------- /lib/cdnjs_command.rb: -------------------------------------------------------------------------------- 1 | require 'open-uri' 2 | require 'fileutils' 3 | require 'json' 4 | 5 | CDNJS_VERSION = "0.0.6" 6 | 7 | module CdnjsCommand 8 | autoload :Params, File.expand_path('../cdnjs_command/params', __FILE__) 9 | autoload :Fetcher, File.expand_path('../cdnjs_command/fetcher', __FILE__) 10 | autoload :Package, File.expand_path('../cdnjs_command/package', __FILE__) 11 | autoload :Helpers, File.expand_path('../cdnjs_command/helpers', __FILE__) 12 | end 13 | -------------------------------------------------------------------------------- /HISTORY.md: -------------------------------------------------------------------------------- 1 | v0.0.6 - Nov 07, 2011 2 | --------------------- 3 | 4 | Fixed 1.8.7 compatibility. 5 | 6 | v0.0.5 - Jul 24, 2011 7 | --------------------- 8 | 9 | Update base URL to the new `cloudflare.com` URL. 10 | 11 | v0.0.4 - Jul 24, 2011 12 | --------------------- 13 | 14 | Calling `cdnjs` actually works now. 15 | 16 | v0.0.3 - Apr 16, 2011 17 | --------------------- 18 | 19 | Using `gem install cdnjs-command` should now install the cdnjs executable. (@mehowte) 20 | 21 | v0.0.2 - Mar 27, 2011 22 | --------------------- 23 | 24 | Fixed `cdnjs url`. 25 | 26 | v0.0.1 - Mar 27, 2011 27 | --------------------- 28 | 29 | Initial. 30 | -------------------------------------------------------------------------------- /cdnjs-command.gemspec: -------------------------------------------------------------------------------- 1 | require './lib/cdnjs_command' 2 | 3 | Gem::Specification.new do |s| 4 | s.name = "cdnjs-command" 5 | s.version = CDNJS_VERSION 6 | s.summary = %{Command line helper for cdnjs.com.} 7 | s.description = %Q{This package lets you download the popular JavaScript packages with one command, thanks to www.cdnjs.com.} 8 | s.authors = ["Rico Sta. Cruz"] 9 | s.email = ["rico@sinefunc.com"] 10 | s.homepage = "http://github.com/rstacruz/cdnjs-command" 11 | s.files = Dir["{bin,lib,test}/**/*", "*.md", "Rakefile"].reject { |f| File.directory?(f) } 12 | s.executables = Dir["bin/*"].map { |f| File.basename(f) } 13 | 14 | s.add_dependency "json" 15 | end 16 | -------------------------------------------------------------------------------- /lib/cdnjs_command/fetcher.rb: -------------------------------------------------------------------------------- 1 | module CdnjsCommand::Fetcher 2 | CACHE_PATH = "~/.cache/cdnjs" 3 | 4 | extend CdnjsCommand::Helpers 5 | 6 | def self.fetch(url) 7 | return cache_for(url) if cached?(url) 8 | 9 | tip "Fetching #{url}..." 10 | output = open(url).read 11 | 12 | FileUtils.mkdir_p File.expand_path(CACHE_PATH) 13 | File.open(cache_path_for(url), 'w') { |f| f.write output } 14 | output 15 | rescue => e 16 | tip "Unable to fetch (#{e.class.name}: #{e.message})." 17 | exit 18 | end 19 | 20 | def self.purge 21 | FileUtils.rm_rf File.expand_path(CACHE_PATH) 22 | end 23 | 24 | def self.cached?(url) 25 | File.file? cache_path_for(url) 26 | end 27 | 28 | def self.cache_path_for(url) 29 | File.expand_path slug(url), CACHE_PATH 30 | end 31 | 32 | def self.cache_for(url) 33 | File.open(cache_path_for(url)) { |f| f.read } 34 | end 35 | 36 | def self.slug(url) 37 | url.gsub(/[\/:]/, '_') 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Rico Sta. Cruz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/cdnjs_command/helpers.rb: -------------------------------------------------------------------------------- 1 | module CdnjsCommand::Helpers 2 | def tip(message) 3 | $stderr.write "#{message}\n" 4 | end 5 | 6 | def invalid_usage(usage="cdnjs --help") 7 | puts "Invalid usage." 8 | puts "Try: #{usage.gsub(/^cdnjs/, File.basename($0))}" 9 | exit 10 | end 11 | 12 | def get_package(id) 13 | Package[id] or begin 14 | tip "Package not found." 15 | tip "For a list of all packages, see: cdnjs list" 16 | exit 17 | end 18 | end 19 | 20 | def show_help 21 | tip "Usage:" 22 | tip " cdnjs NAME - Download specific package" 23 | tip "" 24 | tip "Other commands:" 25 | tip " cdnjs list - List available packages" 26 | tip " cdnjs info NAME - Show package info" 27 | tip " cdnjs html NAME - Show HTML snippet to include given package" 28 | tip " cdnjs url NAME - Show the URL for the given package" 29 | tip " cdnjs update - Updates the package cache" 30 | tip "" 31 | tip "You may also use the first letters of each command; e.g., `cdnjs i jquery`." 32 | tip "Here are some common examples:" 33 | tip "" 34 | tip " $ cdnjs jquery" 35 | tip " $ echo `cdnjs h underscore` >> index.html" 36 | tip "" 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/cdnjs_command/package.rb: -------------------------------------------------------------------------------- 1 | class CdnjsCommand::Package 2 | INDEX_URL = 'http://cdnjs.com/packages.json' 3 | 4 | attr_reader :data, :name, :version, :description, :homepage, 5 | :maintainers, :repositories 6 | 7 | def initialize(hash) 8 | @data = hash 9 | @name = hash['name'].gsub(/\.js$/, '') 10 | @version = hash['version'] 11 | @description = hash['description'] 12 | @homepage = hash['homepage'] 13 | @filename = hash['filename'] 14 | @maintainers = hash['maintainers'] 15 | @repositories = hash['repositories'] 16 | end 17 | 18 | def basename 19 | "#{name}-#{version}.js" 20 | end 21 | 22 | def url 23 | if @filename =~ /^http/ 24 | @filename 25 | else 26 | "http://cdnjs.cloudflare.com/ajax/libs/#{data['name']}/#{version}/#{@filename}" 27 | end 28 | end 29 | 30 | # @return Array of {Package}s 31 | def self.all 32 | packages = JSON.parse(fetch(INDEX_URL))['packages'] 33 | packages.delete(Hash.new) 34 | packages. 35 | map { |hash| Package.new(hash) }. 36 | sort_by { |pkg| pkg.name } 37 | end 38 | 39 | def self.fetch(url) 40 | Fetcher.fetch(INDEX_URL).force_encoding('utf-8') 41 | end 42 | 43 | def self.[](id) 44 | all.detect { |pkg| pkg.name == id } || 45 | all.detect { |pkg| pkg.name[0...id.size] == id } 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cdnjs-command 2 | #### Command line helper for cdnjs.com. 3 | 4 | This makes including JS files in your project easy-peasy. To add a JS libary 5 | to your project: 6 | 7 | $ cdnjs jquery 8 | Created jquery-1.5.1.js. 9 | 10 | You probably want to link the online version as well: 11 | 12 | $ cdnjs html jquery 13 | 14 | 15 | Or you may want just the URL: 16 | 17 | $ cdnjs url underscore 18 | http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js 19 | 20 | ## Installation 21 | 22 | This is a Ruby gem that requires Ruby 1.8+. 23 | 24 | $ gem install cdnjs-command 25 | 26 | ## Usage 27 | 28 | Usage: 29 | cdnjs NAME - Download specific package 30 | 31 | Other commands: 32 | cdnjs list - List available packages 33 | cdnjs info NAME - Show package info 34 | cdnjs html NAME - Show HTML snippet to include given package 35 | cdnjs url NAME - Show the URL for the given package 36 | cdnjs update - Updates the package cache 37 | 38 | You may also use the first letters of each command; e.g., `cdnjs i jquery`. 39 | Here are some common examples: 40 | 41 | $ cdnjs jquery 42 | $ echo `cdnjs h underscore` >> index.html 43 | 44 | ## Acknowledgements 45 | 46 | * [www.cdnjs.com](http://cdnjs.com) 47 | -------------------------------------------------------------------------------- /bin/cdnjs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require File.expand_path('../../lib/cdnjs_command', __FILE__) 4 | 5 | Package = CdnjsCommand::Package 6 | Fetcher = CdnjsCommand::Fetcher 7 | Params = CdnjsCommand::Params 8 | 9 | extend CdnjsCommand::Helpers 10 | ARGV.extend Params 11 | 12 | # ============================================================================ 13 | # Controller 14 | 15 | case ARGV 16 | when Params['-h', '--help'] 17 | show_help 18 | 19 | when Params['-v', '--version'] 20 | puts "cdnjs-command #{CDNJS_VERSION}" 21 | 22 | when Params['list', 'l'] 23 | Package.all.each { |pkg| 24 | puts "%-20s %s" % [ pkg.name + " (" + pkg.version + ")", ""] 25 | } 26 | 27 | when Params['html', 'h'] 28 | name = ARGV.shift or invalid_usage("cdnjs html NAME") 29 | pkg = get_package(name) 30 | 31 | puts "" 32 | 33 | when Params['url', 'u'] 34 | name = ARGV.shift or invalid_usage("cdnjs url NAME") 35 | puts get_package(name).url 36 | 37 | when Params['info', 'i'] 38 | name = ARGV.shift or invalid_usage("cdnjs info NAME") 39 | pkg = get_package(name) 40 | 41 | puts "%s (%s)" % [ pkg.name, pkg.version ] 42 | puts "" 43 | puts "%-10s %s" % [ "URL:", pkg.url ] 44 | puts "%-10s %s" % [ "Homepage:", pkg.homepage ] 45 | puts "" 46 | puts pkg.description 47 | puts "" 48 | puts "" 49 | puts "" 50 | 51 | if pkg.url =~ /^https/ 52 | puts "" 53 | puts "" 54 | end 55 | 56 | 57 | p pkg if ARGV.delete('-v') 58 | 59 | when Params['update'] 60 | Fetcher.purge 61 | Package.all 62 | tip "Done." 63 | 64 | else 65 | name = ARGV.shift or (show_help; exit) 66 | pkg = Package[name] or begin 67 | if ARGV.empty? 68 | puts "Invalid package name." 69 | else 70 | puts "Invalid usage." 71 | end 72 | puts "For a list of all packages: cdnjs list" 73 | puts "For more help: cdnjs --help" 74 | exit 75 | end 76 | 77 | data = Fetcher.fetch(pkg.url) 78 | File.open(pkg.basename, 'w') { |f| f.write data } 79 | 80 | tip "Created #{pkg.basename}." 81 | end 82 | --------------------------------------------------------------------------------