Last active
June 26, 2020 08:06
-
-
Save sholladay/6a2169467edc7bc1e7633b65af244eed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
const path = require('path'); | |
const util = require('util'); | |
const childProcess = require('child_process'); | |
const gh = require('gh-got'); | |
const mkdirtemp = require('mkdirtemp'); | |
const exec = util.promisify(childProcess.exec); | |
const execFile = util.promisify(childProcess.execFile); | |
// Tip: Add an access token in the GITHUB_TOKEN environment variable to increase rate limits. | |
const api = gh.extend(); | |
(async () => { | |
console.log('Fetching list of repositories...') | |
const repos = await api.paginate.all('users/hapijs/repos'); | |
const tempDir = await mkdirtemp(); | |
console.log('Downloading packages to temp directory:', tempDir); | |
process.chdir(tempDir); | |
const lineCounts = await Promise.all(repos.map(async (repo) => { | |
const pkgUrl = new URL(repo.contents_url.replace(/\/\{\+path\}$/, '/package.json')); | |
try { | |
await api.head(pkgUrl.pathname + pkgUrl.search + pkgUrl.hash, { prefixUrl : pkgUrl.origin }); | |
} | |
catch (error) { | |
if (error.response?.statusCode === 404) { | |
return 0; | |
} | |
throw error; | |
} | |
const pack = await execFile('npm', ['pack', repo.full_name], { cwd : tempDir }); | |
const archiveName = pack.stdout.trim(); | |
const archivePath = path.join(tempDir, archiveName); | |
const pkgId = path.parse(archiveName).name; | |
const unpackDir = path.join(tempDir, pkgId); | |
await fs.promises.mkdir(unpackDir); | |
await execFile('tar', ['-xzf', archivePath, '--strip-components=1'], { cwd : unpackDir }); | |
const wc = await exec('wc -l **.{j,t}s', { cwd : unpackDir, shell : 'fish' }); | |
const numLines = Number.parseInt(wc.stdout.trimRight().split(/\r?\n/).slice(-1)); | |
return numLines; | |
})); | |
const lineTotal = lineCounts.reduce((result, lineCount) => { | |
return result + lineCount; | |
}, 0); | |
console.log('Number of repos:', repos.length); | |
console.log('Total lines:', lineTotal); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment