|
1 |
| -module.exports = octoherd; |
| 1 | +import { resolve } from "path"; |
| 2 | +import { appendFileSync } from "fs"; |
2 | 3 |
|
3 |
| -const { resolve } = require("path"); |
| 4 | +import { Octokit } from "@octoherd/octokit"; |
| 5 | +import chalk from "chalk"; |
| 6 | +import tempy from "tempy"; |
4 | 7 |
|
5 |
| -const { Octokit: OctokitCore } = require("@octokit/core"); |
6 |
| -const { paginateRest } = require("@octokit/plugin-paginate-rest"); |
7 |
| -const { throttling } = require("@octokit/plugin-throttling"); |
8 |
| -const { retry } = require("@octokit/plugin-retry"); |
9 |
| -const pino = require("pino"); |
| 8 | +import { cache as octokitCachePlugin } from "./lib/octokit-plugin-cache.js"; |
| 9 | +import { resolveRepositories } from "./lib/resolve-repositories.js"; |
| 10 | +import { VERSION } from "./version.js"; |
10 | 11 |
|
11 |
| -const { cache: octokitCachePlugin } = require("./lib/octokit-plugin-cache"); |
12 |
| -const { resolveRepositories } = require("./lib/resolve-repositories"); |
13 |
| -const { name, version } = require("./package.json"); |
14 |
| - |
15 |
| -const logger = pino(); |
16 |
| -const Octokit = OctokitCore.plugin(paginateRest, throttling, retry).defaults({ |
17 |
| - log: { |
18 |
| - debug: logger.debug.bind(logger), |
19 |
| - info: logger.info.bind(logger), |
20 |
| - warn: logger.warn.bind(logger), |
21 |
| - error: logger.error.bind(logger), |
22 |
| - }, |
23 |
| - userAgent: [name, version].join("/"), |
24 |
| - throttle: { |
25 |
| - onAbuseLimit: (error, options, octokit) => { |
26 |
| - octokit.log.error("onAbuseLimit", error, options); |
27 |
| - }, |
28 |
| - onRateLimit: (error, options, octokit) => { |
29 |
| - octokit.log.error("onRateLimit", error, options); |
30 |
| - }, |
31 |
| - }, |
32 |
| -}); |
| 12 | +const levelColor = { |
| 13 | + debug: chalk.bgGray.black, |
| 14 | + info: chalk.bgGreen.black, |
| 15 | + warn: chalk.bgYellow.black, |
| 16 | + error: chalk.bgRed.white.bold, |
| 17 | +}; |
33 | 18 |
|
34 | 19 | /**
|
35 | 20 | * Find all releases in a GitHub repository or organization after a specified date
|
36 | 21 | *
|
37 | 22 | * @param {object} options
|
38 |
| - * @param {string} options.token Personal Access Token: Requires the "public_repo" scope for public repositories, "repo" scope for private repositories. |
39 |
| - * @param {string} options.script Path to script to run against a repository |
40 |
| - * @param {string} options.repos Array of repository names in the form of "repo-owner/repo-name". To match all repositories for an owner, pass "repo-owner/*" |
41 |
| - * @param {boolean} options.cache Cache responses for debugging |
| 23 | + * @param {string} options.octoherdToken Personal Access Token: Requires the "public_repo" scope for public repositories, "repo" scope for private repositories. |
| 24 | + * @param {string} options.octoherdScript Path to script to run against a repository |
| 25 | + * @param {string} options.octoherdCache Array of repository names in the form of "repo-owner/repo-name". To match all repositories for an owner, pass "repo-owner/*" |
| 26 | + * @param {boolean} options.octoherdRepos Cache responses for debugging |
42 | 27 | */
|
43 |
| -async function octoherd( |
| 28 | +export async function octoherd( |
44 | 29 | options = {
|
45 |
| - cache: false, |
46 |
| - repos: [], |
| 30 | + octoherdCache: false, |
| 31 | + octoherdRepos: [], |
47 | 32 | }
|
48 | 33 | ) {
|
49 |
| - const { token, script, repos, cache, ...userOptions } = options; |
50 |
| - const MyOctokit = cache ? Octokit.plugin(octokitCachePlugin) : Octokit; |
51 |
| - const octokit = new MyOctokit({ |
52 |
| - auth: token, |
| 34 | + const { |
| 35 | + octoherdToken, |
| 36 | + octoherdScript, |
| 37 | + octoherdCache, |
| 38 | + octoherdRepos, |
| 39 | + ...userOptions |
| 40 | + } = options; |
| 41 | + const tmpLogFile = tempy.file({ extension: "ndjson.log" }); |
| 42 | + |
| 43 | + const CliOctokit = octoherdCache |
| 44 | + ? Octokit.plugin(octokitCachePlugin) |
| 45 | + : Octokit; |
| 46 | + const octokit = new CliOctokit({ |
| 47 | + auth: octoherdToken, |
| 48 | + userAgent: ["octoherd-cli", VERSION].join("/"), |
| 49 | + octoherd: { |
| 50 | + debug: true, |
| 51 | + onLogMessage(level, message, additionalData) { |
| 52 | + console.log( |
| 53 | + levelColor[level](" " + level.toUpperCase() + " "), |
| 54 | + Object.keys(additionalData).length |
| 55 | + ? `${message} ${chalk.gray(JSON.stringify(additionalData))}` |
| 56 | + : message |
| 57 | + ); |
| 58 | + }, |
| 59 | + onLogData(data) { |
| 60 | + appendFileSync(tmpLogFile, JSON.stringify(data)); |
| 61 | + }, |
| 62 | + }, |
53 | 63 | });
|
54 | 64 |
|
55 | 65 | let userScript;
|
56 |
| - const path = resolve(process.cwd(), script); |
| 66 | + const path = resolve(process.cwd(), octoherdScript); |
| 67 | + |
57 | 68 | try {
|
58 |
| - userScript = require(path).script; |
| 69 | + userScript = (await import(path)).script; |
59 | 70 | } catch (error) {
|
60 |
| - throw new Error(`[octoherd] ${script} script could not be found`); |
| 71 | + throw new Error(`[octoherd] ${octoherdScript} script could not be found`); |
61 | 72 | }
|
62 | 73 |
|
63 | 74 | if (!userScript) {
|
64 | 75 | throw new Error(`[octoherd] no "script" exported at ${path}`);
|
65 | 76 | }
|
66 | 77 |
|
67 |
| - if (repos.length === 0) { |
| 78 | + if (octoherdCache.length === 0) { |
68 | 79 | throw new Error("[octoherd] No repositories provided");
|
69 | 80 | }
|
70 | 81 |
|
71 |
| - state = { |
| 82 | + const state = { |
72 | 83 | log: console,
|
73 | 84 | octokit,
|
74 | 85 | };
|
75 | 86 |
|
76 |
| - const repositories = await resolveRepositories(state, repos); |
| 87 | + try { |
| 88 | + const repositories = await resolveRepositories(state, octoherdRepos); |
77 | 89 |
|
78 |
| - for (const repository of repositories) { |
79 |
| - octokit.log.info("Running %s on %s...", script, repository.full_name); |
80 |
| - await userScript(octokit, repository, userOptions); |
81 |
| - } |
| 90 | + for (const repository of repositories) { |
| 91 | + octokit.log.info( |
| 92 | + "Running %s on %s...", |
| 93 | + octoherdScript, |
| 94 | + repository.full_name |
| 95 | + ); |
| 96 | + await userScript(octokit, repository, userOptions); |
| 97 | + } |
82 | 98 |
|
83 |
| - octokit.log.info("done"); |
| 99 | + console.log(""); |
| 100 | + console.log(levelColor.info(" DONE "), `Log file written to ${tmpLogFile}`); |
| 101 | + } catch (error) { |
| 102 | + octokit.log.error(error); |
| 103 | + console.log(""); |
| 104 | + console.log( |
| 105 | + levelColor.error(" DONE "), |
| 106 | + `Log file written to ${tmpLogFile}` |
| 107 | + ); |
| 108 | + process.exit(1); |
| 109 | + } |
84 | 110 | }
|
0 commit comments