[
  {
    "path": ".gitignore",
    "content": "node_modules/\n.idea/\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2013 staticfile\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# 介绍\n\n[staticfile.org](http://staticfile.org)命令行工具\n\n# 安装\n\n```\nnpm install -g sfile\n```\n\n# 使用\n\n## 快捷搜索\n\n快速搜索关键字\n\n```\nsfile [library]\n```\n\n![快速搜索](https://github.com/staticfile/cli/raw/master/docs/quicksearch.png)\n\n## 搜索\n\n搜索关键字\n\n```\nsfile search [library]\n\nOptions:\n  -s, --ssl      Output HTTPS link\n  -h, --html     Output HTML\n  -j, --jade     Output Jade Markup\n  -n, --no-link  Do not show the link for latest version\n```\n\n## 获取链接\n\n获取一个库的所有文件链接\n\n```\nsfile get [library]\n\nOptions:\n  -s, --ssl      Output HTTPS link\n  -h, --html     Output HTML\n  -j, --jade     Output Jade Markup\n  -v, --version  Use given version, leave empty if you want to see all supported version\n  -c, --copy     Copy to your system clipboard\n```\n\n## 查看版本\n\n```\nsfile --version\n```\n"
  },
  {
    "path": "bin/sfile",
    "content": "#!/usr/bin/env node\nvar sfile = require('../lib')\n  , clip = require('cliparoo')\n  , opt = require('optimist')\n  , argv = opt\n    .usage(\"[Usage]\\n\\nsfile [command|library]\\n\\nCommands:\\n\\n  s, search [library]\\tTo search the library...\\n  g, get [library]\\tTo get the library assets links...\")\n    .argv;\n\nswitch (argv._[0]) {\n  case 'i':\n  case 'info':\n    var prettyjson = require('prettyjson')\n    var keyword = argv._[1];\n    if (!keyword || argv.help) {\n      opt.showHelp();\n      return;\n    }\n    keyword = keyword.trim();\n\n    sfile.info(keyword, function (err, data) {\n      if (err) {\n        return sfile.error(err);\n      }\n\n      if (data.assets) {\n        data.assets = [data.assets[0]]\n      }\n\n      console.log(prettyjson.render(data));\n    })\n    break;\n  case 'search':\n  case 's':\n    argv = opt\n      .usage(\"sfile \" + argv._[0] + \" [library]\")\n      .alias('s', 'ssl')\n      .alias('h', 'html')\n      .alias('j', 'jade')\n      .alias('n', 'no-link')\n      .describe('s', 'Output HTTPS link')\n      .describe('h', 'Output HTML')\n      .describe('j', 'Output Jade Markup')\n      .describe('n', 'Do not show the link for latest version')\n      .argv;\n\n    var keyword = argv._[1];\n    if (!keyword || argv.help) {\n      opt.showHelp();\n      return;\n    }\n    keyword = keyword.trim();\n\n    sfile.search(keyword, function (err, data) {\n      if (err) return sfile.error(err);\n\n      if (!data.total) {\n        return printLn(\"没有结果\");\n      }\n\n      printLn((\"搜索 \" + keyword.bold + \" 共有 \" + (data.total + \"\").bold + \" 个库，当前列出 \" + (data.libs.length + \"\").bold + \" 个:\"));\n      printLn(\"\");\n\n      data.libs.forEach(function (lib, index) {\n        printLn(((index > 9 ? index : \" \" + index) + \")\").grey + \" \" + lib.name.replace(keyword, keyword.bold) + \" [\" + lib.version.green + \"]\");\n\n        if (!argv.n) {\n          var url = sfile.url(\"/\" + lib.name + \"/\" + lib.version + \"/\" + lib.filename, argv.ssl);\n\n          if (argv.html) {\n            url = sfile.html(url);\n          } else if (argv.jade) {\n            url = sfile.jade(url);\n          }\n\n          printLn((\"    \" + url).grey);\n        }\n\n        printLn(\"\");\n      });\n    });\n    break;\n  case \"get\":\n  case \"g\":\n    argv = opt\n      .usage(\"sfile \" + argv._[0] + \" [library]\")\n      .alias('h', 'html')\n      .alias('j', 'jade')\n      .alias('s', 'ssl')\n      .alias('c', 'copy')\n      .alias('v', 'version')\n      .describe('v', 'Use given version, leave empty if you want to see all supported version ')\n      .describe('h', 'Output HTML')\n      .describe('j', 'Output Jade Markup')\n      .describe('s', 'Output HTTPS link')\n      .describe('c', 'Copy to your system clipboard')\n      .argv;\n\n    var keyword = argv._[1];\n    if (!keyword || argv.help) {\n      opt.showHelp();\n      return;\n    }\n    keyword = keyword.trim();\n    sfile.get(keyword, function (err, lib, suggest) {\n      if (err) return sfile.error(err);\n\n      if (!lib) {\n        printLn(\"未找到库: \" + keyword);\n        // 搜索建议\n        if (suggest.length > 0) {\n          printLn(\"\");\n          for (var i in suggest) {\n            suggest[i] = suggest[i].underline;\n          }\n          printLn(\"你是不是要找: \".cyan + suggest.slice(0, 5).join(\" \"));\n        }\n        return;\n      }\n\n      var version = (argv.version && argv.version !== true) ? argv.version : \"\" + lib.version;\n      var matched = false;\n\n      // 匹配package.json给出的版本\n      lib.assets.forEach(function (asset) {\n        if (asset.version == version) {\n          if (matched) return;\n          matched = asset;\n        }\n      });\n\n      // 未匹配容错\n      if (!matched && lib.assets[0] && !argv.version) {\n        matched = lib.assets[0];\n      }\n\n      if (!matched) {\n        printLn(\"未找到 \" + keyword + \" [\" + (argv.version + \"\").red + \"]\");\n      } else {\n        printLn(\"找到 \" + lib.name.bold + \" [\" + version.green + \"]:\");\n        printLn(\"\");\n\n        var urls = [];\n        matched.files.forEach(function (file) {\n          var path = \"/\" + lib.name + \"/\" + version + \"/\" + file;\n          var url = sfile.url(path, argv.ssl);\n          if (argv.html) {\n            url = sfile.html(url);\n          } else if (argv.jade) {\n            url = sfile.jade(url);\n          }\n\n          url && printLn(url) && urls.push(url);\n        });\n\n        // 复制到剪贴板\n        if (argv.copy) {\n          clip(urls.join(argv.html || argv.jade ? \"\\n\" : \"\\\\\\\\n\"), function (err) {\n            if (err) return sfile.error(err);\n\n            printLn(\"✔ 已复制到剪贴板\".green);\n          });\n        }\n      }\n      printLn(\"\");\n      if (argv.version)\n        printLn(\"支持的版本号: \".cyan + lib.assets.map(function (asset) {\n          return asset.version.underline\n        }).join(\" \"));\n    });\n    break\n  default:\n    if (argv.version || argv.v) {\n      var json = require(\"../package.json\");\n      printLn(json.version);\n    } else if (argv._[0]) {\n      var List = require('term-list')\n        , list = new List({ marker: '\\033[36m› \\033[0m', markerLength: 2 })\n        , styles = ['', 'html', 'jade']\n        , current_style_index = 0\n\n      keyword = argv._[0].trim();\n\n      sfile.search(keyword, function (err, data) {\n        if (err) return sfile.error(err);\n\n        if (!data.total) {\n          return printLn(\"没有结果\");\n        }\n\n        printLn((\"搜索 \" + keyword.bold + \" 共有 \" + (data.total + \"\").bold + \" 个库，当前列出 \" + (data.libs.length + \"\").bold + \" 个:\"));\n\n        data.libs.forEach(function (lib, index) {\n          var url = sfile.url(\"/\" + lib.name + \"/\" + lib.version + \"/\" + lib.filename);\n          lib.index = index;\n          lib.base = lib.name.replace(keyword, keyword.bold) + \" [\" + lib.version.green + \"] \";\n          lib.url = url;\n          lib.to_copy = url;\n          lib.subLabel = lib.filename;\n          lib.current = 'url';\n          list.add(lib, lib.base + lib.subLabel.grey);\n        });\n\n        var changeStyle = function (reverse) {\n          if (reverse) {\n            current_style_index = current_style_index - 1 < 0 ? 2 : current_style_index - 1;\n          } else {\n            current_style_index = current_style_index + 1 > 2 ? 0 : current_style_index + 1;\n          }\n          var current_style = styles[current_style_index];\n\n          list.items.forEach(function (it, i) {\n            var current = list.at(i);\n            current.label = it.id.base + it.id.subLabel.grey + (current_style && (' [' + current_style + ']').red);\n            current.id.to_copy = current_style ? sfile[current_style](it.id.url) : it.id.url;\n          });\n          list.draw();\n        }\n\n        list.start();\n\n        list.on(\"keypress\", function (key, item) {\n          switch (key.name) {\n            case \"return\":\n              clip(item.to_copy, function (err) {\n                if (err) return sfile.error(err);\n\n                printLn(\"✔ 已复制到剪贴板\".green);\n              });\n              list.stop();\n              break;\n            case \"left\":\n              changeStyle(true);\n              break;\n            case \"right\":\n              changeStyle(false);\n              break;\n          }\n        });\n\n        list.on('empty', function () {\n          list.stop();\n        });\n      });\n    } else {\n      opt.showHelp();\n    }\n}\n\nfunction printLn() {\n  var args = Array.prototype.slice.call(arguments, 0);\n  args[0] = \" \" + args[0];\n  console.log.apply(this, args);\n  return true;\n}\n"
  },
  {
    "path": "lib/index.js",
    "content": "var request = require('request')\n  , colors = require('colors')\n  , url = \"http://api.staticfile.org/v1/search?count=10&q=\"\n  , show_url = \"http://api.staticfile.org/v1/packages/\"\n\nexports.search = function (keyword, cb) {\n  request.get({url: url + encodeURIComponent(keyword), json: true}, function (err, response, data) {\n    return cb(err, data);\n  });\n};\n\nexports.get = function (keyword, cb) {\n  request.get({url: url + encodeURIComponent(keyword), json: true}, function (err, response, data) {\n    if (err) return cb(err);\n\n    if (!data.libs) return cb(null, false);\n\n    var matched_lib = false;\n    var suggest = [];\n\n    data.libs.forEach(function (lib) {\n      if (lib.name.toLowerCase() == keyword.toLowerCase()) matched_lib = lib;\n      suggest.push(lib.name);\n    });\n\n    return cb(null, matched_lib, suggest);\n  });\n};\n\nexports.info = function (keyword, cb) {\n  request.get({url: show_url + encodeURIComponent(keyword), json: true}, function (err, response, data) {\n    if (err) return cb(err);\n\n    if (data.hasOwnProperty('success') && !data.success) return cb(new Error(\"Package '\" + keyword + \"' not exist\"));\n\n    return cb(null, data);\n  });\n}\n\nexports.error = function (e) {\n  console.log(\"错误\".redBG + \" \" + (e instanceof Object ? e.message : e));\n};\n\nexports.loading = function () {\n  console.log(\"加载中...\".grey);\n};\n\nexports.html = function (file) {\n  var ext = exports.ext(file);\n  switch (ext) {\n    case \"js\":\n      return '<script type=\"text/javascript\" src=\"' + file + '\"></script>';\n      break;\n    case 'css':\n      return '<link type=\"text/css\" rel=\"stylesheet\" href=\"' + file + '\"/>';\n      break;\n    default:\n      return false;\n  }\n};\n\nexports.jade = function (file) {\n  var ext = exports.ext(file);\n  switch (ext) {\n    case \"js\":\n      return 'script(type=\"text/javascript\" src=\"' + file + '\")'\n      break;\n    case 'css':\n      return 'link(type=\"text/css\" rel=\"stylesheet\" href=\"' + file + '\")';\n      break;\n    default:\n      return false;\n  }\n};\n\nexports.ext = function (filename) {\n  var i = filename.lastIndexOf('.');\n  return (i < 0) ? '' : filename.substr(i + 1);\n};\n\nexports.url = function (path, ssl) {\n  ssl = ssl || false;\n\n  return (ssl ? '//dn-staticfile.qbox.me' : 'http://cdn.staticfile.org') + path;\n};\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"sfile\",\n  \"version\": \"0.1.2\",\n  \"description\": \"The static library search and cli for staticfile.org.\",\n  \"author\": \"hfcorriez <hfcorriez@gmail.com>\",\n  \"licenses\": [\n    {\n      \"type\": \"MIT\",\n      \"url\": \"https://github.com/staticfile/cli/blob/master/LICENSE\"\n    }\n  ],\n  \"homepage\": \"http://staticfile.org\",\n  \"engines\": {\n    \"node\": \"*\"\n  },\n  \"dependencies\": {\n    \"optimist\": \"0.6.0\",\n    \"colors\": \"0.6.2\",\n    \"request\": \"2.27.0\",\n    \"cliparoo\": \"1.0.0\",\n    \"term-list\": \"0.2.0\",\n    \"prettyjson\": \"~0.12.0\"\n  },\n  \"bin\": {\n    \"sfile\": \"bin/sfile\"\n  }\n}\n"
  }
]