import { basename, dirname } from "../deno_ral/path.ts";
import { removeIfEmptyDir } from "../core/path.ts";
import { kBuiltInExtOrg, kExtensionDir } from "./constants.ts";
import { Extension } from "./types.ts";
export async function removeExtension(extension: Extension) {
if (extension.id.organization === kBuiltInExtOrg) {
throw new Error(
`The extension ${extension.title} can't be removed since it is a built in extension.`,
);
}
await Deno.remove(extension.path, { recursive: true });
const extensionDir = dirname(extension.path);
removeIfEmptyDir(extensionDir);
const parentDir = dirname(extensionDir);
if (parentDir && basename(parentDir) === kExtensionDir) {
removeIfEmptyDir(parentDir);
}
}