-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CI: gulp task in support of refcache cleanup & some refcache updates (#…
- Loading branch information
Showing
7 changed files
with
441 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const requireDir = require('require-dir'); | ||
|
||
requireDir('./gulp-src'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.