Skip to content

Commit

Permalink
add script to update npm stats for code resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Nov 26, 2019
1 parent 608553b commit 65a00fd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"scripts": {
"start": "ssg dev",
"build": "ssg export",
"update": "node scripts/updateResources.js"
"update": "node scripts/updateResources.js",
"update-npm-stats": "node scripts/updateNpmStats.js"
}
}
64 changes: 64 additions & 0 deletions scripts/updateNpmStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Fetches and updates the number of monthly npm downloads on each resource.
// api docs: https://github.com/npm/registry/blob/master/docs/download-counts.md

const fs = require("fs");
const yaml = require("js-yaml");
const fetch = require("node-fetch");

const filePath = "data/code.yml";
const period = "point/last-month";
const extractNpmPackage = resource => {
const matches = resource.name.match(/^`(.+)`$/);
return matches && matches[1];
};

async function updateDownloads() {
const data = yaml.safeLoad(fs.readFileSync(filePath, "utf8"));

const updatedResources = [];
for (const resource of data.resources) {
const npmPackage = extractNpmPackage(resource);
if (!npmPackage) {
updatedResources.push(resource);
continue;
}

let result = await fetch(
`https://api.npmjs.org/downloads/${period}/${npmPackage}`
);
result = await result.json();

if (result.error) {
if (result.error.includes("not found")) {
throw Error(
`npm package "${npmPackage}" not found.` +
" If the resource is not supposed to be an npm package," +
" remove the backticks from its name."
);
} else {
throw Error(`npm error: ${JSON.stringify(result)}`);
}
}

updatedResources.push({
...resource,
downloads: result.downloads
});
console.log(`updated ${npmPackage}`);
}

const finishedYaml = {
...data,
resources: updatedResources
};

fs.writeFile(filePath, yaml.safeDump(finishedYaml), err => {
if (err) {
console.log(err);
}
});
}

updateDownloads()
.then(() => console.log("done!"))
.catch(err => console.error(err));

0 comments on commit 65a00fd

Please sign in to comment.