import { Glob } from "bun"; import babelParser from "@babel/parser"; let dir = "/Users/caleb.webber/git/snag/Web.MarketplaceAdmin/"; let glob = new Glob("!{node_modules}/**/*.ts"); const files = [...glob.scanSync(dir)].filter((f) => !f.includes("e2e")); export function parse(text: string) { return babelParser.parse(text, { allowImportExportEverywhere: true, plugins: ["decorators-legacy", "typescript"], }); } export async function fileContainsNgNeat(file: string) { const text = await Bun.file(`${dir}${file}`).text(); return text.includes("ngneat"); } export function getImportFrom(tree: any, module: string) { return tree.program.body.find( (n: any) => n.type === "ImportDeclaration" && n.source.value === module, ); } export function ensureAngularCoreImports(tree: any) { const t2 = structuredClone(tree); const angularCoreImport = getImportFrom(t2, "@angular/core") ?? { type: "ImportDeclaration", specifiers: [], source: { type: "StringLiteral", value: "@angular/core" }, }; angularCoreImport.specifiers.push({ type: "ImportSpecifier", imported: { type: "Identifier", name: "inject" }, local: { type: "Identifier", name: "inject" }, }); angularCoreImport.specifiers.push({ type: "ImportSpecifier", imported: { type: "Identifier", name: "takeUntilDestroyed" }, local: { type: "Identifier", name: "takeUntilDestroyed" }, }); return t2; } export function removeNgNeatImports(tree: any) { const t2 = structuredClone(tree); t2.program.body = t2.program.body.filter( (n: any) => n.type !== "ImportDeclaration" || !n.source.value.includes("ngneat"), ); return t2; } const filesToFix = ( await Promise.all(files.map(async (f) => [await fileContainsNgNeat(f), f])) ) .filter(([shouldFix]) => shouldFix) .map(([, f]) => f); export const trees = await Promise.all( filesToFix.map(async (f) => { const text = await Bun.file(`${dir}${f}`).text(); return parse(text); }), ); for (const f of filesToFix) { const text = await Bun.file(`${dir}${f}`).text(); try { const parsed = parse(text); console.log(`could parse ${f} - ${parsed.type === "File"}`); } catch (e) { console.error(`could not parse ${f}`); console.error(e); } } console.log(`Found ${filesToFix.length} files to fix`);