@lwc-bolts/sf2ts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ /** @import { Component, ConvertedComponent } from "../index.mjs" */
2
+ import { parseString } from "xml2js";
3
+
4
+ /**
5
+ *
6
+ * @param {Component} component
7
+ * @returns {ConvertedComponent}
8
+ */
9
+ export function CustomObject(component) {
10
+ // generate d.ts
11
+ /** @type {any} */
12
+ let obj;
13
+ parseString(component.content, (error, result) => {
14
+ if (error) {
15
+ throw error;
16
+ }
17
+ obj = result;
18
+ });
19
+ const objectName = component.path.split("/").pop()?.split(".")[0];
20
+ const dec = `declare module '@salesforce/schema/${objectName}' {
21
+ /**
22
+ * @description ${obj.CustomObject.description}
23
+ */
24
+ const ${objectName}: {
25
+ objectApiName: '${objectName}';
26
+ }
27
+ export default ${objectName};
28
+ }`;
29
+ return { declarationContent: dec, mapData: [], ...component };
30
+ }
@@ -0,0 +1,30 @@
1
+ /** @import { Component, ConvertedComponent } from "../index.mjs" */
2
+ import { parseString } from "xml2js";
3
+
4
+ /**
5
+ *
6
+ * @param {Component} component
7
+ * @returns {ConvertedComponent}
8
+ */
9
+ export function CustomPermission(component) {
10
+ // generate d.ts
11
+ /** @type {any} */
12
+ let perm;
13
+ parseString(component.content, (error, result) => {
14
+ if (error) {
15
+ throw error;
16
+ }
17
+ perm = result;
18
+ });
19
+ const permName = component.path.split("/").pop()?.split(".")[0];
20
+ const dec = `declare module "@salesforce/customPermission/${permName}" {
21
+ /**
22
+ * ${perm.CustomPermission.label}
23
+ *
24
+ * @description ${perm.CustomPermission.description}
25
+ */
26
+ const has${permName}:boolean;
27
+ export default has${permName};
28
+ }`;
29
+ return { declarationContent: dec, mapData: [], ...component };
30
+ }
@@ -0,0 +1,29 @@
1
+ /** @import { Component, ConvertedComponent } from "../index.mjs" */
2
+ import { parseString } from "xml2js";
3
+
4
+ /**
5
+ *
6
+ * @param {Component} component
7
+ * @returns {ConvertedComponent}
8
+ */
9
+ export function StaticResource(component) {
10
+ // generate d.ts
11
+ /** @type {any} */
12
+ let resource;
13
+ parseString(component.content, (error, result) => {
14
+ if (error) {
15
+ throw error;
16
+ }
17
+ resource = result;
18
+ });
19
+ const resourceName = component.path.split("/").pop()?.split(".")[0];
20
+ const dec = `declare module "@salesforce/resourceUrl/${resourceName}" {
21
+ /**
22
+ * @description ${resource.StaticResource.description}
23
+ * @access ${resource.StaticResource.cacheControl}
24
+ */
25
+ const ${resourceName}:string;
26
+ export default ${resourceName};
27
+ }`;
28
+ return { declarationContent: dec, mapData: [], ...component };
29
+ }
package/lib/index.mjs ADDED
@@ -0,0 +1,51 @@
1
+ import * as converters from "./converts/_index.mjs";
2
+
3
+ // maybe there is a smarter way to do this
4
+ /** @type {Readonly<{[p in keyof converters]: p}>} */
5
+ export const MetadataType = Object.freeze(
6
+ Object.keys(converters).reduce((/** @type {any} */ t, v) => {
7
+ t[v] = v;
8
+ return t;
9
+ }, {})
10
+ );
11
+
12
+ /**
13
+ * @typedef MapPair
14
+ * @property {number} sourcePos
15
+ * @property {number} destPos
16
+ */
17
+
18
+ /**
19
+ * @typedef Component
20
+ * @property {string} path
21
+ * @property {keyof converters} type
22
+ * @property {string} content
23
+ */
24
+
25
+ /**
26
+ * @typedef { Component & {
27
+ * declarationContent: string;
28
+ * mapData: MapPair[]}} ConvertedComponent
29
+ */
30
+
31
+ /**
32
+ *
33
+ * @param {Component[]} components
34
+ * @returns {ConvertedComponent[]}
35
+ */
36
+ export function convert(components) {
37
+ /** @type {ConvertedComponent[]} */
38
+ const convertedComponents = [];
39
+ for (const c of components) {
40
+ if (!converters[c.type]) {
41
+ throw new Error(
42
+ "Unsupported Metadata Type:" +
43
+ c.type +
44
+ ", must be one of " +
45
+ Object.keys(converters)
46
+ );
47
+ }
48
+ convertedComponents.push(converters[c.type](c));
49
+ }
50
+ return convertedComponents;
51
+ }