coverage.js 2.52 KB
#!/usr/bin/env node
var program = require('commander')
var path = require('path')
const nopt = require('nopt')
const config = nopt({ coverage: Boolean })
const configArray = ['s', 'serve', 'n', 'nodemon'].filter(item => config[item])
const coverageRequired = configArray.length >= 1

var istanbulMiddleware = require(path.resolve(__dirname, '..', 'lib/autoIstanbulLib/autoIstanbulHandlers.js'))
var { FRACTION_OF_COVERAGE_PORT_DATA } = istanbulMiddleware.globalVariable()
var exec = require('child_process').exec
var stopPm2 = `pm2 stop all`
const port = FRACTION_OF_COVERAGE_PORT_DATA
program
    .option('-h --help', 'help information', helpInformation)
    .option('-s --serve', 'start the service', serveInformation)
    .option('-n --nodemon', 'close the service', nodemonInformation)
    .option('-c --close', 'close the service', closeInformation)
    .parse(process.argv);                                                                                                                                                                                                                                                                                                                                             
    
    function helpInformation() {
        console.log('options:');
        console.log('');
        console.log('-s --serve             start the service');
        console.log('-n --nodemon             start the service');
        console.log('-c --close             close the service');
        console.log('');
    };

    function serveInformation() {
        runSever()
    }

    function nodemonInformation() {
        runSever()
    }

    function closeInformation() {
        exec(stopPm2, (err, stdout, stderr) => {
            if (err){
                console.log(err);
                console.warn(new Date(),' 代码覆盖率服务残留停止失败');
            } else {
                console.log(stdout);
                console.warn(new Date(),' 代码覆盖率服务残留停止成功');
            }
        })
    }
    function runSever() {
        if (coverageRequired) {
        console.log('Turning on coverage; ensure this is not production')
        istanbulMiddleware.hookLoader(__dirname, { verbose: true })
        }
        console.log('Starting server at: http://localhost:' + port)
        if (!coverageRequired) {
        console.log('Coverage NOT turned on, run with --coverage to turn it on')
        }
        require(path.resolve(__dirname, '..', 'lib/lstanbulabServer/index.js')).serve(port, coverageRequired)
    }