Skip to content

Commit

Permalink
Added metadoc capability to introspect the shell/commands. (data attr…
Browse files Browse the repository at this point in the history
…ibute on each class)
  • Loading branch information
coreybutler committed Apr 4, 2020
1 parent c5e09fc commit b690be3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 12 deletions.
18 changes: 17 additions & 1 deletion examples/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import fs from 'fs'
import path from 'path'
import { Command, Shell } from '../../src/index.js'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))

const pkg = JSON.parse(fs.readFileSync('./package.json'))
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, './package.json')))

const shell = new Shell({
name: Object.keys(pkg.bin)[0],
Expand Down Expand Up @@ -109,6 +111,20 @@ shell.add(new Command({
}
}))


shell.add(new Command({
name: 'doc',
description: 'Output the metadoc of this shell.',
handler () {
console.log(shell.data)
}
}))

shell.use((data, next) => {
console.log('This middleware runs on very command.')
next()
})

const cmd = process.argv.slice(2).join(' ').trim()
// console.log(cmd)
shell.exec(cmd).catch(e => console.log(e.message || e))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@author.io/shell",
"version": "1.1.9",
"version": "1.2.0",
"description": "A micro-framework for creating CLI-like experiences. This supports Node.js and browsers.",
"main": "src/index.js",
"scripts": {
Expand Down
25 changes: 25 additions & 0 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@ export default class Command {
}
}

get data () {
const commands = {}

Array.from(this.#processors.values()).forEach(cmd => {
let data = cmd.data
const name = data.name
delete data.name
commands[name] = data
})

const data = {
name: this.#name,
description: this.description,
help: this.help,
usage: this.usage,
aliases: this.#aliases || [],
flags: this.#flagConfig || {},
handler: (this.#fn || this.#defaultHandler).toString(),
commands,
middleware: this.#middleware.data
}

return data
}

set tableWidth(value) {
this.#tableWidth = value
this.#processors.forEach(cmd => cmd.tableWidth = value)
Expand Down
8 changes: 6 additions & 2 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ const last = a => a[a.length - 1]
const reduce = a => a.slice(0, -1)

export default class Middleware {
constructor () { this.size = 0 }
constructor () { Object.defineProperty(this, '_data', { enumerable: false, value: [] }) }

get size () { return this._data.length }

get data () { return this._data }

use (method) {
this.size++
this._data.push(method.toString())
this.run = ((stack) => (...args) => stack(...reduce(args), () => {
const next = last(args)
method.apply(this, [...reduce(args), next.bind.apply(next, [null, ...reduce(args)])])
Expand Down
25 changes: 25 additions & 0 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ export default class Shell {
}
}

get data () {
const commands = {}

Array.from(this.#processors.values()).forEach(cmd => {
let data = cmd.data
const name = data.name
delete data.name
commands[name] = data
})

return {
name: this.name,
description: this.description,
version: this.version,
commands,
middleware: this.#middleware.data,
help: this.help,
usage: this.usage,
defaultHandler: this.#defaultHandler.toString(),
authohelp: this.#autohelp,
runtime: this.#runtime,
maxHistoryItems: this.#maxHistoryItems
}
}

get version () {
return this.#version || 'Unknown'
}
Expand Down
13 changes: 5 additions & 8 deletions test/unit/01-sanity/01-sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('Sanity Check - Command', t => {
cb && cb()
}
})

console.log('>>', mirror.xdata)
t.ok(mirror instanceof Command, 'Command initialized successfully.')

const CLI = new Shell({
Expand All @@ -40,11 +40,8 @@ test('Sanity Check - Command', t => {

t.ok(CLI instanceof Shell, 'Shell initialized with commands successfully.')

let defaultHandlerFires = false

CLI.exec('test', data => defaultHandlerFires = true)

t.ok(defaultHandlerFires, 'Default handler fires.')

t.end()
CLI.exec('test').then(data => {
t.pass('Default handler fires.')
t.end()
}).catch(e => t.fail(e.message))
})

0 comments on commit b690be3

Please sign in to comment.