UNPKG

neobrag

Version:

neobrag lets you brag to your fellow developers

143 lines (121 loc) 4.33 kB
#!/usr/bin/env node const util = require('util'); const exec = util.promisify(require('child_process').exec); const options = require('./js/setup'); const path = require('path'); const fs = require('fs'); const csv = require('csvtojson'); const Pie = require("cli-pie"); function getCodeDirs() { const codeDirs = path.resolve(__dirname, "./cache/code"); return fs.readdirSync(codeDirs).filter(function (file) { return fs.statSync(codeDirs + '/' + file).isDirectory(); }); } const customName = function(input){ try{ const nameMap = require('./config/name-mapping.json'); return nameMap[input] ? nameMap[input] : input; } catch(e){ //quietly die return input; } } const clocBlame = async function (cachePath, shellPath, repos) { //initiate cloc-blame const statsPath = path.resolve(__dirname, "./stats"); if(!options.quick){ console.info(`Processing files (this may take a while)...`); for (let repo of repos) { console.log(`Processing ${repo}...`); const { stderr, stdout } = await exec(`cd "${cachePath}/code/${repo}" ; statsDir="${statsPath}" cacheDir="${cachePath}" repo="${repo}" "${shellPath}/cloc-blame.sh"`); console.log(stdout); } } let stats = { } for (let repo of repos) { //now read the csv const results = await csv({ noheader: false, headers: ['File Type','Name','Line Count'] }).fromFile(`${statsPath}/${repo}-summary.csv`); fs.writeFileSync(`${statsPath}/${repo}-summary.json`, JSON.stringify(results)); //now that we have some usable JSON, we count everything results.forEach(r =>{ let name = customName(r['Name']); if(name == "ignore") return; if(!stats[name]){ stats[name] = 0; } stats[name] += parseInt(r['Line Count']); }) } console.clear(); console.log(`DEV STATS:\n`); await drawPie(stats); console.table(stats); } const drawPie = async function(stats){ let chart = new Pie(10, [], { legend: true }); Object.keys(stats).forEach(stat =>{ chart.add({ label: stat , value: stats[stat] }) }); console.log(chart.toString()); } const neobrag = async function () { if(process.argv.length == 2) options['use-cache'] = true; try { //check for dependencies const shellPath = path.resolve(__dirname, "./shell"); const cachePath = path.resolve(__dirname, "./cache"); const configPath = path.resolve(__dirname, "./config"); let repos = []; let { stdout } = await exec(`"${shellPath}/check-deps.sh"`); if (!stdout.includes("Dependencies Found")) { console.log(stdout); throw "Dependencies not found; you need git" } if (options['config']) { //read the file from the path let configContents = fs.readFileSync(options['config']).toString(); configContents = configContents.split("\n"); fs.writeFileSync(`${configPath}/config.json`, JSON.stringify(configContents)); } if (options['name-mapping']) { //read the file from the path let nameMapping = JSON.parse(fs.readFileSync(options['name-mapping']).toString()); fs.writeFileSync(`${configPath}/name-mapping.json`, JSON.stringify(nameMapping)); } if (!options['use-cache']) { repos = JSON.parse(fs.readFileSync(`${configPath}/config.json`)); await fs.rmSync(`${cachePath}/code`, { recursive: true, force: true }); await fs.mkdirSync(`${cachePath}/code`); //clone repose listed in config for (let repo of repos) { console.info(`Cloning ${repo} to cache dir...`); await exec(`cd "${cachePath}/code" ; git clone ${repo}`); } } const repoNames = getCodeDirs(); await clocBlame(cachePath, shellPath, repoNames); } catch (e) { console.error(e); process.exitCode = 1; } } neobrag();