#!/usr/bin/env node import { config } from "../public-config.js"; import { deleteApp, deleteSp, getApp, getGraphClient, getServicePrincipal, } from "../src/graph.js"; import { parseArgs } from "node:util"; async function usage() { console.log("Usage: delete-app-and-sp.mjs --app-name "); } async function main() { const { client } = await getGraphClient({ tenantId: config.tenantId, clientId: config.clientId, }); const args = parseArgs({ options: { "app-name": { type: "string", short: "n", }, }, }); if (!args.values["app-name"]) { await usage(); return; } console.log("Will delete app with name:", args.values["app-name"]); const app = await getApp(client, args.values["app-name"]); if (!app) { console.log("No app found with name:", args.values["app-name"]); return; } const sp = await getServicePrincipal(client, app.appId); if (sp && sp.id) { await deleteSp(client, sp.id); console.log("Deleted service principal:", sp.id); } else { console.log("No service principal found for appId:", app.appId); } if (app && app.id) { await deleteApp(client, app.id); console.log("Deleted app:", app.appId); } else { console.log("App object id missing; cannot delete application"); } } await main().catch((e) => { console.error("Error in main:", e); console.error(e.stack); process.exit(1); });