@lwc-bolts/sf2ts 0.0.2 → 0.0.4

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,62 @@
1
+ /** @import { Component, ConvertedComponent } from "../index.mjs" */
2
+ import { parseString } from "xml2js";
3
+ import { PositionAwareTextBuilder } from "../util.mjs";
4
+
5
+ /**
6
+ *
7
+ * @param {Component} component
8
+ * @returns {ConvertedComponent}
9
+ */
10
+ export function CustomLabel(component) {
11
+ // generate d.ts
12
+ /** @type {any} */
13
+ let labels;
14
+ parseString(component.content, (error, result) => {
15
+ if (error) {
16
+ throw error;
17
+ }
18
+ labels = result;
19
+ });
20
+ console.log(labels.CustomLabel);
21
+ const builder = new PositionAwareTextBuilder();
22
+ getDtsFromLabel(builder, component.content, labels.CustomLabel);
23
+ return {
24
+ declarationContent: builder.build(),
25
+ mapData: builder.getMappings(),
26
+ ...component,
27
+ };
28
+ }
29
+
30
+ /**
31
+ *
32
+ * @param {PositionAwareTextBuilder} builder
33
+ * @param {string} content
34
+ * @param {any} label
35
+ */
36
+ export function getDtsFromLabel(builder, content, label) {
37
+ const labelName = label.fullName[0];
38
+ const namePosStart =
39
+ content.indexOf(`<fullName>${labelName}`) + `<fullName>`.length;
40
+ const namePosEnd = namePosStart + labelName.length;
41
+
42
+ builder.addText(`declare module `);
43
+ builder.addText(
44
+ `"@salesforce/label/c.${labelName}"`,
45
+ namePosStart,
46
+ namePosEnd
47
+ );
48
+ builder.addText(
49
+ ` {
50
+ /**
51
+ * @description ${label.shortDescription}
52
+ */
53
+ const `
54
+ );
55
+ builder.addText(`lbl${label.fullName}`, namePosStart, namePosEnd);
56
+ builder.addText(
57
+ ` = '${label.value}';
58
+ export default lbl${label.fullName};
59
+ }
60
+ `
61
+ );
62
+ }
@@ -1,5 +1,7 @@
1
1
  /** @import { Component, ConvertedComponent } from "../index.mjs" */
2
2
  import { parseString } from "xml2js";
3
+ import { PositionAwareTextBuilder } from "../util.mjs";
4
+ import { getDtsFromLabel } from "./custom-label.mjs";
3
5
 
