all files / prolific.root/ prolific.bin.js

100% Statements 46/46
100% Branches 4/4
100% Functions 14/14
100% Lines 46/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                                                                             
#!/usr/bin/env node
 
/*
    ___ usage ___ en_US ___
    usage: node prolific.tcp.bin.js
 
        -i, --inherit   <number>        file handles to inherit
        -I, --ipc                       enable Node.js IPC forwarding
            --configuration <string>    base configuration JSON or environment variable
            --help                      display this message
 
    ___ $ ___ en_US ___
 
        log is required:
            the `--log` address and port is a required argument
 
        port is not an integer:
            the `--log` port must be an integer
 
    ___ . ___
*/
 
var monitor = require('prolific.monitor')
var children = require('child_process')
var inherit = require('prolific.inherit')
var ipc = require('prolific.ipc')
var url = require('url')
 
require('arguable')(module, require('cadence')(function (async, program) {
    var configure = require('./configure')
    var killer = require('./killer')
 
    program.helpIf(program.ultimate.help)
 
    var configuration = configure(program.env, program.ultimate.configuration)
 
    var inheritance = inherit(program)
    configuration.fd = inheritance.fd
 
    var commandable = require('./commandable')
    var argv = program.argv.slice(), terminal = false
    var loop = async(function () {
        program.assert(argv.length != 0, 'no program')
        var parser = commandable(terminal, argv)
        if (parser == null) {
            process.env.PROLIFIC_CONFIGURATION = JSON.stringify(configuration)
            var nullProcessor = { process: function () {} }, nextProcessor = nullProcessor
            var processors = configuration.processors.map(function (configuration) {
                var Processor = require(configuration.moduleName)
                return nextProcessor = new Processor(configuration.parameters, nextProcessor)
            })
            processors.reverse()
            var initialized = []
            async([function () {
                async.forEach(function (processor) {
                    processor.close(async())
                })(initialized)
            }], function () {
                async.forEach(function (processor) {
                    async(function () {
                        processor.open(async())
                    }, function () {
                        initialized.push(processor)
                    })
                })(processors)
            }, function () {
                var child = children.spawn(argv.shift(), argv, { stdio: inheritance.stdio })
                // Let child shtudown to shut us down.
                program.on('SIGINT', killer(child, 'SIGINT'))
                program.on('SIGTERM', killer(child, 'SIGTERM'))
                var io = { async: child.stdio[inheritance.fd], sync: child.stderr }
                ipc(program.ultimate.ipc, process, child)
                processors.push(nullProcessor)
                monitor(processors[0], child, io, program.stderr, async())
            }, function (code) {
                return [ loop.break, code ]
            })
        } else {
            async(function () {
                parser(argv, {
                    properties: { configuration: configuration }
                }, async())
            }, function (processor) {
                if (processor.moduleName) {
                    configuration.processors.push(processor)
                }
                argv = processor.argv
                terminal = processor.terminal
            })
        }
    })()
}))