|
| 1 | +const d = require('download-git-repo'); |
| 2 | +const { exec } = require('child_process'); |
| 3 | +const { promisify } = require('util'); |
| 4 | +const rimraf = require('rimraf'); |
| 5 | +const ObjectsToCsv = require('objects-to-csv'); |
| 6 | +const l = require('./utils/logger'); |
| 7 | + |
| 8 | +const executeCommand = promisify(exec); |
| 9 | + |
| 10 | +async function store(file, data, tag) { |
| 11 | + console.log(`${tag} Salvando...`); |
| 12 | + return new ObjectsToCsv([data]) |
| 13 | + .toDisk(file, { |
| 14 | + append: true, |
| 15 | + bom: true, |
| 16 | + }) |
| 17 | + .then(() => { |
| 18 | + l.success(`${tag} Salvo em ${file}`); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +function download(nameWithOwner, path) { |
| 23 | + return new Promise((resolve, reject) => { |
| 24 | + d(nameWithOwner, path, (err) => { |
| 25 | + if (err) reject(err); |
| 26 | + resolve(); |
| 27 | + }); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +async function clone(nameWithOwner, tag) { |
| 32 | + const split = nameWithOwner.split('/'); |
| 33 | + const path = `./temp/${split[0]}-${split[1]}`; |
| 34 | + |
| 35 | + l.status(`${tag} Baixando ${nameWithOwner} para ${path}`); |
| 36 | + return download(nameWithOwner, path).then(async () => { |
| 37 | + l.log(`${tag} Analisando LOC, SLOC e CLOC...`); |
| 38 | + const { stdout } = await executeCommand( |
| 39 | + `sloc ${path} --keys total,source,comment` |
| 40 | + ); |
| 41 | + |
| 42 | + const output = stdout |
| 43 | + .replace(/\D/gim, ' ') |
| 44 | + .split(' ') |
| 45 | + .filter((e) => e); |
| 46 | + |
| 47 | + const results = { |
| 48 | + total: parseInt(output[0], 10), |
| 49 | + source: parseInt(output[1], 10), |
| 50 | + comments: parseInt(output[2], 10), |
| 51 | + }; |
| 52 | + |
| 53 | + try { |
| 54 | + l.log(`${tag} Análise feita. Apagando ${path}`); |
| 55 | + rimraf.sync(path); |
| 56 | + } catch (error) { |
| 57 | + l.error(`${tag} Não foi possível apagar ${path}:`, error.message); |
| 58 | + l.log(`${tag} Continuando para o próximo...`); |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + // eslint-disable-next-line no-restricted-globals |
| 63 | + total: isNaN(results.total) ? 0 : results.total, |
| 64 | + // eslint-disable-next-line no-restricted-globals |
| 65 | + source: isNaN(results.source) ? 0 : results.source, |
| 66 | + // eslint-disable-next-line no-restricted-globals |
| 67 | + comments: isNaN(results.comments) ? 0 : results.comments, |
| 68 | + }; |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +async function init(repos, tag, file) { |
| 73 | + for (let i = 0; i < repos.length; i += 1) { |
| 74 | + const repo = repos[i]; |
| 75 | + // eslint-disable-next-line no-await-in-loop |
| 76 | + const results = await clone(repo['<usuário>/<repositório>'], tag); |
| 77 | + // eslint-disable-next-line no-param-reassign |
| 78 | + repos[i].LOC = results.total; |
| 79 | + // eslint-disable-next-line no-param-reassign |
| 80 | + repos[i].SLOC = results.source; |
| 81 | + // eslint-disable-next-line no-param-reassign |
| 82 | + repos[i].CLOC = results.comments; |
| 83 | + // eslint-disable-next-line no-await-in-loop |
| 84 | + await store(file, repos[i], tag); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +module.exports = (repos, tag, file) => { |
| 89 | + return init(repos, tag, file); |
| 90 | +}; |
0 commit comments