Update scripts
This commit is contained in:
60
scripts/purge-cache.js
Normal file
60
scripts/purge-cache.js
Normal file
@ -0,0 +1,60 @@
|
||||
const chalk = require('chalk');
|
||||
|
||||
const { getZone, purgeFiles } = require('./utils/cloudflare');
|
||||
const { die } = require('./utils/process');
|
||||
const { getFiles } = require('./utils/unpkg');
|
||||
|
||||
const packageName = process.argv[2];
|
||||
const version = process.argv[3];
|
||||
|
||||
if (packageName == null) {
|
||||
die(
|
||||
'Missing the PACKAGE_NAME argument; use `node purge-cache.js PACKAGE_NAME VERSION`'
|
||||
);
|
||||
}
|
||||
|
||||
if (version == null) {
|
||||
die(
|
||||
'Missing the VERSION argument; use `node purge-cache.js PACKAGE_NAME VERSION`'
|
||||
);
|
||||
}
|
||||
|
||||
function groupBy(array, n) {
|
||||
const groups = [];
|
||||
|
||||
while (array.length) {
|
||||
groups.push(array.splice(0, n));
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
getFiles(packageName, version)
|
||||
.then(files => {
|
||||
console.log(
|
||||
chalk.yellow(
|
||||
`Found ${files.length} files for package ${packageName}@${version}`
|
||||
)
|
||||
);
|
||||
|
||||
return getZone('unpkg.com').then(zone => {
|
||||
let promise = Promise.resolve();
|
||||
|
||||
groupBy(files, 30).forEach(group => {
|
||||
promise = promise.then(() => {
|
||||
const urls = group.map(
|
||||
file => `https://unpkg.com/${packageName}@${version}${file.path}`
|
||||
);
|
||||
|
||||
return purgeFiles(zone.id, urls).then(data => {
|
||||
group.forEach(file =>
|
||||
console.log(chalk.green(`Purged ${file.path}`))
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
});
|
||||
})
|
||||
.catch(die);
|
@ -1,49 +1,14 @@
|
||||
require("isomorphic-fetch");
|
||||
const invariant = require("invariant");
|
||||
const { getZones, getLog } = require('./utils/cloudflare');
|
||||
const { die } = require('./utils/process');
|
||||
|
||||
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL;
|
||||
const CloudflareKey = process.env.CLOUDFLARE_KEY;
|
||||
const RayID = process.argv[2];
|
||||
const RayId = process.argv[2];
|
||||
|
||||
invariant(
|
||||
CloudflareEmail,
|
||||
"Missing the $CLOUDFLARE_EMAIL environment variable"
|
||||
);
|
||||
|
||||
invariant(CloudflareKey, "Missing the $CLOUDFLARE_KEY environment variable");
|
||||
|
||||
invariant(
|
||||
RayID,
|
||||
"Missing the RAY_ID argument; use `heroku run node show-log.js RAY_ID`"
|
||||
);
|
||||
|
||||
function getZones(domain) {
|
||||
return fetch(`https://api.cloudflare.com/client/v4/zones?name=${domain}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-Auth-Email": CloudflareEmail,
|
||||
"X-Auth-Key": CloudflareKey
|
||||
}
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => data.result);
|
||||
if (RayId == null) {
|
||||
die('Missing the RAY_ID argument; use `node show-log.js RAY_ID`');
|
||||
}
|
||||
|
||||
function getLog(zoneId, rayId) {
|
||||
return fetch(
|
||||
`https://api.cloudflare.com/client/v4/zones/${zoneId}/logs/requests/${rayId}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"X-Auth-Email": CloudflareEmail,
|
||||
"X-Auth-Key": CloudflareKey
|
||||
}
|
||||
}
|
||||
).then(res => (res.status === 404 ? "NOT FOUND" : res.json()));
|
||||
}
|
||||
|
||||
getZones("unpkg.com").then(zones => {
|
||||
getLog(zones[0].id, RayID).then(entry => {
|
||||
console.log(entry);
|
||||
getZone('unpkg.com').then(zone => {
|
||||
getLog(zone.id, RayId).then(entry => {
|
||||
console.log(entry || 'NOT FOUND');
|
||||
});
|
||||
});
|
||||
|
90
scripts/utils/cloudflare.js
Normal file
90
scripts/utils/cloudflare.js
Normal file
@ -0,0 +1,90 @@
|
||||
const fetch = require('isomorphic-fetch');
|
||||
|
||||
const CloudflareEmail = process.env.CLOUDFLARE_EMAIL;
|
||||
const CloudflareKey = process.env.CLOUDFLARE_KEY;
|
||||
|
||||
if (CloudflareEmail == null) {
|
||||
console.error('Missing the $CLOUDFLARE_EMAIL environment variable');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (CloudflareKey == null) {
|
||||
console.error('Missing the $CLOUDFLARE_KEY environment variable');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function get(path) {
|
||||
return fetch(`https://api.cloudflare.com/client/v4${path}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Auth-Email': CloudflareEmail,
|
||||
'X-Auth-Key': CloudflareKey
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getLog(zoneId, rayId) {
|
||||
return get(`/zones/${zoneId}/logs/requests/${rayId}`).then(
|
||||
res => (res.status === 404 ? null : res.json())
|
||||
);
|
||||
}
|
||||
|
||||
function getZone(domain) {
|
||||
return get(`/zones?name=${domain}`)
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (!data.success) throw data;
|
||||
|
||||
const zones = data.result;
|
||||
|
||||
if (zones.length > 1) {
|
||||
console.error(
|
||||
`Domain "${domain}" has more than one zone: ${zones.join(', ')}`
|
||||
);
|
||||
}
|
||||
|
||||
return zones[0];
|
||||
});
|
||||
}
|
||||
|
||||
function post(path, data) {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Auth-Email': CloudflareEmail,
|
||||
'X-Auth-Key': CloudflareKey
|
||||
}
|
||||
};
|
||||
|
||||
if (data) {
|
||||
options.headers['Content-Type'] = 'application/json';
|
||||
options.body = JSON.stringify(data);
|
||||
}
|
||||
|
||||
return fetch(`https://api.cloudflare.com/client/v4${path}`, options);
|
||||
}
|
||||
|
||||
function purgeFiles(zoneId, files) {
|
||||
return post(`/zones/${zoneId}/purge_cache`, { files })
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.success) return data;
|
||||
throw data;
|
||||
});
|
||||
}
|
||||
|
||||
function purgeTags(zoneId, tags) {
|
||||
return post(`/zones/${zoneId}/purge_cache`, { tags })
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.success) return data;
|
||||
throw data;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getLog,
|
||||
getZone,
|
||||
purgeFiles,
|
||||
purgeTags
|
||||
};
|
12
scripts/utils/process.js
Normal file
12
scripts/utils/process.js
Normal file
@ -0,0 +1,12 @@
|
||||
const util = require('util');
|
||||
const chalk = require('chalk');
|
||||
|
||||
function die(why) {
|
||||
const message = typeof why === 'string' ? why : util.inspect(why);
|
||||
console.error(chalk.red(message));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
die
|
||||
};
|
22
scripts/utils/unpkg.js
Normal file
22
scripts/utils/unpkg.js
Normal file
@ -0,0 +1,22 @@
|
||||
const fetch = require('isomorphic-fetch');
|
||||
|
||||
function getMetadata(packageName, version) {
|
||||
return fetch(`https://unpkg.com/${packageName}@${version}/?meta`, {
|
||||
method: 'GET'
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
function collectFiles(directory) {
|
||||
return directory.files.reduce((memo, file) => {
|
||||
return memo.concat(file.type === 'directory' ? collectFiles(file) : file);
|
||||
}, []);
|
||||
}
|
||||
|
||||
function getFiles(packageName, version) {
|
||||
return getMetadata(packageName, version).then(collectFiles);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getMetadata,
|
||||
getFiles
|
||||
};
|
Reference in New Issue
Block a user