4
6
  /**
5
7
  *
@@ -19,16 +21,14 @@ export function CustomLabels(component) {
19
21
  if (!Array.isArray(labels.CustomLabels.labels)) {
20
22
  labels.CustomLabels.labels = [labels.CustomLabels.labels];
21
23
  }
22
- let dec = "";
24
+
25
+ const builder = new PositionAwareTextBuilder();
23
26
  for (let label of labels.CustomLabels.labels) {
24
- dec += `declare module "@salesforce/label/c.${label.fullName}" {
25
- /**
26
- * @description ${label.shortDescription}
27
- */
28
- const lbl${label.fullName} = '${label.value}';
29
- export default lbl${label.fullName};
30
- }
31
- `;
27
+ getDtsFromLabel(builder, component.content, label);
32
28
  }
33
- return { declarationContent: dec, mapData: [], ...component };
29
+ return {
30
+ declarationContent: builder.build(),
31
+ mapData: builder.getMappings(),
32
+ ...component,
33
+ };
34
34
  }
@@ -1,5 +1,6 @@
1
1
  /** @import { Component, ConvertedComponent } from "../index.mjs" */
2
2
  import { parseString } from "xml2js";
3
+ import { PositionAwareTextBuilder } from "../util.mjs";
3
4
 
4
5
  /**
5
6
  *
@@ -17,14 +18,34 @@ export function CustomObject(component) {
17
18
  obj = result;
18
19
  });
19
20
  const objectName = component.path.split("/").pop()?.split(".")[0];
20
- const dec = `declare module '@salesforce/schema/${objectName}' {
21
+ if (!objectName) {
22
+ throw new Error("Unable to find Object Name");
23
+ }
24
+
25
+ const namePos = component.content.indexOf("<label>") + "<label>".length;
26
+ const namePosEnd = component.content.indexOf("</label>");
27
+
28
+ const builder = new PositionAwareTextBuilder();
29
+ builder.addText(`declare module `);
30
+ builder.addText(`"@salesforce/schema/${objectName}"`, namePos, namePosEnd);
31
+ builder.addText(
32
+ ` {
21
33
  /**
22
34
  * @description ${obj.CustomObject.description}
23
35
  */
24
- const ${objectName}: {
36
+ const `
37
+ );
38
+ builder.addText(`${objectName}`, namePos, namePosEnd);
39
+ builder.addText(
40
+ `: {
25
41
  objectApiName: '${objectName}';
26
42
  }
27
43
  export default ${objectName};
28
- }`;
29
- return { declarationContent: dec, mapData: [], ...component };
44
+ }`
45
+ );
46
+ return {
47
+ declarationContent: builder.build(),
48
+ mapData: builder.getMappings(),
49
+ ...component,
50
+ };
30
51
  }
@@ -1,5 +1,6 @@
1
1
  /** @import { Component, ConvertedComponent } from "../index.mjs" */
2
2
  import { parseString } from "xml2js";
3
+ import { PositionAwareTextBuilder } from "../util.mjs";
3
4
 
4
5
  /**
5
6
  *
@@ -7,7 +8,6 @@ import { parseString } from "xml2js";
7
8
  * @returns {ConvertedComponent}
8
9
  */
9
10
  export function CustomPermission(component) {
10
- // generate d.ts
11
11
  /** @type {any} */
12
12
  let perm;
13
13
  parseString(component.content, (error, result) => {
@@ -17,14 +17,35 @@ export function CustomPermission(component) {
17
17
  perm = result;
18
18
  });
19
19
  const permName = component.path.split("/").pop()?.split(".")[0];
20
- const dec = `declare module "@salesforce/customPermission/${permName}" {
20
+ const namePosStart = component.content.indexOf("<label>") + "<label>".length;
21
+ const namePosEnd = component.content.indexOf("</label>");
22
+
23
+ const builder = new PositionAwareTextBuilder();
24
+ builder.addText("declare module ");
25
+ builder.addText(
26
+ `"@salesforce/customPermission/${permName}"`,
27
+ namePosStart,
28
+ namePosEnd
29
+ );
30
+
31
+ builder.addText(
32
+ ` {
21
33
  /**
22
34
  * ${perm.CustomPermission.label}
23
35
  *
24
36
  * @description ${perm.CustomPermission.description}
25
37
  */
26
- const has${permName}:boolean;
38
+ const `
39
+ );
40
+ builder.addText(`has${permName}`, namePosStart, namePosEnd);
41
+ builder.addText(
42
+ `:boolean;
27
43
  export default has${permName};
28
- }`;
29
- return { declarationContent: dec, mapData: [], ...component };
44
+ }`
45
+ );
46
+ return {
47
+ declarationContent: builder.build(),
48
+ mapData: builder.getMappings(),
49
+ ...component,
50
+ };
30
51
  }
@@ -1,5 +1,6 @@
1
1
  /** @import { Component, ConvertedComponent } from "../index.mjs" */
2
2
  import { parseString } from "xml2js";
3
+ import { PositionAwareTextBuilder } from "../util.mjs";
3
4
 
4
5
  /**
5
6
  *
@@ -17,13 +18,39 @@ export function StaticResource(component) {
17
18
  resource = result;
18
19
  });
19
20
  const resourceName = component.path.split("/").pop()?.split(".")[0];
20
- const dec = `declare module "@salesforce/resourceUrl/${resourceName}" {
21
+ const namePosStart =
22
+ component.content.indexOf("<description>") + "<description>".length;
23
+ const namePosEnd = component.content.indexOf("</description>");
24
+
25
+ const builder = new PositionAwareTextBuilder();
26
+
27
+ builder.addText("declare module ");
28
+
29
+ builder.addText(
30
+ `"@salesforce/resourceUrl/${resourceName}"`,
31
+ namePosStart,
32
+ namePosEnd
33
+ );
34
+
35
+ builder.addText(
36
+ ` {
21
37
  /**
22
38
  * @description ${resource.StaticResource.description}
23
39
  * @access ${resource.StaticResource.cacheControl}
24
40
  */
25
- const ${resourceName}:string;
41
+ const `
42
+ );
43
+
44
+ builder.addText(`${resourceName}`, namePosStart, namePosEnd);
45
+
46
+ builder.addText(
47
+ `:string;
26
48
  export default ${resourceName};
27
- }`;
28
- return { declarationContent: dec, mapData: [], ...component };
49
+ }`
50
+ );
51
+ return {
52
+ declarationContent: builder.build(),
53
+ mapData: builder.getMappings(),
54
+ ...component,
55
+ };
29
56
  }
package/lib/util.mjs ADDED
@@ -0,0 +1,68 @@
1
+ /** @import { MapPair } from "./index.mjs" */
2
+
3
+ /**
4
+ * @typedef MapDelta
5
+ * @property {number | null} sourcePos
6
+ * @property {number} destDeltaPos
7
+ */
8
+
9
+ /**
10
+ *
11
+ * @param {MapDelta[]} mappings
12
+ */
13
+ export function getMappings(mappings) {
14
+ // increase destination position as an aggregate
15
+ /** @type {MapPair[]} */
16
+ let filteredMappings = [];
17
+ let lastDestPos = 0;
18
+ mappings.forEach((m) => {
19
+ m.destDeltaPos += lastDestPos;
20
+ lastDestPos = m.destDeltaPos;
21
+ if (m.sourcePos !== null) {
22
+ filteredMappings.push({
23
+ sourcePos: m.sourcePos,
24
+ destPos: m.destDeltaPos,
25
+ });
26
+ }
27
+ });
28
+ return filteredMappings;
29
+ }
30
+
31
+ export class PositionAwareTextBuilder {
32
+ /** @type {MapDelta[]} */
33
+ mappings = [];
34
+ /** @type {string[]} */
35
+ textParts = [];
36
+
37
+ lastTextLen = 0;
38
+
39
+ /**
40
+ *
41
+ * @param {string} text
42
+ * @param {number?} sourceStartPos
43
+ * @param {number?} sourceEndPos
44
+ */
45
+ addText(text, sourceStartPos = null, sourceEndPos = null) {
46
+ this.mappings.push({
47
+ sourcePos: sourceStartPos,
48
+ destDeltaPos: this.lastTextLen,
49
+ });
50
+ this.lastTextLen = text.length;
51
+ if (sourceEndPos != null) {
52
+ this.mappings.push({
53
+ sourcePos: sourceEndPos,
54
+ destDeltaPos: this.lastTextLen,
55
+ });
56
+ this.lastTextLen = 0;
57
+ }
58
+ this.textParts.push(text);
59
+ }
60
+
61
+ build() {
62
+ return this.textParts.join("");
63
+ }
64
+
65
+ getMappings() {
66
+ return getMappings(this.mappings);
67
+ }
68
+ }