Skip to content

Commit

Permalink
CI: gulp task in support of refcache cleanup & some refcache updates (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chalin authored Jun 30, 2023
1 parent 4f41f0b commit 5556f79
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 412 deletions.
3 changes: 3 additions & 0 deletions gulp-src/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Gulp task files

All files in this directory, except for `helpers.js`, are gulp tasks.
9 changes: 9 additions & 0 deletions gulp-src/_util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');

exports.taskArgs = () =>
yargs(hideBin(process.argv).slice(1))
.strict()
.help('info')
// To avoid "task did not complete" errors, prevent help option from exiting
.exitProcess(false);
15 changes: 15 additions & 0 deletions gulp-src/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const gulp = require('gulp');

const usage = `
Usage:
npx gulp --tasks # for a list of tasks
npx gulp <task> --info for task argument info`;

const usageTask = (done) => {
console.log(usage);
done();
};

usageTask.description = 'Display usage instructions';

gulp.task('default', usageTask);
57 changes: 57 additions & 0 deletions gulp-src/prune.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// cSpell:ignore refcache

const gulp = require('gulp');
const fs = require('fs').promises;
const { taskArgs } = require('./_util');

const refcacheFile = 'static/refcache.json';
const n_default = 25;

// The refcacheFile is a JSON map with each map entry of the form, e.g.:
//
// "https://cncf.io": {
// "StatusCode": 206,
// "LastSeen": "2023-06-29T13:38:47.996793-04:00"
// },

// Prune the oldest <n> entries from refcacheFile in a way that avoids
// reordering entries (makes diffs easier to manage).
async function pruneTask() {
const argv = taskArgs().options({
num: {
alias: 'n',
type: 'number',
description: 'Number of oldest refcache entries to drop.',
default: n_default,
},
}).argv;

const n = argv.num > 0 ? argv.num : n_default;

if (argv.info) return; // Info was already displayed

try {
const json = await fs.readFile(refcacheFile, 'utf8');
const entries = JSON.parse(json);

// Create a sorted array of URL keys and `LastSeen` dates
const sortedUrlsAndDates = Object.keys(entries)
.map((url) => [url, entries[url].LastSeen])
.sort((a, b) => new Date(a[1]) - new Date(b[1]));

// Get oldest argv.num keys
const oldestKeys = sortedUrlsAndDates.slice(0, n).map((item) => item[0]);

// Remove oldest entries
oldestKeys.forEach((key) => delete entries[key]);

const prettyJson = JSON.stringify(entries, null, 2) + '\n';
await fs.writeFile(refcacheFile, prettyJson, 'utf8');
} catch (err) {
console.error(err);
}
}

pruneTask.description = `Prune the oldest '--num <n>' entries from ${refcacheFile} file (default ${n_default}).`;

gulp.task('prune', pruneTask);
3 changes: 3 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const requireDir = require('require-dir');

requireDir('./gulp-src');
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@
"devDependencies": {
"autoprefixer": "^10.4.14",
"cspell": "^6.31.1",
"gulp": "^4.0.2",
"hugo-extended": "0.113.0",
"netlify-cli": "^15.0.1",
"npm-run-all": "^4.1.5",
"postcss-cli": "^10.1.0",
"prettier": "^2.8.4",
"require-dir": "^1.2.0",
"textlint": "^13.1.4",
"textlint-filter-rule-allowlist": "^4.0.0",
"textlint-filter-rule-comments": "^1.2.2",
Expand Down
Loading

0 comments on commit 5556f79

Please sign in to comment.