UNPKG

sf-package-list

Version:

Convert Salesforce package.xml manifests to and from plain-text list format.

76 lines 2.4 kB
import { readFile, writeFile } from 'node:fs/promises'; import { buildXml } from '../utils/xmlBuilder.js'; export async function listToPackageXml({ listPath, xmlPath, noApiVersion, }) { const warnings = []; let xmlString; let listString; if (listPath) { try { listString = await readFile(listPath, 'utf-8'); } catch { warnings.push(`List file "${listPath}" could not be read. Using empty package.xml.`); } } else { warnings.push('No list file provided. Using empty package.xml.'); } if (listString) { const lines = listString.split('\n'); const packageJson = parseListLines(lines, noApiVersion, warnings); xmlString = buildXmlString(packageJson); } else { xmlString = generateEmptyPackageXml(); } await writeFile(xmlPath, xmlString); return { xmlPath, warnings }; } function buildXmlString(packageJson) { return '<?xml version="1.0" encoding="UTF-8"?>\n' + buildXml(packageJson); } function generateEmptyPackageXml() { const emptyPackage = { Package: { '@_xmlns': 'http://soap.sforce.com/2006/04/metadata', types: [], version: undefined, }, }; return buildXmlString(emptyPackage); } function parseListLines(lines, noApiVersion, warnings) { const packageJson = { Package: { '@_xmlns': 'http://soap.sforce.com/2006/04/metadata', types: [], version: undefined, }, }; for (const line of lines) { const trimmedLine = line.trim(); if (!trimmedLine) continue; const parts = trimmedLine.split(':'); if (parts.length < 2) { warnings.push(`Line does not match expected package list format and will be skipped: ${line}`); continue; } const [key, values] = parts.map((s) => s.trim()); if (key.toLowerCase() === 'version') { if (!noApiVersion) { packageJson.Package.version = values; } continue; } packageJson.Package.types.push({ members: values .split(',') .map((s) => s.trim()) .filter(Boolean), name: key, }); } return packageJson; } //# sourceMappingURL=listToPackageXml.js.map