+ "example": "/**\n * This plugin will create a file `smartTags.json` containing all of the smart\n * tags gathered from all of the various sources (smart comments, smart tags,\n * plugins, etc etc). This provides a relatively easy migration path from using\n * smart comments to using smart tags instead. Neither Smart Comments nor Smart\n * Tags are \"better\" - they each have trade offs - you can use which ever one\n * matches your teams development flow better (or even mix and match!).\n *\n * Author phryneas (https://github.com/graphile/graphile.github.io/pull/243)\n */\nconst { writeFile } = require(\"fs\");\n\nmodule.exports = builder => {\n builder.hook(\"init\", (_, build) => {\n function sortStuff(a, b) {\n const aSchema =\n \"namespaceName\" in a\n ? a.namespaceName\n : a.class.namespaceName;\n const bSchema =\n \"namespaceName\" in b\n ? b.namespaceName\n : b.class.namespaceName;\n return (\n aSchema.localeCompare(\n bSchema\n ) *\n 100 +\n a.name.localeCompare(b.name)\n );\n }\n const smart = {\n version: 1,\n config: {\n class: [\n ...build\n .pgIntrospectionResultsByKind\n .class,\n ]\n .sort(sortStuff)\n .reduce((acc, pgClass) => {\n let attribute = [\n ...pgClass.attributes,\n ]\n .sort((a, b) =>\n a.name.localeCompare(\n b.name\n )\n )\n .reduce(\n (acc, pgAttr) => {\n const tags =\n Object.keys(\n pgAttr.tags\n ).length > 0\n ? pgAttr.tags\n : undefined;\n if (\n pgAttr.description ||\n tags\n ) {\n acc[\n pgAttr.name\n ] = {\n ...(pgAttr.description\n ? {\n description:\n pgAttr.description,\n }\n : {}),\n tags,\n };\n }\n return acc;\n },\n {}\n );\n if (\n Object.keys(attribute)\n .length === 0\n ) {\n attribute = undefined;\n }\n let constraint = [\n ...pgClass.constraints,\n ]\n .sort(sortStuff)\n .reduce(\n (acc, pgConst) => {\n if (\n pgConst.name.startsWith(\n \"FAKE_\"\n )\n ) {\n return acc;\n }\n const tags =\n Object.keys(\n pgConst.tags\n ).length > 0\n ? pgConst.tags\n : undefined;\n if (\n pgConst.class &&\n (pgConst.description ||\n tags)\n ) {\n acc[\n pgConst.class\n .namespaceName +\n \".\" +\n pgConst.name\n ] = {\n ...(pgConst.description\n ? {\n description:\n pgConst.description,\n }\n : {}),\n tags,\n };\n }\n return acc;\n },\n {}\n );\n if (\n Object.keys(constraint)\n .length === 0\n ) {\n constraint = undefined;\n }\n const tags =\n Object.keys(\n pgClass.tags\n ).length > 0\n ? pgClass.tags\n : undefined;\n if (\n pgClass.description ||\n tags ||\n attribute\n )\n acc[\n pgClass.namespaceName +\n \".\" +\n pgClass.name\n ] = {\n ...(pgClass.description\n ? {\n description:\n pgClass.description,\n }\n : {}),\n tags,\n attribute,\n constraint,\n };\n return acc;\n }, {}),\n procedure: [\n ...build\n .pgIntrospectionResultsByKind\n .procedure,\n ]\n .sort(sortStuff)\n .reduce((acc, pgProc) => {\n if (\n pgProc.name.startsWith(\n \"FAKE_\"\n )\n ) {\n return acc;\n }\n const tags =\n Object.keys(pgProc.tags)\n .length > 0\n ? pgProc.tags\n : undefined;\n if (\n pgProc.description ||\n tags\n ) {\n acc[\n pgProc.namespaceName +\n \".\" +\n pgProc.name\n ] = {\n ...(pgProc.description\n ? {\n description:\n pgProc.description,\n }\n : {}),\n tags,\n };\n }\n return acc;\n }, {}),\n },\n };\n writeFile(\n __dirname + \"/smartTags.json\",\n JSON.stringify(\n smart,\n undefined,\n 2\n ),\n e => {\n console.log(e);\n }\n );\n return _;\n });\n};\n",
0 commit comments