|
1 | 1 | #!/usr/bin/env node
|
2 | 2 |
|
3 |
| -const semver = require('semver'); |
| 3 | +require('babel-polyfill'); |
4 | 4 |
|
5 |
| -if (semver.lt(process.version, '8.5.0')) { |
6 |
| - // only shim pre 8 binaries |
7 |
| - require('babel-polyfill'); |
8 |
| - require('babel-register'); |
| 5 | +const program = require('commander'); |
| 6 | +const http = require('http'); |
| 7 | +const https = require('https'); |
| 8 | +const fs = require('fs'); |
| 9 | +const Path = require('path'); |
| 10 | +const Url = require('url'); |
| 11 | +const updateNotifier = require('update-notifier'); |
| 12 | + |
| 13 | +const lcov = require('../lib/lcov'); |
| 14 | +const cobertura = require('../lib/cobertura'); |
| 15 | +const golang = require('../lib/golang'); |
| 16 | +const jacoco = require('../lib/jacoco'); |
| 17 | + |
| 18 | +const git = require('../lib/git'); |
| 19 | +const ci = require('../lib/ci'); |
| 20 | + |
| 21 | +const pkg = require('../package.json'); |
| 22 | + |
| 23 | +updateNotifier({pkg}).notify(); |
| 24 | + |
| 25 | +program |
| 26 | + .version(pkg.version) |
| 27 | + .option('-u, --upload [server]', 'Set the url to upload lcov data too', 'http://localhost:8080') |
| 28 | + .option('-s, --serve', 'Pass this option to startup a lcov-server instance') |
| 29 | + .option('-d, --db [db]', 'Set the db connection', 'mongodb://localhost:32768/lcov-server') |
| 30 | + .option('-p, --parser <parser>', 'Set the parser value [lcov, cobertura, golang, jacoco], defaults to lcov', 'lcov') |
| 31 | + .option('-bp, --basePath <path>', 'The path that defines the base directory where the files that were covered will be located') |
| 32 | + .parse(process.argv); |
| 33 | + |
| 34 | +const { parser, upload, serve, db, basePath } = program; |
| 35 | + |
| 36 | +if(parser && ['lcov', 'cobertura', 'golang', 'jacoco'].indexOf(parser) === -1) { |
| 37 | + console.error(`parser ${parser} not supported`); // eslint-disable-line |
| 38 | + process.exit(1); |
9 | 39 | }
|
10 | 40 |
|
11 |
| -require('./index.js'); |
| 41 | +if(serve) { |
| 42 | + process.env.MONGO_URL = process.env.MONGO_URL || db; |
| 43 | + |
| 44 | + require('../index'); |
| 45 | +} else { |
| 46 | + const parsedUrl = Url.parse(upload); |
| 47 | + |
| 48 | + let input = ''; |
| 49 | + process.stdin.resume(); |
| 50 | + process.stdin.setEncoding('utf8'); |
| 51 | + process.stdin.on('data', (chunk) => { |
| 52 | + input += chunk; |
| 53 | + }); |
| 54 | + process.stdin.on('end', (async () => { |
| 55 | + const env = ci(); |
| 56 | + const output = { |
| 57 | + service_job_id: env.service_job_id, |
| 58 | + service_pull_request: env.service_pull_request, |
| 59 | + service_name: env.service_name, |
| 60 | + source_files: [], |
| 61 | + git: { |
| 62 | + commit: env.commit, |
| 63 | + branch: env.branch, |
| 64 | + message: env.message, |
| 65 | + committer_name: env.committer_name, |
| 66 | + committer_email: env.committer_email |
| 67 | + }, |
| 68 | + run_at: new Date() |
| 69 | + }; |
| 70 | + |
| 71 | + let _lcov = {}; |
| 72 | + switch(parser) { |
| 73 | + case 'lcov': |
| 74 | + _lcov = await lcov.parse(input); |
| 75 | + break; |
| 76 | + case 'cobertura': |
| 77 | + _lcov = await cobertura.parse(input); |
| 78 | + break; |
| 79 | + case 'golang': |
| 80 | + _lcov = await golang.parse(input); |
| 81 | + break; |
| 82 | + case 'jacoco': |
| 83 | + _lcov = await jacoco.parse(input); |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + const _git = await git.parse(); |
| 88 | + |
| 89 | + // Go through and set the file contents |
| 90 | + for (let i = 0; i < _lcov.length; i++) { |
| 91 | + let path = basePath ? Path.resolve(process.cwd(), basePath, _lcov[i].file) : _lcov[i].file; |
| 92 | + |
| 93 | + _lcov[i].source = fs.readFileSync(path).toString('utf8'); |
| 94 | + _lcov[i].title = _lcov[i].file.substring(_lcov[i].file.lastIndexOf('/') + 1, _lcov[i].file.length); |
| 95 | + } |
| 96 | + |
| 97 | + output['source_files'] = _lcov; |
| 98 | + output['git'] = Object.assign(output['git'], _git); |
| 99 | + |
| 100 | + const options = { |
| 101 | + hostname: parsedUrl.hostname, |
| 102 | + port: parsedUrl.port || 80, |
| 103 | + path: '/api/upload', |
| 104 | + method: 'POST', |
| 105 | + headers: { |
| 106 | + 'Content-Type': 'application/json', |
| 107 | + } |
| 108 | + }; |
| 109 | + let operation = http; |
| 110 | + let data = ''; |
| 111 | + |
| 112 | + if(parsedUrl.protocol == 'https:') { |
| 113 | + options.port = 443; |
| 114 | + operation = https; |
| 115 | + } |
| 116 | + |
| 117 | + let req = operation.request(options, (res) => { |
| 118 | + res.on('data', (chunk) => { |
| 119 | + data += chunk; |
| 120 | + }); |
| 121 | + res.on('end', () => { |
| 122 | + try { |
| 123 | + const response = JSON.parse(data); |
| 124 | + if(response.error) { |
| 125 | + console.error(response.error); // eslint-disable-line |
| 126 | + } else { |
| 127 | + console.log(`\n coverage sent successfully 💚 \n`); // eslint-disable-line |
| 128 | + } |
| 129 | + } catch(ex) { |
| 130 | + console.log(`\n uhoh something went wrong, ${ex.toString()}`); // eslint-disable-line |
| 131 | + } |
| 132 | + }); |
| 133 | + }); |
| 134 | + req.write(JSON.stringify(output)); |
| 135 | + req.end(); |
| 136 | + })); |
| 137 | +} |
0 commit comments