From 953fc02274832f6ddd21a8b88b91b71bc1fcbaa1 Mon Sep 17 00:00:00 2001 From: Jeffrey Yasskin Date: Mon, 16 Oct 2023 20:56:00 -0700 Subject: [PATCH] Stop requiring a config.json, and set up command line parsing. --- scanner/main.ts | 9 +++++---- scanner/package.json | 1 + scanner/pnpm-lock.yaml | 8 ++++++++ scanner/third_party/config.cjs | 12 ++++++++++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/scanner/main.ts b/scanner/main.ts index b649e6e..8e769ff 100644 --- a/scanner/main.ts +++ b/scanner/main.ts @@ -2,6 +2,7 @@ import Bottleneck from "bottleneck"; import specs from 'browser-specs' assert { type: "json" }; import fs from 'node:fs/promises'; import { mean, quantile } from 'simple-statistics'; +import config from './third_party/config.cjs'; import octokit from './third_party/octokit-cache.js'; const ghLimiter = new Bottleneck({ @@ -63,7 +64,7 @@ function ageStats(arr: number[]): AgeStats | undefined { async function analyzeRepo(org: string, repo: string, globalStats: GlobalStatsInput): Promise { let result: RepoSummary | null = null; try { - result = JSON.parse(await fs.readFile(`summaries/${org}/${repo}.json`, { encoding: 'utf8' }), + result = JSON.parse(await fs.readFile(`${config.outDir}/${org}/${repo}.json`, { encoding: 'utf8' }), (key, value) => { if (['created_at', 'closed_at'].includes(key)) { return new Date(value); @@ -116,8 +117,8 @@ async function analyzeRepo(org: string, repo: string, globalStats: GlobalStatsIn globalStats.closeAgesMs.push(...closeAgesMs); globalStats.openAgesMs.push(...openAgesMs); - await fs.mkdir(`summaries/${org}`, { recursive: true }); - await fs.writeFile(`summaries/${org}/${repo}.json`, JSON.stringify(result, undefined, 2)); + await fs.mkdir(`${config.outDir}/${org}`, { recursive: true }); + await fs.writeFile(`${config.outDir}/${org}/${repo}.json`, JSON.stringify(result, undefined, 2)); return result; } @@ -148,7 +149,7 @@ async function main() { const globalStats: GlobalStatsInput = { openAgesMs: [], closeAgesMs: [] }; await Promise.all(githubRepos.map(({ org, repo }) => analyzeRepo(org, repo, globalStats))); - await fs.writeFile("summaries/global.json", JSON.stringify({ + await fs.writeFile(`${config.outDir}/global.json`, JSON.stringify({ ageAtCloseMs: ageStats(globalStats.closeAgesMs), openAgeMs: ageStats(globalStats.openAgesMs), }, undefined, 2)); diff --git a/scanner/package.json b/scanner/package.json index dfe8bdd..0ab13b8 100644 --- a/scanner/package.json +++ b/scanner/package.json @@ -16,6 +16,7 @@ "@octokit/plugin-throttling": "^8.0.0", "bottleneck": "^2.19.5", "browser-specs": "^3.62.0", + "commander": "^11.1.0", "node-fetch": "^3.3.2", "simple-statistics": "^7.8.3" }, diff --git a/scanner/pnpm-lock.yaml b/scanner/pnpm-lock.yaml index e5c3d13..ea06187 100644 --- a/scanner/pnpm-lock.yaml +++ b/scanner/pnpm-lock.yaml @@ -17,6 +17,9 @@ dependencies: browser-specs: specifier: ^3.62.0 version: 3.65.0 + commander: + specifier: ^11.1.0 + version: 11.1.0 node-fetch: specifier: ^3.3.2 version: 3.3.2 @@ -136,6 +139,11 @@ packages: resolution: {integrity: sha512-z0XFyjREfFZ3hGjKoPKnHHJFCbtkEh94EtkSQ3fmLFwcSTo2dJzrPD1qdbTiOJisIdRntwcmp1dXqLdCrKCbUg==} dev: false + /commander@11.1.0: + resolution: {integrity: sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==} + engines: {node: '>=16'} + dev: false + /data-uri-to-buffer@4.0.1: resolution: {integrity: sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==} engines: {node: '>= 12'} diff --git a/scanner/third_party/config.cjs b/scanner/third_party/config.cjs index f2a862c..822ce36 100644 --- a/scanner/third_party/config.cjs +++ b/scanner/third_party/config.cjs @@ -4,8 +4,15 @@ "use strict"; const path = require('path'); -const config = require("../config.json"); +const { program } = require('commander'); +const config = {}; + +program + .option('--gh-token ', undefined, process.env["GITHUB_TOKEN"] || "missing-GitHub-token") + .option('--out-dir ', "where to write scan results", "summaries"); + +const options = program.opts(); // environment variables @@ -20,11 +27,12 @@ config.debug = (config.env === "development") || config.debug || false; // auth tokens and keys -config.ghToken = config.ghToken || "missing-GitHub-token"; +config.ghToken = options.ghToken; // app specifics config.cache = config.cache || "https://labs.w3.org/github-cache"; +config.outDir = options.outDir; // dump the configuration into the server log (but not in the server monitor!) console.log("".padStart(80, '-'));