showandtell
Version:
A Javascript library providing debugger-like command-line interactivity for program state inspection and modification
35 lines (29 loc) • 863 B
JavaScript
/**
* This example shows how to create a new command and register it
* to a story. When run, you will be able to do things like
*
* help square
* square test
*/
const showandtell = require('showandtell')
const square = new showandtell.Command({
name: 'SQUARE',
help: 'SQUARE <variable>',
args: [{name: 'variable', help: 'The name of a top-level value to compute the square of'}],
func: function (state, args, next) {
if (!state.hasOwnProperty(args.variable)) {
next(new Error(`State does not have top-level key ${args.variable}`), state)
} else {
state[args.variable] *= state[args.variable]
next(null, state)
}
}
})
const story = new showandtell.Story([square])
let state = {test: 32}
story.start(state)
.then((newState) => {
console.log('new test value: ', state.test)
})
.catch(console.error)