diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d751acd --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +node_js: + - "6" + - "4" + - "0.12" +install: + - npm install +script: + - npm test \ No newline at end of file diff --git a/README.md b/README.md index 079acd2..f8af344 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ [staticfile.org](http://staticfile.org)命令行工具 +![](https://api.travis-ci.org/staticfile/cli.svg) + # 安装 ``` @@ -10,28 +12,12 @@ npm install -g sfile # 使用 -## 快捷搜索 - -快速搜索关键字 - -``` -sfile [library] -``` - -![快速搜索](https://github.com/staticfile/cli/raw/master/docs/quicksearch.png) - ## 搜索 搜索关键字 ``` sfile search [library] - -Options: - -s, --ssl Output HTTPS link - -h, --html Output HTML - -j, --jade Output Jade Markup - -n, --no-link Do not show the link for latest version ``` ## 获取链接 @@ -43,10 +29,6 @@ sfile get [library] Options: -s, --ssl Output HTTPS link - -h, --html Output HTML - -j, --jade Output Jade Markup - -v, --version Use given version, leave empty if you want to see all supported version - -c, --copy Copy to your system clipboard ``` ## 查看版本 diff --git a/bin/sfile b/bin/sfile index 1ca52b8..296ae1b 100755 --- a/bin/sfile +++ b/bin/sfile @@ -3,13 +3,13 @@ var sfile = require('../lib') , clip = require('cliparoo') , opt = require('optimist') , argv = opt - .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...") + .usage("[Usage]\n\nsfile [command]\n\nCommands:\n\n s, search [library]\tTo search the library...\n g, get [library]\tTo get the library assets links...") .argv; switch (argv._[0]) { case 'i': case 'info': - var prettyjson = require('prettyjson') + var prettyjson = require('prettyjson'); var keyword = argv._[1]; if (!keyword || argv.help) { opt.showHelp(); @@ -27,20 +27,12 @@ switch (argv._[0]) { } console.log(prettyjson.render(data)); - }) + }); break; case 'search': case 's': argv = opt .usage("sfile " + argv._[0] + " [library]") - .alias('s', 'ssl') - .alias('h', 'html') - .alias('j', 'jade') - .alias('n', 'no-link') - .describe('s', 'Output HTTPS link') - .describe('h', 'Output HTML') - .describe('j', 'Output Jade Markup') - .describe('n', 'Do not show the link for latest version') .argv; var keyword = argv._[1]; @@ -63,18 +55,6 @@ switch (argv._[0]) { data.libs.forEach(function (lib, index) { printLn(((index > 9 ? index : " " + index) + ")").grey + " " + lib.name.replace(keyword, keyword.bold) + " [" + lib.version.green + "]"); - if (!argv.n) { - var url = sfile.url("/" + lib.name + "/" + lib.version + "/" + lib.filename, argv.ssl); - - if (argv.html) { - url = sfile.html(url); - } else if (argv.jade) { - url = sfile.jade(url); - } - - printLn((" " + url).grey); - } - printLn(""); }); }); @@ -101,57 +81,16 @@ switch (argv._[0]) { return; } keyword = keyword.trim(); - sfile.get(keyword, function (err, lib, suggest) { + sfile.info(keyword, function (err, libs) { if (err) return sfile.error(err); - if (!lib) { - printLn("未找到库: " + keyword); - // 搜索建议 - if (suggest.length > 0) { - printLn(""); - for (var i in suggest) { - suggest[i] = suggest[i].underline; - } - printLn("你是不是要找: ".cyan + suggest.slice(0, 5).join(" ")); - } - return; - } - - var version = (argv.version && argv.version !== true) ? argv.version : "" + lib.version; - var matched = false; - - // 匹配package.json给出的版本 - lib.assets.forEach(function (asset) { - if (asset.version == version) { - if (matched) return; - matched = asset; - } - }); - - // 未匹配容错 - if (!matched && lib.assets[0] && !argv.version) { - matched = lib.assets[0]; - } - - if (!matched) { - printLn("未找到 " + keyword + " [" + (argv.version + "").red + "]"); - } else { - printLn("找到 " + lib.name.bold + " [" + version.green + "]:"); - printLn(""); - - var urls = []; - matched.files.forEach(function (file) { - var path = "/" + lib.name + "/" + version + "/" + file; - var url = sfile.url(path, argv.ssl); - if (argv.html) { - url = sfile.html(url); - } else if (argv.jade) { - url = sfile.jade(url); + var version = (argv.version && argv.version !== true) ? argv.version : "" + libs.version; + printLn("库名:".green+libs.name); + printLn("当前版本:".green+version); + libs.assets[0].files.forEach(function (filename, index) { + printLn(sfile.url('/'+libs.name+'/'+version+'/'+filename).white); } - - url && printLn(url) && urls.push(url); - }); - + ) // 复制到剪贴板 if (argv.copy) { clip(urls.join(argv.html || argv.jade ? "\n" : "\\\\n"), function (err) { @@ -159,91 +98,12 @@ switch (argv._[0]) { printLn("✔ 已复制到剪贴板".green); }); - } - } + }; printLn(""); - if (argv.version) - printLn("支持的版本号: ".cyan + lib.assets.map(function (asset) { - return asset.version.underline - }).join(" ")); }); - break + break; default: - if (argv.version || argv.v) { - var json = require("../package.json"); - printLn(json.version); - } else if (argv._[0]) { - var List = require('term-list') - , list = new List({ marker: '\033[36m› \033[0m', markerLength: 2 }) - , styles = ['', 'html', 'jade'] - , current_style_index = 0 - - keyword = argv._[0].trim(); - - sfile.search(keyword, function (err, data) { - if (err) return sfile.error(err); - - if (!data.total) { - return printLn("没有结果"); - } - - printLn(("搜索 " + keyword.bold + " 共有 " + (data.total + "").bold + " 个库,当前列出 " + (data.libs.length + "").bold + " 个:")); - - data.libs.forEach(function (lib, index) { - var url = sfile.url("/" + lib.name + "/" + lib.version + "/" + lib.filename); - lib.index = index; - lib.base = lib.name.replace(keyword, keyword.bold) + " [" + lib.version.green + "] "; - lib.url = url; - lib.to_copy = url; - lib.subLabel = lib.filename; - lib.current = 'url'; - list.add(lib, lib.base + lib.subLabel.grey); - }); - - var changeStyle = function (reverse) { - if (reverse) { - current_style_index = current_style_index - 1 < 0 ? 2 : current_style_index - 1; - } else { - current_style_index = current_style_index + 1 > 2 ? 0 : current_style_index + 1; - } - var current_style = styles[current_style_index]; - - list.items.forEach(function (it, i) { - var current = list.at(i); - current.label = it.id.base + it.id.subLabel.grey + (current_style && (' [' + current_style + ']').red); - current.id.to_copy = current_style ? sfile[current_style](it.id.url) : it.id.url; - }); - list.draw(); - } - - list.start(); - - list.on("keypress", function (key, item) { - switch (key.name) { - case "return": - clip(item.to_copy, function (err) { - if (err) return sfile.error(err); - - printLn("✔ 已复制到剪贴板".green); - }); - list.stop(); - break; - case "left": - changeStyle(true); - break; - case "right": - changeStyle(false); - break; - } - }); - - list.on('empty', function () { - list.stop(); - }); - }); - } else { opt.showHelp(); - } } function printLn() { diff --git a/docs/quicksearch.png b/docs/quicksearch.png deleted file mode 100644 index 49903a7..0000000 Binary files a/docs/quicksearch.png and /dev/null differ diff --git a/lib/index.js b/lib/index.js index 8f752a9..de5c56d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,30 +1,13 @@ var request = require('request') , colors = require('colors') - , url = "http://api.staticfile.org/v1/search?count=10&q=" - , show_url = "http://api.staticfile.org/v1/packages/" + , url = "https://api.staticfile.org/v1/search?count=10&q=" + , show_url = "https://api.staticfile.org/v1/packages/"; exports.search = function (keyword, cb) { request.get({url: url + encodeURIComponent(keyword), json: true}, function (err, response, data) { return cb(err, data); }); -}; - -exports.get = function (keyword, cb) { - request.get({url: url + encodeURIComponent(keyword), json: true}, function (err, response, data) { - if (err) return cb(err); - - if (!data.libs) return cb(null, false); - - var matched_lib = false; - var suggest = []; - - data.libs.forEach(function (lib) { - if (lib.name.toLowerCase() == keyword.toLowerCase()) matched_lib = lib; - suggest.push(lib.name); - }); - - return cb(null, matched_lib, suggest); - }); + return true; }; exports.info = function (keyword, cb) { @@ -35,7 +18,7 @@ exports.info = function (keyword, cb) { return cb(null, data); }); -} +}; exports.error = function (e) { console.log("错误".redBG + " " + (e instanceof Object ? e.message : e)); @@ -63,7 +46,7 @@ exports.jade = function (file) { var ext = exports.ext(file); switch (ext) { case "js": - return 'script(type="text/javascript" src="' + file + '")' + return 'script(type="text/javascript" src="' + file + '")'; break; case 'css': return 'link(type="text/css" rel="stylesheet" href="' + file + '")'; @@ -78,8 +61,6 @@ exports.ext = function (filename) { return (i < 0) ? '' : filename.substr(i + 1); }; -exports.url = function (path, ssl) { - ssl = ssl || false; - - return (ssl ? '//dn-staticfile.qbox.me' : 'http://cdn.staticfile.org') + path; +exports.url = function (path) { + return '//cdn.staticfile.org' + path; }; diff --git a/package.json b/package.json index 4767d13..80d257b 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,11 @@ { "name": "sfile", - "version": "0.1.2", + "version": "1.0.0", "description": "The static library search and cli for staticfile.org.", "author": "hfcorriez ", + "contributors": [ + "Xuanwo (https://xuanwo.org/)" + ], "licenses": [ { "type": "MIT", @@ -13,6 +16,9 @@ "engines": { "node": "*" }, + "scripts": { + "test": "mocha --recursive" + }, "dependencies": { "optimist": "0.6.0", "colors": "0.6.2", @@ -23,5 +29,9 @@ }, "bin": { "sfile": "bin/sfile" + }, + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^3.1.2" } } diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..b6e7b1f --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,8 @@ +var sfile = require('../lib'); +var should = require('chai').should(); + +describe('index test', function () { + it('search test', function () { + sfile.search('jquery').should.eql(true); + }) +}); \ No newline at end of file diff --git a/test/sfile.test.js b/test/sfile.test.js new file mode 100644 index 0000000..e69de29