Update scripts
This commit is contained in:
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