Skip to content

Commit

Permalink
ES Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
amezin committed Nov 25, 2023
1 parent 3b6cbfd commit a589fae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
61 changes: 35 additions & 26 deletions cleanup.js
Original file line number Diff line number Diff line change
@@ -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 });
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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',
})
Expand All @@ -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(
Expand Down Expand Up @@ -165,33 +174,33 @@ 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,
);

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();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down

0 comments on commit a589fae

Please sign in to comment.