Skip to content

Commit

Permalink
Updated build process. Fixed formatting (row adjustments on line wrap…
Browse files Browse the repository at this point in the history
…). Version bump.
  • Loading branch information
coreybutler committed Apr 22, 2020
1 parent 1e21efa commit c529f3b
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 95 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,50 @@ One development goal of this framework is to remain as lightweight and unopinion
1. [@author.io/shell-middleware](https://github.com/author/shell-middleware)
### Customized Help/Usage Messages
This library has only one dependency, [@author.io/table](https://github.com/author/table). It is used to format the usage and help messages within a shell app. The `Table` library can be used to create your own custom screens, though most users will likely want to stick with the defaults.
_Example:_
```javascript
import { Shell, Command, Table } from '@author.io/node-shell'

const shell = new Shell(...)
shell.usage = '...'
shell.help = () => {
const rows = [
['Command', 'Alias Names'],
['...', '...']
]

const table = new Table(rows)

console.log(shell.usage + '\n' + table.output)
}
```
**The `usage` and/or `help` attributes of an individual `Command` can also be set:**
```javascript
import { Shell, Command, Table } from '@author.io/node-shell'

const cmd = new Command(...)
cmd.usage = '...'
cmd.help = () => {
const rows = [
['Flags', 'Alias Names'],
['...', '...']
]

const table = new Table(rows)

console.log(cmd.usage + '\n' + table.output)
}
```
There is also a `Formatter` class that helps combine usage/help messages internally. This class is exposed for those who want to dig into the inner workings, but it should be considered as more of an example than a supported feature. Since it is an internal class, it may change without warning (though we'll try to keep the methods consistent across releases).
### Introspection/Metadata Generation
A JSON metadoc can be produced from the shell:
Expand Down
22 changes: 22 additions & 0 deletions build/lib/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import chalk from 'chalk' // Included in Rollup
import stripCode from 'rollup-plugin-strip-code'
import replace from '@rollup/plugin-replace'

Expand Down Expand Up @@ -154,4 +155,25 @@ export default class build {

return false
}

ignoreCircularDependency () {
const refs = new Set([...arguments])

return warning => {
if (
warning.code === 'CIRCULAR_DEPENDENCY' &&
refs.size > 0 &&
Array.from(refs).filter(p => {
try {
return !warning.importer.indexOf(path.normalize(p))
} catch (e) { return false }
}).length > 0
) {
return
}

console.log(chalk.yellow.bold(`(!) ${warning.code}`))
console.log(chalk.grey(warning.message))
}
}
}
3 changes: 3 additions & 0 deletions build/rollup.browser.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const globalplugins = [
]

// 2. Build Browser Production Package: Standard (Minified/Munged)
const onwarn = build.ignoreCircularDependency('../src/command.js', '../src/shell.js', '../src/format.js')
outdir += `/browser-${build.name}`
build.supportedBrowsers().forEach(edition => {
console.log(`Generating ${edition} browser code.`)
Expand All @@ -56,6 +57,7 @@ build.supportedBrowsers().forEach(edition => {
configuration.push({
input,
plugins,
onwarn,
output: {
banner: config.banner,
file: `${outdir}/${build.name}-${build.version}${edition !== 'current' ? '-' + edition : ''}.min.js`,
Expand All @@ -73,6 +75,7 @@ build.supportedBrowsers().forEach(edition => {
configuration.push({
input,
plugins,
onwarn,
output: {
banner: config.banner,
file: `${outdir}/${build.name}-${build.version}-global.min.js`,
Expand Down
8 changes: 6 additions & 2 deletions build/rollup.node.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ install()
const build = new Build()

// Identify source file
const input = path.resolve(`../${build.pkg.main||'../src/index.js'}`)
const input = path.resolve(`../${build.pkg.main || '../src/index.js'}`)

// Configure metadata for the build process.
const rootdir = config.nodeOutput // Main output directory
Expand All @@ -26,7 +26,8 @@ fs.rmdirSync(rootdir, { recursive: true })
let terserCfg = config.terser
terserCfg.module = true
// terserCfg.mangle = { properties: true }
console.log(terserCfg)
// console.log(terserCfg)

// Identify plugins
const plugins = [
build.only('node'),
Expand All @@ -42,10 +43,12 @@ const plugins = [
]

// 2. Build Node Production Package: Standard (Minified/Munged)
const onwarn = build.ignoreCircularDependency('../src/command.js', '../src/shell.js', '../src/format.js')
outdir += `/node-${build.name}`
configuration.push({
input,
plugins,
onwarn,
output: {
banner: config.banner,
file: `${outdir}/${build.name}-${build.version}.min.js`,
Expand All @@ -59,6 +62,7 @@ configuration.push({
configuration.push({
input,
plugins,
onwarn,
output: {
banner: config.banner,
file: `${outdir}-legacy/${build.name}-${build.version}.min.js`,
Expand Down
3 changes: 3 additions & 0 deletions build/rollup.test.browser.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const globalplugins = [
]

// 2. Build Browser Production Package: Standard (Minified/Munged)
const onwarn = build.ignoreCircularDependency('../src/command.js', '../src/shell.js', '../src/format.js')
build.supportedBrowsers().forEach(edition => {
console.log(`Generating ${edition} browser code.`)
const plugins = globalplugins.slice()
Expand All @@ -57,6 +58,7 @@ build.supportedBrowsers().forEach(edition => {
configuration.push({
input,
plugins,
onwarn,
output: {
exports: 'named',
banner: config.banner,
Expand All @@ -75,6 +77,7 @@ build.supportedBrowsers().forEach(edition => {
configuration.push({
input,
plugins,
onwarn,
output: {
exports: 'named',
banner: config.banner,
Expand Down
2 changes: 2 additions & 0 deletions build/rollup.test.node.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ const plugins = [
]

// 2. Build Node Production Package: Standard (Minified/Munged)
const onwarn = build.ignoreCircularDependency('../src/command.js', '../src/shell.js', '../src/format.js')
configuration.push({
input,
plugins,
onwarn,
output: {
exports: 'named',
banner: config.banner,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@author.io/shell",
"version": "1.3.1",
"version": "1.3.2",
"description": "A micro-framework for creating CLI-like experiences. This supports Node.js and browsers.",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -51,6 +51,6 @@
},
"dependencies": {
"@author.io/arg": "^1.2.5",
"@author.io/table": "^1.0.0-beta.4"
"@author.io/table": "^1.0.0"
}
}
7 changes: 4 additions & 3 deletions src/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class Formatter {
const desc = this.#data.description.trim()

if (desc.trim().length > 0 && out !== desc) {
out.push(`\n ${desc.trim()}` + '\n')
out.push(new Table([[desc.trim().replace(/\n/gi, '\n ')]], null, null, this.#tableWidth, [2, 0, 1, 1]).output)
}

return out.join('\n')
} else if (this.#data instanceof Shell) {
return `${this.#data.name}${this.#data.__commandMap.size > 0 ? ' [COMMAND]' : ''}\n\n ${this.#data.description || ''} Version ${this.#data.version}.\n`.trim()
return `${this.#data.name}${this.#data.description.trim().length > 0 ? new Table([[desc.trim().replace(/\n/gi, '\n ')]], null, null, this.#tableWidth, [2, 0, 1, 1]).output : ''}${this.#data.__commandMap.size > 0 ? ' [COMMAND]' : ''}\n\n ${this.#data.description || ''} Version ${this.#data.version}.\n`.trim()
}

return ''
Expand All @@ -48,6 +48,7 @@ class Formatter {
if (this.#data instanceof Command) {
const flags = this.#data.__flagConfig
const rows = []

if (flags.size > 0) {
flags.forEach((cfg, flag) => {
let aliases = Array.from(cfg.aliases||[])
Expand Down Expand Up @@ -79,4 +80,4 @@ class Formatter {
}
}

export { Formatter as default, Table }
export { Formatter as default, Formatter, Table }
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Command from './command.js'
import Shell from './shell.js'
import Formatter from './format.js'
const all = { Shell, Command, Formatter }
export { Command, Shell, Formatter, all as default }
import { Formatter, Table } from './format.js'
const all = { Shell, Command, Formatter, Table }
export { Command, Shell, Formatter, Table, all as default }
85 changes: 0 additions & 85 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,91 +193,6 @@ export default class Shell {
this.#updateHelp()

return this.#formattedDefaultHelp.help

// let mainmsg = [this.usage + '\n']

// const help = new Map()

// let nameWidth = 0
// let aliasWidth = 0
// let tabWidth = this.#tabWidth
// let maxWidth = this.#tableWidth

// this.#processors.forEach(proc => {
// nameWidth = proc.name.length > nameWidth ? proc.name.length : nameWidth
// aliasWidth = proc.aliases.join(', ').trim().length + proc.aliases.length > aliasWidth ? proc.aliases.join(', ').trim().length + proc.aliases.length : aliasWidth

// let summary = proc.description

// let size = proc.subcommands.size
// if (size > 0) {
// summary += ` Has ${ size === 1 ? 'an' : size } additional subcommand${ size !== 1 ? 's' : '' }.`
// }

// help.set(proc.name, {
// description: summary,
// aliases: proc.aliases
// })
// })

// help.forEach((data, name) => {
// let msg = name

// // Command name
// while (msg.length < nameWidth) {
// msg += ' '
// }

// // Aliases
// let aliases = data.aliases.map(item => `${item}`).join(', ')
// while (aliases.length < aliasWidth) {
// aliases += ' '
// }

// msg += (aliases.trim().length > 0 ? ' [' + aliases.replace(/^(.*[^\s])/i, '$1]$`') : aliases) + '\t'

// // Desc
// let desc = []
// let tabs = msg.match(/\t/gi).length
// let descWidth = maxWidth - (nameWidth + aliasWidth + 10)

// if (data.description && data.description.length > descWidth) {
// let dsc = new String(data.description)
// let match = new RegExp(`(.{0,${descWidth}}[\\s\n])`, 'g')

// desc = data.description.match(match)
// desc.push(dsc.replace(desc.join(''), ''))

// while (desc.length > 1 && desc[desc.length - 1].length + desc[desc.length - 2].length < descWidth) {
// desc[desc.length - 2] += desc.pop()
// }

// desc = desc.reverse().map(item => item.trim())
// } else {
// desc.push(data.description)
// }

// if (desc.length > 0) {
// let prefix = ''
// for (let i = 0; i < (nameWidth + aliasWidth + 10 + (tabs * tabWidth)); i++) {
// prefix += ' '
// }

// msg += ' : ' + desc.pop()
// while (desc.length > 0) {
// msg += `\n${prefix}${desc.pop()}`
// }
// }

// mainmsg.push(' - ' + msg)
// })

// let tab = ''
// for (let i = 0; i < tabWidth; i++) {
// tab += ' '
// }

// return mainmsg.join('\n') + '\n'.replace(/\n{2,}$/, '\n').replace(/\t/g, tab)
}

set help (value) {
Expand Down

0 comments on commit c529f3b

Please sign in to comment.