1"use strict"; 2 3Object.defineProperty(exports, "__esModule", { 4 value: true 5}); 6exports.buildResourceGroup = buildResourceGroup; 7exports.buildResourceItem = buildResourceItem; 8exports.ensureDefaultResourceXML = ensureDefaultResourceXML; 9exports.findResourceGroup = findResourceGroup; 10exports.getObjectAsResourceGroup = getObjectAsResourceGroup; 11exports.getObjectAsResourceItems = getObjectAsResourceItems; 12exports.getResourceItemsAsObject = getResourceItemsAsObject; 13exports.readResourcesXMLAsync = readResourcesXMLAsync; 14function _XML() { 15 const data = require("../utils/XML"); 16 _XML = function () { 17 return data; 18 }; 19 return data; 20} 21const fallbackResourceString = `<?xml version="1.0" encoding="utf-8"?><resources></resources>`; 22 23/** 24 * Read an XML file while providing a default fallback for resource files. 25 * 26 * @param options path to the XML file, returns a fallback XML if the path doesn't exist. 27 */ 28async function readResourcesXMLAsync({ 29 path, 30 fallback = fallbackResourceString 31}) { 32 const xml = await (0, _XML().readXMLAsync)({ 33 path, 34 fallback 35 }); 36 // Ensure the type is expected. 37 if (!xml.resources) { 38 xml.resources = {}; 39 } 40 return xml; 41} 42 43/** 44 * Ensure the provided xml has a `resources` object (the expected shape). 45 * 46 * @param xml 47 */ 48function ensureDefaultResourceXML(xml) { 49 if (!xml) { 50 xml = { 51 resources: {} 52 }; 53 } 54 if (!xml.resources) { 55 xml.resources = {}; 56 } 57 return xml; 58} 59 60/** 61 * Build a `ResourceItemXML` given its `name` and `value`. This makes things a bit more readable. 62 * 63 * - JSON: `{ $: { name }, _: value }` 64 * - XML: `<item name="NAME">VALUE</item>` 65 * 66 * @param props name and value strings. 67 */ 68function buildResourceItem({ 69 name, 70 value, 71 targetApi, 72 translatable 73}) { 74 const item = { 75 $: { 76 name 77 }, 78 _: value 79 }; 80 if (targetApi) { 81 item.$['tools:targetApi'] = targetApi; 82 } 83 if (translatable !== undefined) { 84 item.$['translatable'] = String(translatable); 85 } 86 return item; 87} 88function buildResourceGroup(parent) { 89 var _parent$items; 90 return { 91 $: { 92 name: parent.name, 93 parent: parent.parent 94 }, 95 item: (_parent$items = parent.items) !== null && _parent$items !== void 0 ? _parent$items : [] 96 }; 97} 98function findResourceGroup(xml, group) { 99 var _xml$filter, _xml$filter$call; 100 const app = xml === null || xml === void 0 ? void 0 : (_xml$filter = xml.filter) === null || _xml$filter === void 0 ? void 0 : (_xml$filter$call = _xml$filter.call(xml, ({ 101 $: head 102 }) => { 103 let matches = head.name === group.name; 104 if (group.parent != null && matches) { 105 matches = head.parent === group.parent; 106 } 107 return matches; 108 })) === null || _xml$filter$call === void 0 ? void 0 : _xml$filter$call[0]; 109 return app !== null && app !== void 0 ? app : null; 110} 111 112/** 113 * Helper to convert a basic XML object into a simple k/v pair. 114 * 115 * @param xml 116 * @returns 117 */ 118function getResourceItemsAsObject(xml) { 119 return xml.reduce((prev, curr) => ({ 120 ...prev, 121 [curr.$.name]: curr._ 122 }), {}); 123} 124 125/** 126 * Helper to convert a basic k/v object to a ResourceItemXML array. 127 * 128 * @param xml 129 * @returns 130 */ 131function getObjectAsResourceItems(obj) { 132 return Object.entries(obj).map(([name, value]) => ({ 133 $: { 134 name 135 }, 136 _: value 137 })); 138} 139function getObjectAsResourceGroup(group) { 140 return { 141 $: { 142 name: group.name, 143 parent: group.parent 144 }, 145 item: getObjectAsResourceItems(group.item) 146 }; 147} 148//# sourceMappingURL=Resources.js.map