|
| 1 | +import { fetchComponentDataForManifest } from "."; |
| 2 | +import { createFlagHelpText } from "../utils/createFlagHelpText"; |
| 3 | +import { getFlagsBooleanValue } from "../utils/getFlagBooleanValue"; |
| 4 | +import { mkdirSync } from "node:fs"; |
| 5 | +import { createActions } from "../componentManifest/createActions"; |
| 6 | +import { createTriggers } from "../componentManifest/createTriggers"; |
| 7 | +import { createConnections } from "../componentManifest/createConnections"; |
| 8 | +import { createDataSources } from "../componentManifest/createDataSources"; |
| 9 | +import path from "node:path"; |
| 10 | +import { removeComponentManifest } from "../componentManifest/removeComponentManifest"; |
| 11 | +import { createTemplate } from "../utils/createTemplate"; |
| 12 | + |
| 13 | +export const runMain = async (process: NodeJS.Process) => { |
| 14 | + const args = process.argv.slice(2); |
| 15 | + const componentKey = args[0]; |
| 16 | + const cniDir = process.cwd(); |
| 17 | + |
| 18 | + const flags = { |
| 19 | + isPrivate: { |
| 20 | + flag: ["--private"], |
| 21 | + value: getFlagsBooleanValue({ |
| 22 | + args, |
| 23 | + flags: ["--private"], |
| 24 | + }), |
| 25 | + description: |
| 26 | + "Denotes if the component is private. If not specified, the component is assumed to be public.", |
| 27 | + }, |
| 28 | + dryRun: { |
| 29 | + flag: ["--dry-run", "-d"], |
| 30 | + value: getFlagsBooleanValue({ |
| 31 | + args, |
| 32 | + flags: ["--dry-run", "-d"], |
| 33 | + }), |
| 34 | + description: |
| 35 | + "Perform a dry run without generating the component manifest. This provides a preview of what you could expect to happen when running the command without this flag.", |
| 36 | + }, |
| 37 | + verbose: { |
| 38 | + flag: ["--verbose", "-v"], |
| 39 | + value: getFlagsBooleanValue({ |
| 40 | + args, |
| 41 | + flags: ["--verbose", "-v"], |
| 42 | + }), |
| 43 | + description: |
| 44 | + "Provides more detailed or extensive information about the files being generated during the process.", |
| 45 | + }, |
| 46 | + help: { |
| 47 | + flag: ["--help", "-h"], |
| 48 | + value: getFlagsBooleanValue({ |
| 49 | + args, |
| 50 | + flags: ["--help", "-h"], |
| 51 | + }), |
| 52 | + description: "Show this help message.", |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + if (flags.help.value || !componentKey) { |
| 57 | + createFlagHelpText({ |
| 58 | + command: "cni-component-manifest COMPONENT_KEY", |
| 59 | + flags, |
| 60 | + }); |
| 61 | + if (!componentKey) { |
| 62 | + console.error("\nError: COMPONENT_KEY is required."); |
| 63 | + } |
| 64 | + process.exit(!componentKey ? 1 : 0); |
| 65 | + } |
| 66 | + |
| 67 | + if (!cniDir) { |
| 68 | + console.error("Please run this script using npm or yarn."); |
| 69 | + process.exit(1); |
| 70 | + } |
| 71 | + |
| 72 | + // Fetch component data from API |
| 73 | + const component = await fetchComponentDataForManifest({ |
| 74 | + componentKey, |
| 75 | + isPrivate: flags.isPrivate.value || false, |
| 76 | + }); |
| 77 | + |
| 78 | + // Generate the manifest |
| 79 | + const destinationDir = path.join(process.cwd(), "src", "manifests", component.key); |
| 80 | + const templatesDir = path.join(__dirname, "..", "componentManifest", "templates"); |
| 81 | + const dryRun = flags.dryRun.value; |
| 82 | + const verbose = flags.verbose.value; |
| 83 | + |
| 84 | + if (verbose) { |
| 85 | + console.info(`Creating component manifest for ${component.display.label}...`); |
| 86 | + } |
| 87 | + |
| 88 | + removeComponentManifest({ destinationDir, verbose }); |
| 89 | + |
| 90 | + // Create the directory structure |
| 91 | + mkdirSync(destinationDir, { recursive: true }); |
| 92 | + |
| 93 | + // Generate the source files |
| 94 | + await await createTemplate({ |
| 95 | + source: path.join(templatesDir, "index.ts.ejs"), |
| 96 | + destination: path.join(destinationDir, "index.ts"), |
| 97 | + data: { |
| 98 | + component, |
| 99 | + }, |
| 100 | + verbose, |
| 101 | + dryRun, |
| 102 | + }); |
| 103 | + |
| 104 | + await createActions({ |
| 105 | + component, |
| 106 | + dryRun, |
| 107 | + verbose, |
| 108 | + sourceDir: templatesDir, |
| 109 | + destinationDir, |
| 110 | + }); |
| 111 | + |
| 112 | + await createTriggers({ |
| 113 | + component, |
| 114 | + dryRun, |
| 115 | + verbose, |
| 116 | + sourceDir: templatesDir, |
| 117 | + destinationDir, |
| 118 | + }); |
| 119 | + |
| 120 | + await createConnections({ |
| 121 | + component, |
| 122 | + dryRun, |
| 123 | + verbose, |
| 124 | + sourceDir: templatesDir, |
| 125 | + destinationDir, |
| 126 | + }); |
| 127 | + |
| 128 | + await createDataSources({ |
| 129 | + component, |
| 130 | + dryRun, |
| 131 | + verbose: flags.verbose.value, |
| 132 | + sourceDir: templatesDir, |
| 133 | + destinationDir, |
| 134 | + }); |
| 135 | + |
| 136 | + console.info( |
| 137 | + `Component manifest created successfully for ${component.display.label} in ${destinationDir}!`, |
| 138 | + ); |
| 139 | +}; |
0 commit comments