Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Feb 10, 2019
1 parent d1c3774 commit f618296
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 80 deletions.
6 changes: 3 additions & 3 deletions bin/seneca-doc-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inspect_local_plugin()

async function inspect_local_plugin() {
var plugin = await Inspect(LocalFolder, LocalPackage)

var inj = {
'action-list': {
text: Render.action_list(plugin.actions)
Expand All @@ -25,6 +25,6 @@ async function inspect_local_plugin() {
text: Render.action_desc(plugin.actions)
}
}
Inject.update_file(Path.resolve(LocalFolder,'README.md'), inj)

Inject.update_file(Path.resolve(LocalFolder, 'README.md'), inj)
}
16 changes: 10 additions & 6 deletions lib/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ module.exports = {
Fs.writeFileSync(path, source)
},
update_source: function(source, injections) {
Object.keys(injections).forEach((name)=>{
Object.keys(injections).forEach(name => {
var inj = injections[name]
var re = new RegExp('<!--START:'+name+'-->.*?<!--END:'+name+'-->','s')
source = source.replace(re,'<!--START:'+name+'-->\n'+inj.text+'\n<!--END:'+name+'-->')
var re = new RegExp(
'<!--START:' + name + '-->.*?<!--END:' + name + '-->',
's'
)
source = source.replace(
re,
'<!--START:' + name + '-->\n' + inj.text + '\n<!--END:' + name + '-->'
)
})
return source
}
}


const intern = (module.exports.intern = {
})
const intern = (module.exports.intern = {})
70 changes: 35 additions & 35 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,90 @@
var Util = require('util')
var Jsonic = require('seneca').util.Jsonic


module.exports = {
action_list: function (list) {
action_list: function(list) {
const b = ['\n\n## Action Patterns\n']

list.forEach((x)=>{
b.push('* ['+x.pattern+'](#'+intern.patlink(x.pattern)+')')
list.forEach(x => {
b.push('* [' + x.pattern + '](#' + intern.patlink(x.pattern) + ')')
})

return b.join('\n')+'\n\n'
return b.join('\n') + '\n\n'
},

action_desc: function (list) {
action_desc: function(list) {
const b = ['\n\n## Action Descriptions\n']

list.forEach((x)=>{
b.push('### &laquo; `'+x.pattern+'` '+x.sync+'&raquo;\n')
b.push(x.desc+'\n\n')
list.forEach(x => {
b.push('### &laquo; `' + x.pattern + '` ' + x.sync + '&raquo;\n')
b.push(x.desc + '\n\n')

var example_keys = Object.keys(x.examples)
if(0 < example_keys.length) {
if (0 < example_keys.length) {
b.push('\n\n#### Examples\n\n')
example_keys.forEach((patpart)=>{
b.push('\n* `'+x.pattern+','+patpart+'`\n * '+x.examples[patpart])
example_keys.forEach(patpart => {
b.push(
'\n* `' +
x.pattern +
',' +
patpart +
'`\n * ' +
x.examples[patpart]
)
})
}

b.push(intern.action_params(x.rules))

if(x.reply_desc) {
if (x.reply_desc) {
b.push('\n\n#### Replies With\n\n')
b.push(
'```\n'+
(Util.inspect(x.reply_desc,{depth:null,compact:false}))
+'\n```\n\n')
'```\n' +
Util.inspect(x.reply_desc, { depth: null, compact: false }) +
'\n```\n\n'
)
}

b.push('----------')
})

return b.join('\n')+'\n\n'

return b.join('\n') + '\n\n'
}

}


const intern = (module.exports.intern = {
action_params: function(rules) {
const self = this
const params = Object.keys(rules)

if( 0 < params.length ) {
if (0 < params.length) {
const b = ['#### Parameters\n\n']
params.forEach(function(param) {
var rule = rules[param]

var param_md = '* _'+param+'_: '
if(rule.isJoi) {
var param_md = '* _' + param + '_: '
if (rule.isJoi) {
var joidesc = rule.describe()
b.push(param_md +
joidesc.type +
self.joiflags(joidesc.flags)
)
b.push(' * '+joidesc.description)
}
else {
b.push(param_md + joidesc.type + self.joiflags(joidesc.flags))
b.push(' * ' + joidesc.description)
} else {
b.push(param_md + Util.inspect(rule))
}
})
return b.join('\n')+'\n\n'
return b.join('\n') + '\n\n'
}

return ''
},

patlink: function(pat) {
return '-'+pat.replace(/[^\w]/g,'')+'-'
return '-' + pat.replace(/[^\w]/g, '') + '-'
},

joiflags: function(flags) {
var names = Object.keys(flags)
if(0 < names.length) {
return ' <i><small>'+Jsonic.stringify(flags)+'</small></i>'
if (0 < names.length) {
return ' <i><small>' + Jsonic.stringify(flags) + '</small></i>'
}
return ''
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "seneca-doc",
"version": "0.0.6",
"version": "0.1.0",
"description": "Documentation helper for Seneca plugins.",
"main": "seneca-doc.js",
"bin": {
"seneca-doc": "./bin/seneca-doc-exec.js"
},
"scripts": {
"test": "lab -P test -t 50",
"test": "lab -P test -t 80",
"prettier": "prettier --write *.js lib/*.js bin/*.js test/*.js",
"coveralls": "lab -s -P test -r lcov | coveralls",
"clean-npm": "rm -rf node_modules package-lock.json",
Expand Down
34 changes: 18 additions & 16 deletions seneca-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ module.exports.errors = {
pin_missing: 'Pin missing from message: <%=msg%>'
}


module.exports.preload = function() {
const seneca = this

seneca.private$.action_modifiers.push(function(actdef) {
actdef.desc = (actdef.func && actdef.func.desc) || 'No description provided.'
actdef.desc =
(actdef.func && actdef.func.desc) || 'No description provided.'
actdef.examples = (actdef.func && actdef.func.examples) || {}
actdef.reply_desc = (actdef.func && actdef.func.reply_desc) || null
})
Expand All @@ -21,27 +21,28 @@ module.exports.preload = function() {
function seneca_doc(options) {
const seneca = this
const Joi = seneca.util.Joi

seneca
.add('role:doc,describe:plugin', describe_plugin)
.add('role:doc,describe:pin', describe_pin)


Object.assign(describe_plugin, {
desc: 'Provide introspection data for a plugin and its actions.',
examples: {
'plugin:entity': 'Describe the seneca-entity plugin.',
'plugin:entity$foo':
'Describe the seneca-entity plugin instance with tag _foo_.'
'Describe the seneca-entity plugin instance with tag _foo_.'
},
validate: {
plugin: Joi.string().required().description(
'The full name of the plugin (if tagged, use the form name$tag).')

plugin: Joi.string()
.required()
.description(
'The full name of the plugin (if tagged, use the form name$tag).'
)
},
reply_desc: {
plugin: 'plugin parameter',
actions: [ '{ Seneca action definition }' ]
actions: ['{ Seneca action definition }']
}
})

Expand Down Expand Up @@ -70,20 +71,21 @@ function seneca_doc(options) {
})
}


Object.assign(describe_pin, {
desc: 'Provide introspection data for actions matching a _pin_ (a sub pattern).',
desc:
'Provide introspection data for actions matching a _pin_ (a sub pattern).',
examples: {
'pin:"a:1,b:2"': 'Describe actions matching at least `a:1,b:2`.',
'pin:"a:1,b:2"': 'Describe actions matching at least `a:1,b:2`.'
},
validate: {
pin: Joi.alternatives().try(Joi.string(),Joi.object()).required().description(
'The pin sub pattern in string or object format.')

pin: Joi.alternatives()
.try(Joi.string(), Joi.object())
.required()
.description('The pin sub pattern in string or object format.')
},
reply_desc: {
pin: 'pin parameter',
actions: [ '{ Seneca action definition }' ]
actions: ['{ Seneca action definition }']
}
})

Expand Down
28 changes: 12 additions & 16 deletions test/seneca-doc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const Plugin = require('..')
const Render = require('../lib/render')
const Inject = require('../lib/inject')


lab.test(
'validate',
Util.promisify(function(x, fin) {
Expand All @@ -36,11 +35,10 @@ lab.test('describe-plugin', async () => {

lab.test('describe-pin', async () => {
var si = await seneca_instance().ready()
var out = await si.post('role:doc,describe:pin', { pin:'role:seneca' })
var out = await si.post('role:doc,describe:pin', { pin: 'role:seneca' })
expect(out.actions.length).above(0)
})


lab.test('action_list', async () => {
var si = await seneca_instance().ready()
var out = await si.post('role:doc,describe:plugin', { plugin: 'seneca-doc' })
Expand All @@ -53,7 +51,6 @@ lab.test('action_desc', async () => {
var md = Render.action_desc(out.actions)
})


lab.test('update_source', async () => {
const src = `
a
Expand All @@ -66,9 +63,9 @@ d
<!--END:bar-->
e
`
var out = Inject.update_source(src,{
foo:{text:'BBB'},
bar:{text:'DDD'}
var out = Inject.update_source(src, {
foo: { text: 'BBB' },
bar: { text: 'DDD' }
})

expect(out).equal(`
Expand All @@ -84,17 +81,16 @@ e
`)
})


lab.test('update_file', async () => {
var foo_text = ''+Math.random()
var bar_text = ''+Math.random()
Inject.update_file(__dirname+'/test.md',{
foo:{text:''+foo_text},
bar:{text:''+bar_text}
var foo_text = '' + Math.random()
var bar_text = '' + Math.random()

Inject.update_file(__dirname + '/test.md', {
foo: { text: '' + foo_text },
bar: { text: '' + bar_text }
})
var out = Fs.readFileSync(__dirname+'/test.md').toString()

var out = Fs.readFileSync(__dirname + '/test.md').toString()
expect(out.indexOf(foo_text)).above(-1)
expect(out.indexOf(bar_text)).above(-1)
})
Expand Down
4 changes: 2 additions & 2 deletions test/test.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
a
<!--START:foo-->
0.5429911436898835
0.5416173668029427
<!--END:foo-->
c
<!--START:bar-->
0.06788732011581677
0.46773934388398586
<!--END:bar-->
e

0 comments on commit f618296

Please sign in to comment.