cf-worker-prune-routes
Version:
Prune unused cloudflare worker routes
41 lines (33 loc) • 1.02 kB
JavaScript
import { execAsync } from './execAsync.js';
const { CF_API_TOKEN } = process.env;
export const wranglerListRoutes = async ({ config, env }) => {
let cachedOutput = '';
try {
if (!CF_API_TOKEN)
throw new Error(
'Missing Cloudflare API token environment variable CF_API_TOKEN, which is necessary for running wrangler commands'
);
const { stdout, stderr } = await execAsync(
`$(npm bin)/wrangler route list --config ${config} --env ${env}`
);
if (stderr) throw new Error(stderr);
cachedOutput = stdout;
return JSON.parse(stdout);
} catch (err) {
console.log(err);
console.log(cachedOutput);
process.exit(1);
}
};
export const wranglerPruneRoute = async ({ id, config, env }) => {
try {
const { stdout, stderr } = await execAsync(
`$(npm bin)/wrangler route delete ${id} --env ${env} --config ${config}`
);
if (stderr) throw new Error(stderr);
return stdout;
} catch (err) {
console.log(err);
process.exit(1);
}
};