Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Usage is now a CLI command instead of a flat
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Sep 2, 2018
1 parent 267342a commit adc0407
Show file tree
Hide file tree
Showing 7 changed files with 2,039 additions and 1,982 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,21 @@ lyo

## Options

```sh
```
$ lyo --help
Usage
$ lyo [options] Run Lyo
$ lyo init [options] Add Lyo to your project
$ lyo [options] Run Lyo
$ lyo init [options] Add Lyo to your project
$ lyo usage [options] Show how to use the output file
Options
--input -i Entry file
--output -o Output file / folder
--name -n Module name in browser
--usage -u Show how to use the output file
Examples
$ lyo
$ lyo --usage
$ lyo -i main.js
$ lyo -n runMyFunction
$ lyo -o dist/bundle.min.js
Expand All @@ -54,13 +53,13 @@ the following steps

### 1. Add Lyo to your project

Run `lyo init` (with options) will edit your `package.json` as follows:
Run `lyo init` (with options) to edit your `package.json` as follows:

- Lyo will be added to the dev dependencies
- A pre-publish script will be created (or edited) so Lyo is triggered before every `npm publish`
- If you provide options, they will be saved as default options (you can change/remove them later)

```sh
```
# Example with some random options
$ lyo init -i lib/main.js -n runMyModule
```
Expand All @@ -70,14 +69,13 @@ Don't forget to run `npm install` after that

### 2. Add documentation

Use Lyo with the `--usage` option to show an example code snippet. You can edit and include it in your `README.md`.
Run `lyo usage` (with options) to show an example code snippet. You can edit and include it in your `README.md`.
Don't forget to check that the version is correct, or replace it with `latest`

```sh
$ lyo --usage
> ...
> Lyo finished successfully!
> Edit and include the following in your README.md
```
$ lyo usage
> Edit and include the following snippet in your README.md
```
![HTML example](https://i.imgur.com/xryNOT5.png)

Expand Down
14 changes: 8 additions & 6 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
const meow = require('meow');
const lyo = require('..');
const init = require('../lib/init');
const usage = require('../lib/usage');

const cli = meow(`
Usage
$ lyo [options] Run Lyo
$ lyo init [options] Add Lyo to your project
$ lyo [options] Run Lyo
$ lyo init [options] Add Lyo to your project
$ lyo usage [options] Show how to use the output file
Options
--input -i Entry file
--output -o Output file / folder
--name -n Module name in browser
--usage -u Show how to use the output file
Examples
$ lyo
$ lyo --usage
$ lyo -i main.js
$ lyo -n checkThings
$ lyo -o dist/bundle.min.js
`, {
flags: {
input: {type: 'string', alias: 'i'},
output: {type: 'string', alias: 'o'},
name: {type: 'string', alias: 'n'},
usage: {type: 'boolean', alias: 'u'}
name: {type: 'string', alias: 'n'}
},
description: 'Lyo'
});
Expand All @@ -35,6 +34,9 @@ switch (cli.input[0]) {
case 'init':
init(cli.flags);
break;
case 'usage':
usage.getUsage(cli.flags);
break;
case undefined:
lyo(cli.flags);
break;
Expand Down
28 changes: 2 additions & 26 deletions lib/display.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#! /usr/bin/env node
'use strict';

const path = require('path');
const chalk = require('chalk');

const pkg = require(path.join(process.cwd(), 'package.json'));

function succeed(opts) {
function succeed() {
console.info(chalk.green.underline('\nLyo finished successfully!'));
if (opts.usage) {
usage(opts);
}
}

function options(opts) {
Expand All @@ -20,22 +14,4 @@ function options(opts) {
`);
}

function usage(opts) {
const localPath = opts.output.replace(/\\/g, '/');
const distPath = `https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/${localPath}`;

console.info(chalk`
Edit and include the following in your {magenta README.md}
{gray <!-- local usage -->}
{yellow <script} src="{green ${localPath}}">{yellow </script>}
{gray <!-- CDN usage -->}
{yellow <script} src="{green ${distPath}}">{yellow </script>}
{yellow <script>}
{blue ${opts.name}}()
{yellow </script>}
`);
}

module.exports = {succeed, options, usage};
module.exports = {succeed, options};
64 changes: 64 additions & 0 deletions lib/usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const path = require('path');

const chalk = require('chalk');
const indentString = require('indent-string');
const display = require('./display');
const parseOptions = require('./options');

const pkg = require(path.join(process.cwd(), 'package.json'));

function getUsage(flags) {
const opts = parseOptions(flags, pkg);
display.options(opts);

const signature = getModuleSignature(opts);

printUsage(opts, signature);
}

function printUsage(opts, signature) {
const localPath = opts.output.replace(/\\/g, '/');
const distPath = `https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/${localPath}`;

console.info(chalk`
Edit and include the following snippet in your {magenta README.md}
{gray <!-- local usage -->}
{yellow <script} src="{green ${localPath}}">{yellow </script>}
{gray <!-- CDN usage -->}
{yellow <script} src="{green ${distPath}}">{yellow </script>}
` + (signature ? chalk`
{yellow <script>}
{blue ${indentString(signature, 2)}}
{yellow </script>}
` : ''));
}

function getModuleSignature(opts) {
const module = require(path.resolve(opts.input));

if (typeof module === 'function') {
return getFunctionSignature(module, opts.name);
}

if (typeof module === 'object') {
const res = [];
for (const prop in module) {
if (module.hasOwnProperty(prop) && typeof module[prop] === 'function') {
res.push(`${opts.name}.${getFunctionSignature(module[prop], prop)}`);
}
}
return res.join('\n');
}
return '';
}

function getFunctionSignature(fn, name) {
const placeHolders = ['foo', 'bar', 'baz', 'qux'];
if (fn.length > placeHolders.length) {
return `${name}(args...);`;
}
return `${name}(${placeHolders.slice(0, fn.length).join(', ')});`;
}

module.exports = {getUsage, getModuleSignature};

0 comments on commit adc0407

Please sign in to comment.