Skip to content

Develop #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "6"
- "4"
- "0.12"
install:
- npm install
script:
- npm test
22 changes: 2 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[staticfile.org](http://staticfile.org)命令行工具

![](https://api.travis-ci.org/staticfile/cli.svg)

# 安装

```
Expand All @@ -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
```

## 获取链接
Expand All @@ -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
```

## 查看版本
Expand Down
164 changes: 12 additions & 152 deletions bin/sfile
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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];
Expand All @@ -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("");
});
});
Expand All @@ -101,149 +81,29 @@ 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) {
if (err) return sfile.error(err);

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() {
Expand Down
Binary file removed docs/quicksearch.png
Binary file not shown.
33 changes: 7 additions & 26 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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));
Expand Down Expand Up @@ -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 + '")';
Expand All @@ -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;
};
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <hfcorriez@gmail.com>",
"contributors": [
"Xuanwo <xuanwo.cn@gmail.com> (https://xuanwo.org/)"
],
"licenses": [
{
"type": "MIT",
Expand All @@ -13,6 +16,9 @@
"engines": {
"node": "*"
},
"scripts": {
"test": "mocha --recursive"
},
"dependencies": {
"optimist": "0.6.0",
"colors": "0.6.2",
Expand All @@ -23,5 +29,9 @@
},
"bin": {
"sfile": "bin/sfile"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.1.2"
}
}
8 changes: 8 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -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);
})
});
Empty file added test/sfile.test.js
Empty file.