|
| 1 | +#!/usr/bin/env ts-node |
| 2 | + |
| 3 | +import { writeClientPackageXml, ClientPackageXML } from "../src/package-xml-v2"; |
| 4 | +import { getWidgetInfo } from "../src/package-info"; |
| 5 | +import { readPropertiesFile } from "../src/package-xml-v2/properties-xml"; |
| 6 | +import path from "node:path"; |
| 7 | +import { existsSync } from "node:fs"; |
| 8 | + |
| 9 | +async function generatePackageXml(): Promise<void> { |
| 10 | + const widgetDir = process.cwd(); |
| 11 | + |
| 12 | + // Read package.json info |
| 13 | + const packageInfo = await getWidgetInfo(widgetDir); |
| 14 | + |
| 15 | + const srcDir = path.join(widgetDir, "src"); |
| 16 | + |
| 17 | + // Create src directory if it doesn't exist |
| 18 | + if (!existsSync(srcDir)) { |
| 19 | + throw new Error(`Src folder not found: ${srcDir}`); |
| 20 | + } |
| 21 | + |
| 22 | + // Get properties file name from package.json (mxpackage.name + ".xml") |
| 23 | + const propertiesFileName = packageInfo.mxpackage.name + ".xml"; |
| 24 | + const propertiesFilePath = path.join(srcDir, propertiesFileName); |
| 25 | + |
| 26 | + // Properties file must exist |
| 27 | + if (!existsSync(propertiesFilePath)) { |
| 28 | + throw new Error(`Properties file not found: ${propertiesFilePath}`); |
| 29 | + } |
| 30 | + |
| 31 | + // Read properties file and extract widget ID |
| 32 | + const propertiesXml = await readPropertiesFile(propertiesFilePath); |
| 33 | + const widgetId = propertiesXml.widget["@_id"]; |
| 34 | + |
| 35 | + // Generate ClientPackageXML structure |
| 36 | + const clientPackageXml: ClientPackageXML = { |
| 37 | + name: packageInfo.mxpackage.name, |
| 38 | + version: packageInfo.version, |
| 39 | + widgetFiles: [packageInfo.mxpackage.name + ".xml"], |
| 40 | + files: [widgetId.split(".").slice(0, -1).join("/") + "/"] |
| 41 | + }; |
| 42 | + |
| 43 | + // Write the generated package.xml |
| 44 | + const packageXmlPath = path.join(srcDir, "package.xml"); |
| 45 | + await writeClientPackageXml(packageXmlPath, clientPackageXml); |
| 46 | +} |
| 47 | + |
| 48 | +async function main() { |
| 49 | + try { |
| 50 | + await generatePackageXml(); |
| 51 | + } catch (error) { |
| 52 | + console.error("Error generating package.xml:", error instanceof Error ? error.message : String(error)); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +if (require.main === module) { |
| 58 | + main(); |
| 59 | +} |
0 commit comments