unpkg/scripts/purge-cache.js

73 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-02-02 01:54:21 +00:00
const chalk = require('chalk');
2019-07-09 23:38:32 +00:00
const { getZone, purgeFiles } = require('./utils/cloudflare.js');
const { getFiles } = require('./utils/unpkg.js');
2019-02-02 01:54:21 +00:00
function groupBy(array, n) {
const groups = [];
2019-07-09 23:38:32 +00:00
while (array.length) groups.push(array.splice(0, n));
2019-02-02 01:54:21 +00:00
return groups;
}
2020-01-21 15:48:25 +00:00
async function purgeCache(packageName, version) {
2019-07-09 23:38:32 +00:00
if (packageName == null) {
console.error(
chalk.red(
'Missing <package-name>; use `node purge-cache.js <package-name> <version>`'
2019-02-02 01:54:21 +00:00
)
);
2019-07-09 23:38:32 +00:00
return 1;
}
if (version == null) {
console.error(
chalk.red(
`Missing <version>; use 'node purge-cache.js "${packageName}" <version>'`
)
);
2019-07-09 23:38:32 +00:00
return 1;
}
const files = await getFiles(packageName, version);
console.log(
chalk.yellow(
`Found ${files.length} files for package ${packageName}@${version}`
)
);
2019-07-09 23:38:32 +00:00
let urls = files.map(
file => `https://unpkg.com/${packageName}@${version}${file.path}`
);
if (version === 'latest') {
// Purge the URL w/out the "@latest" too.
urls = urls.concat(
files.map(file => `https://unpkg.com/${packageName}${file.path}`)
);
}
2019-02-02 01:54:21 +00:00
2019-07-09 23:38:32 +00:00
return getZone('unpkg.com').then(zone => {
let promise = Promise.resolve();
2019-02-02 01:54:21 +00:00
2019-07-09 23:38:32 +00:00
groupBy(urls, 30).forEach(group => {
promise = promise.then(() =>
purgeFiles(zone.id, group).then(() => {
group.forEach(url => console.log(chalk.green(`Purged ${url}`)));
})
);
2019-02-02 01:54:21 +00:00
});
2019-07-09 23:38:32 +00:00
return promise;
});
}
const packageName = process.argv[2];
const version = process.argv[3];
2020-01-21 15:48:25 +00:00
purgeCache(packageName, version).then(exitCode => {
2019-07-09 23:38:32 +00:00
process.exit(exitCode);
});