|
| 1 | +import fs from "fs/promises"; |
| 2 | +import path from "path"; |
| 3 | +import { parse } from "@babel/parser"; |
| 4 | +import * as recast from "recast"; |
| 5 | +import { namedTypes as n, builders as b } from "ast-types"; |
| 6 | + |
| 7 | +const parser = { |
| 8 | + parse(source) { |
| 9 | + return parse(source, { |
| 10 | + sourceType: "module", |
| 11 | + plugins: ["typescript"], |
| 12 | + }); |
| 13 | + }, |
| 14 | +}; |
| 15 | + |
| 16 | +export async function injectResourceIntoIndex({ |
| 17 | + indexFilePath = path.resolve(process.cwd(), "index.ts"), |
| 18 | + table, |
| 19 | + resourceId, |
| 20 | + label, |
| 21 | + icon = "flowbite:user-solid", |
| 22 | +}) { |
| 23 | + let code = await fs.readFile(indexFilePath, "utf-8"); |
| 24 | + const ast = recast.parse(code, { parser }); |
| 25 | + |
| 26 | + const importLine = `import ${resourceId}Resource from "./resources/${table}";`; |
| 27 | + let alreadyImported = false; |
| 28 | + |
| 29 | + recast.visit(ast, { |
| 30 | + visitImportDeclaration(path) { |
| 31 | + const { node } = path; |
| 32 | + if ( |
| 33 | + n.ImportDeclaration.check(node) && |
| 34 | + node.source.value === `./resources/${table}` |
| 35 | + ) { |
| 36 | + alreadyImported = true; |
| 37 | + return false; |
| 38 | + } |
| 39 | + this.traverse(path); |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + if (alreadyImported) { |
| 44 | + console.warn(`⚠️ Resource already imported: ${table}`); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // Add import at top |
| 49 | + ast.program.body.unshift( |
| 50 | + b.importDeclaration( |
| 51 | + [b.importDefaultSpecifier(b.identifier(`${resourceId}Resource`))], |
| 52 | + b.stringLiteral(`./resources/${table}`) |
| 53 | + ) |
| 54 | + ); |
| 55 | + |
| 56 | + // Find config object with `resources` and `menu` |
| 57 | + recast.visit(ast, { |
| 58 | + visitObjectExpression(path) { |
| 59 | + const node = path.node; |
| 60 | + |
| 61 | + const resourcesProp = node.properties.find( |
| 62 | + (p) => |
| 63 | + n.ObjectProperty.check(p) && |
| 64 | + n.Identifier.check(p.key) && |
| 65 | + p.key.name === "resources" && |
| 66 | + n.ArrayExpression.check(p.value) |
| 67 | + ); |
| 68 | + |
| 69 | + if (resourcesProp) { |
| 70 | + const arr = resourcesProp.value.elements; |
| 71 | + const alreadyExists = arr.some( |
| 72 | + (el) => |
| 73 | + n.Identifier.check(el) && |
| 74 | + el.name === `${resourceId}Resource` |
| 75 | + ); |
| 76 | + if (!alreadyExists) { |
| 77 | + arr.push(b.identifier(`${resourceId}Resource`)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + const menuProp = node.properties.find( |
| 82 | + (p) => |
| 83 | + n.ObjectProperty.check(p) && |
| 84 | + n.Identifier.check(p.key) && |
| 85 | + p.key.name === "menu" && |
| 86 | + n.ArrayExpression.check(p.value) |
| 87 | + ); |
| 88 | + |
| 89 | + if (menuProp) { |
| 90 | + const newItem = b.objectExpression([ |
| 91 | + b.objectProperty(b.identifier("label"), b.stringLiteral(capitalizeWords(label))), |
| 92 | + b.objectProperty(b.identifier("icon"), b.stringLiteral(icon)), |
| 93 | + b.objectProperty(b.identifier("resourceId"), b.stringLiteral(resourceId)), |
| 94 | + ]); |
| 95 | + |
| 96 | + menuProp.value.elements.push(newItem); |
| 97 | + } |
| 98 | + |
| 99 | + return false; // Done |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + const newCode = recast.print(ast).code; |
| 104 | + await fs.writeFile(indexFilePath, newCode, "utf-8"); |
| 105 | + console.log(`✅ Injected resource "${resourceId}" into index`); |
| 106 | +} |
| 107 | + |
| 108 | +function capitalizeWords(str) { |
| 109 | + return str |
| 110 | + .split(" ") |
| 111 | + .map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()) |
| 112 | + .join(" "); |
| 113 | +} |
0 commit comments