seeq
Version:
Search or detect name on some resource (GitHub, NPM, Bower, ...)
559 lines (461 loc) • 16.8 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>seeq Source: cli.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.spacelab.css">
</head>
<body>
<div class="container-fluid">
<div class="navbar navbar-fixed-top ">
<div class="navbar-inner">
<a class="brand" href="index.html">seeq</a>
<ul class="nav">
<li class="dropdown">
<a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="module-bower.html">bower</a>
</li>
<li>
<a href="module-cdn.html">cdn</a>
</li>
<li>
<a href="module-cdnjs.html">cdnjs</a>
</li>
<li>
<a href="module-cli.html">cli</a>
</li>
<li>
<a href="module-component.html">component</a>
</li>
<li>
<a href="module-customelements.html">customelements</a>
</li>
<li>
<a href="module-format.html">format</a>
</li>
<li>
<a href="util.html">format/util</a>
</li>
<li>
<a href="module-github.html">github</a>
</li>
<li>
<a href="module-grunt.html">grunt</a>
</li>
<li>
<a href="module-gulp.html">gulp</a>
</li>
<li>
<a href="module-jam.html">jam</a>
</li>
<li>
<a href="module-jsdelivr.html">jsdelivr</a>
</li>
<li>
<a href="module-json.html">json</a>
</li>
<li>
<a href="module-jster.html">jster</a>
</li>
<li>
<a href="module-markdown.html">markdown</a>
</li>
<li>
<a href="module-microjs.html">microjs</a>
</li>
<li>
<a href="module-npm.html">npm</a>
</li>
<li>
<a href="module-raw.html">raw</a>
</li>
<li>
<a href="module-resource.html">resource</a>
</li>
<li>
<a href="util_.html">resource/util</a>
</li>
<li>
<a href="module-seeq.html">seeq</a>
</li>
<li>
<a href="module-spm.html">spm</a>
</li>
<li>
<a href="module-text.html">text</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="util-FailedHttpRequestError.html">FailedHttpRequestError</a>
</li>
<li>
<a href="util-IncorrectResponseError.html">IncorrectResponseError</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<h1 class="page-title">Source: cli.js</h1>
<section>
<article>
<pre class="sunlight-highlight-javascript linenums">/**
* Command-line interface.
*
* @module cli
*/
/*jshint boss:true, laxbreak:true*/
function resourceListToString(resourceList, short) {
var nK = resourceList.length,
result = ["\nAvailable resources (", nK, "):\n"],
sIndent = " ",
nI, resource;
for (nI = 0; nI < nK; nI++) {
resource = resourceList[nI];
if (short) {
result.push("\n",
sIndent, "* ", resource.name, " (", resource.url, ") - ", resource.description);
}
else {
result.push("\n",
sIndent, resource.name, " - ", resource.description, "\n",
sIndent, "url: ", resource.url, "\n",
sIndent, "tags: ", resource.tag.join(" "), "\n");
if (resource.note) {
result.push(sIndent, resource.note, "\n");
}
}
}
return result.join("");
}
var fs = require("fs"),
charm = require("charm")(),
nomnom = require("nomnom"),
formatUnit = require("./format"),
resourceUnit = require("./resource"),
seeq = require("./seeq"),
pkg = JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")),
resourceList = resourceUnit.getList({includeApi: true}),
generalSettingName = {
"partialMatch": null,
"caseSensitive": null,
"search": null,
"limit": null
},
argParser = nomnom()
.script(pkg.name)
.options({
"name": {
position: 0,
list: true,
help: "Name/string that should be searched for or checked for presence on a resource"
},
"help": {
abbr: "h",
help: "Show usage information and exit"
},
"listResource": {
abbr: "l",
full: "list-resource",
flag: true,
help: "Show information about all available resources"
},
"resource": {
abbr: "r",
full: "at",
help: "Filter for available resources by name: comma-separated list of names (case-insensitive) of resources that should be checked/searched"
},
"resourceTag": {
abbr: "t",
full: "tag",
help: "Filter for available resources by tag: comma-separated list of tags (case-insensitive); -tag means that resource should not have the tag"
},
"checkAllTags": {
full: "all-tag",
flag: true,
help: "Whether all specified tags should be checked for a resource"
},
"partialMatch": {
abbr: "p",
full: "partial-match",
help: "Allow partial matching when checking name: 0 - disallow (by default), 1 - allow at the beginning of matching strings, 2 - allow substring matching",
choices: [0, 1, 2]
},
"caseSensitive": {
abbr: "c",
full: "case-sensitive",
flag: true,
help: "Use case-sensitive check/search when possible"
},
"search": {
abbr: "s",
flag: true,
help: "Make search instead of check"
},
"limit": {
abbr: "m",
help: "Limit of quantity of results per resource",
callback: function(value) {
if (value < 1) {
return "Limit should be equal to or greater than 1";
}
}
},
"format": {
abbr: "f",
"default": formatUnit.exists("text") ? "text" : "raw",
help: "Result format; can be: " + formatUnit.getNameList().join(", ")
},
"verbose": {
abbr: "V",
flag: true,
help: "Enable verbose output"
},
"version": {
abbr: "v",
flag: true,
help: "Show program version"
}
})
.help(resourceListToString(resourceList, true));
(function() {
var nI, nL, settings, sName, value;
// Add options of resources
for (nI = 0, nL = resourceList.length; nI < nL; nI++) {
value = resourceList[nI];
if (settings = value.api.settings) {
sName = value.id;
for (value in settings) {
argParser.option(sName + "-" + value, settings[value]);
}
}
}
}());
function showProgress(data) {
var sOut = [data.number, "/", data.total, " (", Math.floor(data.number * 100 / data.total), "%)"].join("");
charm.left(showProgress.shift);
charm.write(sOut);
showProgress.shift = sOut.length;
if (data.number === data.total) {
charm.write("\n");
}
}
showProgress.shift = 3;
function showResult(resultMap, nameList, args) {
var sFormat = args.format,
result = sFormat ? formatUnit.exists(sFormat) : false;
if (result) {
result = formatUnit.format(resultMap,
sFormat,
{
queryList: nameList,
verbose: args.verbose
});
}
else {
result = JSON.stringify(resultMap, null, 4);
}
console.log("\nResults:\n\n" + result);
}
/**
* Perform operation using specified options.
*
* @param {Object} [args]
* Operation options.
* @alias module:cli.run
*/
function run(args) {
var bShowUsage = true,
list,
nameList,
nL,
resourceNameList,
resourceSettings,
sName,
value;
if (! args) {
args = {};
}
if (args.version) {
console.log("Version: " + pkg.version);
bShowUsage = false;
}
if (args.listResource) {
console.log(resourceListToString(resourceList));
bShowUsage = false;
}
if (args.name && args.name.length) {
// List of names to check
nameList = args.name;
// Determine resources to check
if (args.resource) {
args.resource = args.resource.split(",");
}
if (args.resourceTag) {
args.resourceTag = args.resourceTag.split(",");
}
resourceNameList = resourceUnit.getNameList({
selectName: args.resource,
selectTag: args.resourceTag,
checkAllTags: args.checkAllTags
});
// Make check if resources are specified
if (resourceNameList.length) {
// Prepare resources settings
resourceSettings = {
};
for (sName in args) {
value = args[sName];
// General settings
if (sName in generalSettingName) {
(resourceSettings._general || (resourceSettings._general = {}))[sName] = value;
}
// Resource-specific settings
else if (list = /^(\w+)\-(.+)$/.exec(sName)) {
if (sName = resourceUnit.getIdByName(list[1])) {
( resourceSettings[sName] || (resourceSettings[sName] = {}) )[ list[2] ] = value;
}
}
}
// Start checking
console.log("Checking " + resourceNameList.join(", ") + "...");
if (nameList.length > 1 || resourceNameList.length > 1) {
value = showProgress;
charm.pipe(process.stdout);
charm.write("Progress: 0 %");
}
else {
value = null;
}
seeq.search(nameList,
function(resultMap) {
showResult(resultMap, nameList, args);
},
{
resource: resourceNameList,
settings: resourceSettings,
progressCallback: value
});
}
// If no resource is found
else {
console.log("No resource is found.");
if (args.resource) {
list = args.resource.filter(function(name) {
return ! resourceUnit.isAvailable(name);
});
if (nL = list.length) {
console.log(nL > 1
? "The following resources are unknown: " + list.join(", ")
: list[0] + " is unknown resource");
}
}
console.log("Run `" + pkg.name + " -l` to see information about all available resources.");
}
}
else if (bShowUsage) {
argParser.print(argParser.getUsage());
}
}
/**
* Perform operation specified via command-line arguments.
*
* @param {Object} [options]
* Predefined values for command-line options.
* Each option specified in this object will be used only when the corresponding option is not set in command-line arguments.
* @alias module:cli.cli
* @see {@link module:cli.run run}
*/
function cli(options) {
var args = argParser.parse(),
optionMap,
sName,
value;
// Use predefined values for absent arguments
if (options) {
optionMap = argParser.specs;
for (sName in optionMap) {
if (! (sName in args)) {
if (sName in options) {
args[sName] = options[sName];
}
else {
value = optionMap[sName];
if (value.full in options) {
args[sName] = options[value.full];
}
else if (value.abbr in options) {
args[sName] = options[value.abbr];
}
}
}
}
}
// Run CLI
run(args);
}
// Exports
exports.cli = cli;
exports.run = run;
// If script is run directly from Node
if (require.main === module) {
cli();
}
</pre>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="copyright">
Copyright (c) 2014 Denis Sikuler
</span>
<br />
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Thu Aug 21 2014 23:46:51 GMT+0400 (MSK) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
<br clear="both">
</div>
</div>
<script src="scripts/sunlight.js"></script>
<script src="scripts/sunlight.javascript.js"></script>
<script src="scripts/sunlight-plugin.doclinks.js"></script>
<script src="scripts/sunlight-plugin.linenumbers.js"></script>
<script src="scripts/sunlight-plugin.menu.js"></script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/jquery.scrollTo.js"></script>
<script src="scripts/jquery.localScroll.js"></script>
<script src="scripts/bootstrap-dropdown.js"></script>
<script src="scripts/toc.js"></script>
<script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
<script>
$( function () {
$( "#toc" ).toc( {
selectors : "h1,h2,h3,h4",
showAndHide : false,
scrollTo : 60
} );
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
} );
</script>
</body>
</html>