This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+2
View File
@@ -0,0 +1,2 @@
node_modules/
.DS_Store
+57
View File
@@ -0,0 +1,57 @@
# cli-usage
Easily show the usage of your CLI tool from a Markdown string
or file. You can just plug `cli-usage` in without thinking
about paramters, or you can handle that your self using the
`.get` API end-point.
## Install
```
npm install cli-usage
```
## Usage
Most basic usage, just plug in:
```javascript
var usage = require('cli-usage');
usage();
//=> If help-flag is passed, print usage
//=> and exit with code 0.
```
This will listen for `-h`, `-help` or `--help` passed
into your CLI and try to locate a `usage.md` file from
the directory of the file. If `help` is passed and the
`usage.md` file found, the usage will be printed and
the application will exit with code `0`.
You can also pass in a filename or a string.
```javascript
var usage = require('cli-usage');
usage('./some/path/to/usage.md');
```
or
```javascript
var usage = require('cli-usage');
usage('# Simple usage');
```
### Get compiled usage
Instead of `cli-usage` doing all the work, you can
also just retrieve the compiled usage text and handle
it your self.
#### Example
```javascript
var usage = require('cli-usage');
console.log(usage.get('# some custom markdown from string'));
console.log(usage.get('./usage.md'));
```
+5
View File
@@ -0,0 +1,5 @@
var usage = require('../');
console.log(usage.get('# some custom markdown from string'));
console.log(usage.get('./usage.md'));
+3
View File
@@ -0,0 +1,3 @@
var usage = require('../');
usage();
+6
View File
@@ -0,0 +1,6 @@
# CLI Usage
Some sort of description here..
```
$ how --to="use" | grep it
```
+59
View File
@@ -0,0 +1,59 @@
var TerminalRenderer = require('marked-terminal');
var marked = require('marked');
var path = require('path');
var fs = require('fs');
var argv = process.argv;
var DEFAULT_FILENAME = 'usage.md';
var possibleFile = path.join(path.dirname(argv[1]), DEFAULT_FILENAME);
marked.setOptions({
// Define custom renderer
renderer: new TerminalRenderer()
});
module.exports = function (str) {
if (!isHelp()) {
return void 0;
}
console.log(get(str));
process.exit(0);
};
module.exports.get = get;
function get (str) {
if (str && path.extname(str) === '.md') {
return fromFile(path.resolve(path.dirname(argv[1]), str));
}
if (str) {
return fromString(str);
}
if (fs.existsSync(possibleFile)) {
return fromFile(possibleFile);
}
throw Error('Could not locate usage source. Need pass inn file or text, or have usage.md in same dir as CLI');
}
function fromFile (filename) {
return marked(fs.readFileSync(filename).toString());
}
function fromString (text) {
return marked(text);
}
function isHelp () {
var without = argv.slice(2);
return without.some(function (option) {
return check(option, 'h') || check(option, 'help');
});
}
function check (option, needle) {
return option.indexOf('-' + needle) !== -1 || option.indexOf('--' + needle) !== -1;
}
+87
View File
@@ -0,0 +1,87 @@
{
"_args": [
[
{
"raw": "cli-usage@^0.1.1",
"scope": null,
"escapedName": "cli-usage",
"name": "cli-usage",
"rawSpec": "^0.1.1",
"spec": ">=0.1.1 <0.2.0",
"type": "range"
},
"c:\\xampp\\htdocs\\laravel\\node_modules\\node-notifier"
]
],
"_from": "cli-usage@>=0.1.1 <0.2.0",
"_id": "cli-usage@0.1.4",
"_inCache": true,
"_location": "/cli-usage",
"_nodeVersion": "6.3.1",
"_npmOperationalInternal": {
"host": "packages-16-east.internal.npmjs.com",
"tmp": "tmp/cli-usage-0.1.4.tgz_1473324686930_0.2721161802764982"
},
"_npmUser": {
"name": "mikaelb",
"email": "mikaelbre@gmail.com"
},
"_npmVersion": "3.10.3",
"_phantomChildren": {},
"_requested": {
"raw": "cli-usage@^0.1.1",
"scope": null,
"escapedName": "cli-usage",
"name": "cli-usage",
"rawSpec": "^0.1.1",
"spec": ">=0.1.1 <0.2.0",
"type": "range"
},
"_requiredBy": [
"/node-notifier"
],
"_resolved": "https://registry.npmjs.org/cli-usage/-/cli-usage-0.1.4.tgz",
"_shasum": "7c01e0dc706c234b39c933838c8e20b2175776e2",
"_shrinkwrap": null,
"_spec": "cli-usage@^0.1.1",
"_where": "c:\\xampp\\htdocs\\laravel\\node_modules\\node-notifier",
"author": {
"name": "Mikael Brevik"
},
"dependencies": {
"marked": "^0.3.6",
"marked-terminal": "^1.6.2"
},
"description": "Easily show CLI usage from a markdown source file",
"devDependencies": {
"mocha": "^3.0.2"
},
"directories": {},
"dist": {
"shasum": "7c01e0dc706c234b39c933838c8e20b2175776e2",
"tarball": "https://registry.npmjs.org/cli-usage/-/cli-usage-0.1.4.tgz"
},
"gitHead": "998afc5fe38325f26c043a93d554ec5cc5ef8a0b",
"keywords": [
"CLI-Usage",
"Markdown",
"Usage",
"CLI",
"Print-Usage"
],
"license": "MIT",
"main": "index.js",
"maintainers": [
{
"name": "mikaelb",
"email": "mikaelbre@gmail.com"
}
],
"name": "cli-usage",
"optionalDependencies": {},
"readme": "ERROR: No README data found!",
"scripts": {
"test": "mocha tests.js -R spec"
},
"version": "0.1.4"
}
+24
View File
@@ -0,0 +1,24 @@
var assert = require('assert');
var path = require('path');
var usage = require('./');
var fs = require('fs');
var origFs = fs.readFileSync;
var origMarked = fs.origMarked;
describe('cli-usage', function () {
afterEach(function() {
fs.readFileSync = origFs;
});
describe('get', function () {
it('should get compiled markdown from file input', function () {
var expected = 'expected';
fs.readFileSync = function (filename) {
assert.equal(path.basename(filename), 'file.md');
return expected;
};
assert.ok(usage.get('file.md').indexOf(expected) !== -1);
});
});
});