[apps/tidep0084.git] / example / iot-gateway / node_modules / aws-iot-device-sdk / node_modules / mqtt / node_modules / help-me / help-me.js
2 var fs = require('fs')
3 , path = require('path')
4 , PassThrough = require('readable-stream').PassThrough
5 , xtend = require('xtend')
6 , defaults = {
7 dir: path.join(path.dirname(require.main.filename), 'doc')
8 , ext: '.txt'
9 }
10 , pump = require('pump')
13 function helpMe(opts) {
15 opts = xtend(defaults, opts)
17 if (!opts.dir) {
18 throw new Error('missing directory')
19 }
21 return {
22 createStream: createStream,
23 toStdout: toStdout
24 }
26 function toPath(name) {
27 return path.join(opts.dir, name + opts.ext)
28 }
30 function createStream(args) {
31 var out = new PassThrough()
32 var toStream = toPath('help')
34 if (typeof args === 'string') {
35 toStream = toPath(args)
36 } else if (args && args.length > 0) {
37 toStream = toPath(args[0])
38 }
40 return fs.createReadStream(toStream)
41 .on('error', function(err) {
42 out.emit('error', err)
43 })
44 .pipe(out)
45 }
47 function toStdout(args) {
48 createStream(args)
49 .on('error', function(err) {
50 console.log('no such help file\n')
51 toStdout()
52 })
53 .pipe(process.stdout)
54 }
55 }
57 module.exports = helpMe