This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Usage is now a CLI command instead of a flat
- Loading branch information
Showing
7 changed files
with
2,039 additions
and
1,982 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
Oops, something went wrong.