@rxap/schematics-xml-parser
Version:
This package provides utilities for parsing XML templates within Angular Schematics. It includes functionality to locate templates in a file system, parse them using a specified DOMParser, and register custom elements for parsing. The package also offers
115 lines • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FindTemplate = FindTemplate;
exports.ParseTemplate = ParseTemplate;
const schematics_utilities_1 = require("@rxap/schematics-utilities");
const xml_parser_1 = require("@rxap/xml-parser");
const path_1 = require("path");
function FindTemplate(template, host, basePath, baseDirEntry = host.getDir('templates')) {
if (basePath) {
const path = (0, path_1.join)(baseDirEntry.path, basePath, template);
if (host.exists(path)) {
return path;
}
else {
console.warn(`Could not find template path with a provided basePath: ${path}`);
}
}
else {
const path = (0, path_1.join)(baseDirEntry.path, template);
if (host.exists(path)) {
return path;
}
else {
console.warn(`Could not find template path without a basePath: ${path}`);
}
}
{
const path = (0, path_1.join)(baseDirEntry.path, 'shared', template);
if (host.exists(path)) {
return path;
}
else {
console.warn(`Could not find template path in the shared folder: ${path}`);
}
}
return null;
}
/**
* Parse the template and returns the ParsedElement object
*
* The template parameter can be an xml document or a path to a xml document.
*
* The xml document must be in the templates directory.
*
* It is possible to provide a relative template path.
* If the basePath property is not set to undefined then a search in the sub directory is
* started.
*
* Examples
*
* Example 1
*
* template = 'views/tables/product.xml'
* basePath = undefined
*
* The following path are checked in order:
* - templates/views/tables/product.xml
* - templates/shared/views/tables/product.xml
*
* Example 2
*
* template = 'views/tables/product.xml'
* basePath = 'feature/product'
*
* The following path are checked in order:
* - templates/feature/product/views/tables/product.xml
* - templates/shared/views/tables/product.xml
*
* @param DOMParser
* @param host a schematic Tree instance
* @param template the path to the template xml document or a xml document
* @param basePath the basePath for the search
* @param elements a collection of ParsedElement class constructors that should be include in the xml parsing
*/
function ParseTemplate(DOMParser, host, template, basePath, ...elements) {
let templateFile;
let filename = '__inline__';
let templateFilePath = template;
const basePathList = (0, schematics_utilities_1.coerceArray)(basePath);
if (template.match(/\.xml$/)) {
if (!host.exists(template)) {
for (const bp of basePathList) {
templateFilePath = FindTemplate(template, host, bp);
if (templateFilePath) {
break;
}
}
}
if (!templateFilePath) {
throw new Error(`Could not find template file for '${template}'`);
}
else {
console.log(`Find template file path '${templateFilePath}' for '${template}'`);
}
const filenameMatch = templateFilePath.match(/\/([^/]+)\.xml$/);
if (filenameMatch && filenameMatch[1]) {
filename = filenameMatch[1];
}
const templateFileBuffer = host.read(templateFilePath);
if (!templateFileBuffer) {
throw new Error(`Could not read the file at path '${templateFilePath}'`);
}
templateFile = templateFileBuffer.toString('utf-8');
}
else {
templateFile = template;
}
const parser = new xml_parser_1.XmlParserService(DOMParser);
parser.register(...elements);
if (!templateFile) {
throw new Error('The template for the xml parser is not defined');
}
return parser.parseFromXml(templateFile, filename, templateFilePath);
}
//# sourceMappingURL=parse-template.js.map