Skip to content

Instantly share code, notes, and snippets.

@xiwcx
Last active October 29, 2018 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiwcx/63642a09447497f36ab8b90e31f230b4 to your computer and use it in GitHub Desktop.
Save xiwcx/63642a09447497f36ab8b90e31f230b4 to your computer and use it in GitHub Desktop.
packages.json to CSV w/ meta
const data = require('path/to/package.json')
const repoUrl = require('get-repository-url')
const scrapeIt = require('scrape-it')
const sortBy = require('lodash.sortby')
const writeCSV = require('write-csv')
// scrape it configuration to collect the number of stars and latest tagged version
// https://github.com/IonicaBizau/scrape-it#memo-documentation
const scrapeConfig = {
latest: {
selector: '.select-menu-list[data-tab-filter="tags"] a',
eq: 0
},
stars: '.js-social-count'
}
const retrieveMeta = async k => {
// first, retrieve the repository url from the package.json
const repo = await repoUrl(k)
.then(url => url ? url : 'N/A')
.catch(() => 'N/A')
// store whether the package has a repository
const keyHasURL = repo.startsWith('http')
// if it does have a repository perform the additional meta information retrieval
if (keyHasURL) {
var ghMeta = await scrapeIt(repo, scrapeConfig)
.then(({ data, response }) => data)
var latest = ghMeta.latest
// strip the 'v' if it exists to match package.json tag format
var latestVersion = latest.startsWith('v') ? latest.substring(1) : latest
}
// format objects
return {
name: k,
repo,
'our version': allDependencies[k],
'latest version': keyHasURL ? latestVersion : null,
stars: keyHasURL ? ghMeta.stars : null,
}
}
// combine dependencies and devDependencies
const allDependencies = Object.assign(data.devDependencies, data.dependencies)
// create new array from packages object
const formmatedDependencies = Object.keys(allDependencies).map(retrieveMeta)
// wait for all information to be retrieved and aphabetize by name
Promise.all(formmatedDependencies)
.then(values => writeCSV('./results.csv', sortBy(values, 'name')))
{
"name": "packages-to-csv",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"get-repository-url": "^2.0.0",
"lodash.sortby": "^4.7.0",
"scrape-it": "^5.1.0",
"write-csv": "^1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment