Skip to content

Commit a37c60c

Browse files
committed
[+] Analysis: LOC, SLOC e CLOC
1 parent 8c61d0f commit a37c60c

2 files changed

Lines changed: 96 additions & 4 deletions

File tree

src/analysis.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
};

src/mine.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const moment = require('moment');
22
const ObjectsToCsv = require('objects-to-csv');
33
const fetch = require('./utils/fetch');
44
const l = require('./utils/logger');
5+
const analysis = require('./analysis');
56

67
class Mine {
78
constructor(objective, language) {
@@ -19,7 +20,7 @@ class Mine {
1920
const digs = [];
2021
let tag = `[${this.current}/${this.objective}]`;
2122
while (this.current <= this.objective) {
22-
console.log(`${tag} Buscando...`);
23+
console.log(`${tag} Buscando página...`);
2324
// eslint-disable-next-line no-await-in-loop
2425
await this.dig(token, tag);
2526
tag = `[${this.current}/${this.objective}]`;
@@ -34,7 +35,8 @@ class Mine {
3435
await fetch(token, this.cursor, this.language).then((res) => {
3536
this.cursor = res.pageInfo.endCursor || null;
3637
this.current += 1;
37-
return Mine.store(this.file, Mine.polish(res.nodes, tag), tag);
38+
const data = Mine.polish(res.nodes, tag);
39+
return analysis(data, tag, this.file);
3840
});
3941
} catch (e) {
4042
l.error(`${tag} Erro na requisição:`, e.message);
@@ -50,12 +52,12 @@ class Mine {
5052
bom: true,
5153
})
5254
.then(() => {
53-
l.success(`${tag} Salvo em ${file}`);
55+
l.success(`${tag} Resultados da página salvos em ${file}`);
5456
});
5557
}
5658

5759
static polish(dirt, tag) {
58-
console.log(`${tag} Formatando...`);
60+
console.log(`${tag} Formatando página...`);
5961
return dirt.map((repo) => {
6062
return {
6163
'<usuário>/<repositório>': repo.nameWithOwner,

0 commit comments

Comments
 (0)