diff --git a/cleanup.js b/cleanup.js index 5f05a1e..3e2d6b2 100755 --- a/cleanup.js +++ b/cleanup.js @@ -1,12 +1,14 @@ #!/usr/bin/env node -const http = require('http'); -const https = require('https'); -const stream = require('node:stream'); -const url = require('node:url'); +import http from 'http'; +import https from 'https'; +import stream from 'node:stream'; +import url from 'node:url'; -const yargs = require('yargs/yargs'); -const merge_stream = require('merge-stream'); +import yargs from 'yargs/yargs'; +import { hideBin } from 'yargs/helpers'; + +import merge_stream from 'merge-stream'; const httpAgent = new http.Agent({ keepAlive: true }); const httpsAgent = new https.Agent({ keepAlive: true }); @@ -15,11 +17,11 @@ function agent(url) { return url.protocol == 'http:' ? httpAgent : httpsAgent; } -const Octokit = require('@octokit/core').Octokit.plugin( - require('@octokit/plugin-paginate-rest').paginateRest, - require('@octokit/plugin-throttling').throttling, - require('@octokit/plugin-retry').retry, - require('@octokit/plugin-request-log').requestLog, +const Octokit = (await import('@octokit/core')).Octokit.plugin( + (await import('@octokit/plugin-paginate-rest')).paginateRest, + (await import('@octokit/plugin-throttling')).throttling, + (await import('@octokit/plugin-retry')).retry, + (await import('@octokit/plugin-request-log')).requestLog ) // from docker/metadata-action @@ -28,7 +30,9 @@ function sanitizeTag(tag) { } async function main() { - const args = yargs(require('yargs/helpers').hideBin(process.argv)) + const log_levels = ['debug', 'info', 'warn', 'error']; + + const args = yargs(hideBin(process.argv)) .option('token', { alias: 't', demandOption: true, @@ -77,7 +81,7 @@ async function main() { alias: 'v', demandOption: true, describe: 'Console log level', - choices: ['trace', 'debug', 'info', 'warn', 'error', 'fatal'], + choices: log_levels, requiresArg: true, default: 'info', }) @@ -97,12 +101,17 @@ async function main() { .strict() .argv; + const logLevel = log_levels.indexOf(args.logLevel) + const log = Object.fromEntries( + log_levels.map( + (name, index) => index >= logLevel ? console[name].bind(console) : new Function() + ) + ); + const octokit = new Octokit({ auth: args.token, baseUrl: args.apiUrl, - log: require('console-log-level')({ - level: args.logLevel, - }), + log, throttle: { onRateLimit: (retryAfter, options, octokit) => { octokit.log.warn( @@ -165,21 +174,21 @@ async function main() { ).flatMap(response => response.data); const repoPackages = packages.filter( - package => ( - package.repository && package.repository.node_id === repo.node_id + pkg => ( + pkg.repository && pkg.repository.node_id === repo.node_id ) ); const repoPackagesWithVersions = repoPackages.map( - async package => { - package.versions = await stream.Readable.from( + async pkg => { + pkg.versions = await stream.Readable.from( octokit.paginate.iterator( 'GET {+package_url}/versions', - { package_url: package.url } + { package_url: pkg.url } ) ).flatMap(response => response.data).toArray(); - return package; + return pkg; }, concurrencyOptions, ); @@ -187,11 +196,11 @@ async function main() { const registryUrl = new url.URL(args.registryUrl); const versions = repoPackagesWithVersions.flatMap( - package => { - const image = `${registryUrl.host}/${package.owner.login}/${package.name}`; - const registryBaseUrl = new url.URL(`/v2/${package.owner.login}/${package.name}/`, registryUrl).toString(); + pkg => { + const image = `${registryUrl.host}/${pkg.owner.login}/${pkg.name}`; + const registryBaseUrl = new url.URL(`/v2/${pkg.owner.login}/${pkg.name}/`, registryUrl).toString(); - return package.versions.map(version => { + return pkg.versions.map(version => { version.image = `${image}@${version.name}`; version.manifestUrl = new url.URL(`./manifests/${version.name}`, registryBaseUrl).toString(); version.blobBaseUrl = new url.URL('./blobs/', registryBaseUrl).toString(); diff --git a/package.json b/package.json index 5db3c52..98cad31 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "ghcr-cleanup", + "type": "module", "dependencies": { "@octokit/core": "^5.0.0", "@octokit/plugin-paginate-rest": "^9.0.0", "@octokit/plugin-request-log": "^4.0.0", "@octokit/plugin-retry": "^6.0.0", "@octokit/plugin-throttling": "^8.0.0", - "console-log-level": "^1.4.1", "merge-stream": "^2.0.0", "yargs": "^17.6.2" }