Update scripts

This commit is contained in:
Michael Jackson 2019-07-09 16:38:32 -07:00
parent b67e58d985
commit 49c55af59d
4 changed files with 81 additions and 103 deletions

View File

@ -1,23 +0,0 @@
const AuthAPI = require('../server/AuthAPI');
const scopes = {
blacklist: {
read: true
}
};
AuthAPI.createToken(scopes).then(
token => {
// Verify it, just to be sure.
AuthAPI.verifyToken(token).then(payload => {
console.log(token, '\n');
console.log(JSON.stringify(payload, null, 2), '\n');
console.log(AuthAPI.getPublicKey());
process.exit();
});
},
error => {
console.error(error);
process.exit(1);
}
);

View File

@ -1,65 +1,72 @@
const chalk = require('chalk');
const { getZone, purgeFiles } = require('./utils/cloudflare');
const { die } = require('./utils/process');
const { getFiles } = require('./utils/unpkg');
const { getZone, purgeFiles } = require('./utils/cloudflare.js');
const { getFiles } = require('./utils/unpkg.js');
function groupBy(array, n) {
const groups = [];
while (array.length) groups.push(array.splice(0, n));
return groups;
}
async function run(packageName, version) {
if (packageName == null) {
console.error(
chalk.red(
'Missing <package-name>; use `node purge-cache.js <package-name> <version>`'
)
);
return 1;
}
if (version == null) {
console.error(
chalk.red(
`Missing <version>; use 'node purge-cache.js "${packageName}" <version>'`
)
);
return 1;
}
const files = await getFiles(packageName, version);
console.log(
chalk.yellow(
`Found ${files.length} files for package ${packageName}@${version}`
)
);
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}`)
);
}
return getZone('unpkg.com').then(zone => {
let promise = Promise.resolve();
groupBy(urls, 30).forEach(group => {
promise = promise.then(() =>
purgeFiles(zone.id, group).then(() => {
group.forEach(url => console.log(chalk.green(`Purged ${url}`)));
})
);
});
return promise;
});
}
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}`
)
);
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}`)
);
}
return getZone('unpkg.com').then(zone => {
let promise = Promise.resolve();
groupBy(urls, 30).forEach(group => {
promise = promise.then(() => {
return purgeFiles(zone.id, group).then(data => {
group.forEach(url => console.log(chalk.green(`Purged ${url}`)));
});
});
});
return promise;
});
})
.catch(die);
run(packageName, version).then(exitCode => {
process.exit(exitCode);
});

View File

@ -1,14 +1,20 @@
const { getZones, getLog } = require('./utils/cloudflare');
const { die } = require('./utils/process');
const { getZone, getLog } = require('./utils/cloudflare.js');
const RayId = process.argv[2];
async function run(rayId) {
if (rayId == null) {
console.error('Missing the RAY_ID argument; use `node show-log.js RAY_ID`');
return 1;
}
if (RayId == null) {
die('Missing the RAY_ID argument; use `node show-log.js RAY_ID`');
const zone = await getZone('unpkg.com');
const entry = await getLog(zone.id, rayId);
console.log(entry || 'NOT FOUND');
return 0;
}
getZone('unpkg.com').then(zone => {
getLog(zone.id, RayId).then(entry => {
console.log(entry || 'NOT FOUND');
});
const rayId = process.argv[2];
run(rayId).then(exitCode => {
process.exit(exitCode);
});

View File

@ -1,12 +0,0 @@
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
};