@servicenow/sdk-build-core 2.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.
Files changed (130) hide show
  1. package/dist/BuildOptions.d.ts +50 -0
  2. package/dist/BuildOptions.js +46 -0
  3. package/dist/BuildOptions.js.map +1 -0
  4. package/dist/GUID.d.ts +2 -0
  5. package/dist/GUID.js +48 -0
  6. package/dist/GUID.js.map +1 -0
  7. package/dist/Keys.d.ts +29 -0
  8. package/dist/Keys.js +258 -0
  9. package/dist/Keys.js.map +1 -0
  10. package/dist/TypeScript.d.ts +5 -0
  11. package/dist/TypeScript.js +81 -0
  12. package/dist/TypeScript.js.map +1 -0
  13. package/dist/XML.d.ts +25 -0
  14. package/dist/XML.js +72 -0
  15. package/dist/XML.js.map +1 -0
  16. package/dist/index.d.ts +8 -0
  17. package/dist/index.js +38 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/plugins/Context.d.ts +198 -0
  20. package/dist/plugins/Context.js +3 -0
  21. package/dist/plugins/Context.js.map +1 -0
  22. package/dist/plugins/Diagnostic.d.ts +10 -0
  23. package/dist/plugins/Diagnostic.js +52 -0
  24. package/dist/plugins/Diagnostic.js.map +1 -0
  25. package/dist/plugins/Plugin.d.ts +175 -0
  26. package/dist/plugins/Plugin.js +15 -0
  27. package/dist/plugins/Plugin.js.map +1 -0
  28. package/dist/plugins/behaviors/Arranger.d.ts +26 -0
  29. package/dist/plugins/behaviors/Arranger.js +3 -0
  30. package/dist/plugins/behaviors/Arranger.js.map +1 -0
  31. package/dist/plugins/behaviors/Composer.d.ts +101 -0
  32. package/dist/plugins/behaviors/Composer.js +15 -0
  33. package/dist/plugins/behaviors/Composer.js.map +1 -0
  34. package/dist/plugins/behaviors/Diagnostics.d.ts +8 -0
  35. package/dist/plugins/behaviors/Diagnostics.js +3 -0
  36. package/dist/plugins/behaviors/Diagnostics.js.map +1 -0
  37. package/dist/plugins/behaviors/Generator.d.ts +21 -0
  38. package/dist/plugins/behaviors/Generator.js +3 -0
  39. package/dist/plugins/behaviors/Generator.js.map +1 -0
  40. package/dist/plugins/behaviors/OwnedTables.d.ts +6 -0
  41. package/dist/plugins/behaviors/OwnedTables.js +3 -0
  42. package/dist/plugins/behaviors/OwnedTables.js.map +1 -0
  43. package/dist/plugins/behaviors/PostProcessor.d.ts +5 -0
  44. package/dist/plugins/behaviors/PostProcessor.js +3 -0
  45. package/dist/plugins/behaviors/PostProcessor.js.map +1 -0
  46. package/dist/plugins/behaviors/Serializer.d.ts +29 -0
  47. package/dist/plugins/behaviors/Serializer.js +3 -0
  48. package/dist/plugins/behaviors/Serializer.js.map +1 -0
  49. package/dist/plugins/behaviors/Transformer.d.ts +23 -0
  50. package/dist/plugins/behaviors/Transformer.js +3 -0
  51. package/dist/plugins/behaviors/Transformer.js.map +1 -0
  52. package/dist/plugins/behaviors/extractors/Data.d.ts +107 -0
  53. package/dist/plugins/behaviors/extractors/Data.js +191 -0
  54. package/dist/plugins/behaviors/extractors/Data.js.map +1 -0
  55. package/dist/plugins/behaviors/extractors/Extractors.d.ts +41 -0
  56. package/dist/plugins/behaviors/extractors/Extractors.js +3 -0
  57. package/dist/plugins/behaviors/extractors/Extractors.js.map +1 -0
  58. package/dist/plugins/behaviors/extractors/index.d.ts +2 -0
  59. package/dist/plugins/behaviors/extractors/index.js +19 -0
  60. package/dist/plugins/behaviors/extractors/index.js.map +1 -0
  61. package/dist/plugins/behaviors/index.d.ts +9 -0
  62. package/dist/plugins/behaviors/index.js +26 -0
  63. package/dist/plugins/behaviors/index.js.map +1 -0
  64. package/dist/plugins/index.d.ts +5 -0
  65. package/dist/plugins/index.js +23 -0
  66. package/dist/plugins/index.js.map +1 -0
  67. package/dist/plugins/util/CallExpression.d.ts +6 -0
  68. package/dist/plugins/util/CallExpression.js +93 -0
  69. package/dist/plugins/util/CallExpression.js.map +1 -0
  70. package/dist/plugins/util/CodeTransformation.d.ts +74 -0
  71. package/dist/plugins/util/CodeTransformation.js +421 -0
  72. package/dist/plugins/util/CodeTransformation.js.map +1 -0
  73. package/dist/plugins/util/ConfigurationFunction.d.ts +106 -0
  74. package/dist/plugins/util/ConfigurationFunction.js +377 -0
  75. package/dist/plugins/util/ConfigurationFunction.js.map +1 -0
  76. package/dist/plugins/util/ObjectLiteral.d.ts +9 -0
  77. package/dist/plugins/util/ObjectLiteral.js +60 -0
  78. package/dist/plugins/util/ObjectLiteral.js.map +1 -0
  79. package/dist/plugins/util/index.d.ts +4 -0
  80. package/dist/plugins/util/index.js +21 -0
  81. package/dist/plugins/util/index.js.map +1 -0
  82. package/dist/util/Debug.d.ts +8 -0
  83. package/dist/util/Debug.js +39 -0
  84. package/dist/util/Debug.js.map +1 -0
  85. package/dist/util/Util.d.ts +4 -0
  86. package/dist/util/Util.js +41 -0
  87. package/dist/util/Util.js.map +1 -0
  88. package/dist/util/XMLJsonBuilder.d.ts +18 -0
  89. package/dist/util/XMLJsonBuilder.js +59 -0
  90. package/dist/util/XMLJsonBuilder.js.map +1 -0
  91. package/dist/util/XMLUploadParser.d.ts +22 -0
  92. package/dist/util/XMLUploadParser.js +67 -0
  93. package/dist/util/XMLUploadParser.js.map +1 -0
  94. package/dist/util/index.d.ts +4 -0
  95. package/dist/util/index.js +21 -0
  96. package/dist/util/index.js.map +1 -0
  97. package/license +9 -0
  98. package/package.json +42 -0
  99. package/src/BuildOptions.ts +27 -0
  100. package/src/GUID.ts +26 -0
  101. package/src/Keys.ts +287 -0
  102. package/src/TypeScript.ts +65 -0
  103. package/src/XML.ts +85 -0
  104. package/src/index.ts +8 -0
  105. package/src/plugins/Context.ts +249 -0
  106. package/src/plugins/Diagnostic.ts +31 -0
  107. package/src/plugins/Plugin.ts +246 -0
  108. package/src/plugins/behaviors/Arranger.ts +42 -0
  109. package/src/plugins/behaviors/Composer.ts +124 -0
  110. package/src/plugins/behaviors/Diagnostics.ts +13 -0
  111. package/src/plugins/behaviors/Generator.ts +31 -0
  112. package/src/plugins/behaviors/OwnedTables.ts +5 -0
  113. package/src/plugins/behaviors/PostProcessor.ts +6 -0
  114. package/src/plugins/behaviors/Serializer.ts +39 -0
  115. package/src/plugins/behaviors/Transformer.ts +32 -0
  116. package/src/plugins/behaviors/extractors/Data.ts +247 -0
  117. package/src/plugins/behaviors/extractors/Extractors.ts +57 -0
  118. package/src/plugins/behaviors/extractors/index.ts +2 -0
  119. package/src/plugins/behaviors/index.ts +9 -0
  120. package/src/plugins/index.ts +5 -0
  121. package/src/plugins/util/CallExpression.ts +83 -0
  122. package/src/plugins/util/CodeTransformation.ts +500 -0
  123. package/src/plugins/util/ConfigurationFunction.ts +477 -0
  124. package/src/plugins/util/ObjectLiteral.ts +37 -0
  125. package/src/plugins/util/index.ts +4 -0
  126. package/src/util/Debug.ts +46 -0
  127. package/src/util/Util.ts +21 -0
  128. package/src/util/XMLJsonBuilder.ts +64 -0
  129. package/src/util/XMLUploadParser.ts +90 -0
  130. package/src/util/index.ts +4 -0
@@ -0,0 +1,477 @@
1
+ import { Logger } from '@servicenow/sdk-project'
2
+ import {
3
+ ArrowFunction,
4
+ CallExpression,
5
+ FunctionExpression,
6
+ Identifier,
7
+ Node,
8
+ ObjectLiteralExpression,
9
+ PropertyAccessExpression,
10
+ PropertyAssignment,
11
+ SyntaxKind,
12
+ TemplateExpression,
13
+ ts,
14
+ } from 'ts-morph'
15
+ import { Context } from '../Context'
16
+ import { EntityData, ObjectData } from '../behaviors'
17
+ import { getCallExpressionName } from './CallExpression'
18
+
19
+ export type ConfigEntity = {
20
+ inputObject: InputObject
21
+ configEntries: ConfigEntries
22
+ }
23
+
24
+ export type InputObject = {
25
+ $id: string | number
26
+ [name: string]: any
27
+ }
28
+
29
+ export type ConfigEntries = {
30
+ [index: `${number}`]: ConfigEntry
31
+ }
32
+
33
+ export type ConfigEntry = {
34
+ name: string
35
+ info: { [name: string]: any }
36
+ }
37
+
38
+ export type ComposerClass = {
39
+ prototype: any
40
+ [key: string]: any
41
+ }
42
+
43
+ // TODO: Just created this to avoid refactoring all of the ATF stuff. Should be removed
44
+ // and the EntityData class should be used instead, if possible.
45
+ export class ConfigurationFunctionEntityData extends EntityData {
46
+ constructor(
47
+ private readonly config: ConfigEntity,
48
+ node: CallExpression
49
+ ) {
50
+ super(
51
+ 'test',
52
+ 'NO_GUID_GENERATED', // TODO: Generate GUID?
53
+ new ObjectData({}, node),
54
+ node
55
+ )
56
+ }
57
+
58
+ override getValue() {
59
+ return this.config
60
+ }
61
+ }
62
+
63
+ /**
64
+ * This is an extractor used to extract configuration functions.
65
+ * This is an example of a configuration function:
66
+ * ```typescript
67
+ * import { table } from 'place'
68
+ * PlaceHolderFunction('Unique ID', (param) => {
69
+ * param.foo({arg1: 'hey', arg2: table});
70
+ * const output = param.bar({placeholder: 'value'});
71
+ * param.baz({arg1: output.value1})
72
+ * })
73
+ * ```
74
+ */
75
+ export class ConfigurationFunctionExtractor {
76
+ private rootNode: CallExpression
77
+ private context: Context
78
+ private composerClass: ComposerClass
79
+ private identifiers: (Identifier | null)[]
80
+
81
+ /**
82
+ *
83
+ * @param rootNode rootNode of AST where extraction begins
84
+ * @param context context from the plugin this is called in
85
+ * @param composerClass A class whose prototype will be used to validate the allowed functions within the configuration function
86
+ */
87
+ constructor(rootNode: CallExpression, context: Context, composerClass: ComposerClass) {
88
+ this.rootNode = rootNode
89
+ this.context = context
90
+ this.composerClass = composerClass
91
+ this.identifiers = []
92
+ }
93
+
94
+ /**
95
+ * Grabs the property access expression chain of a call expression, and protects against accidentally grabbing property access expressions in arguments of the call expression
96
+ */
97
+ private getPropChain(ce: CallExpression): PropertyAccessExpression[] {
98
+ // Do this to ensure we are only considering property access expressions that are apart of the chain and not potentially in the arguments of the function
99
+ const props = [ce.getFirstDescendantByKindOrThrow(SyntaxKind.PropertyAccessExpression)]
100
+ const childProps = (props[0] as PropertyAccessExpression).getDescendantsOfKind(
101
+ SyntaxKind.PropertyAccessExpression
102
+ )
103
+ if (childProps.length > 0) {
104
+ props.push(...childProps)
105
+ }
106
+ return props
107
+ }
108
+
109
+ /**
110
+ * Returns the first element of a property access chain
111
+ * v
112
+ * a.b.c.d({})
113
+ */
114
+ private getFirstProp(ce: CallExpression): Identifier {
115
+ // Need to use this instead of just grabbing descendents cause the arguments of the call expression can also potentially have property access expressions which we don't want to consider
116
+ const props = this.getPropChain(ce)
117
+ if (props.length === 1) {
118
+ const ids = props[0]?.getChildrenOfKind(SyntaxKind.Identifier) as Identifier[]
119
+ return ids[0] as Identifier
120
+ }
121
+ const lastProp = props[props.length - 1] as PropertyAccessExpression
122
+ return lastProp.getChildrenOfKind(SyntaxKind.Identifier)[0] as Identifier
123
+ }
124
+
125
+ /**
126
+ * Returns the last element of a property access chain
127
+ *
128
+ * v
129
+ * a.b.c.d({})
130
+ */
131
+ private getLastProp(ce: CallExpression): Identifier {
132
+ // Do not need special chain handling here like in getFirstProp cause we are always grabbing the property access expression from the top level regardless of chain length
133
+ const props = ce.getFirstDescendantByKindOrThrow(SyntaxKind.PropertyAccessExpression)
134
+ const ids = props.getChildrenOfKind(SyntaxKind.Identifier) as Identifier[]
135
+ return ids[ids.length - 1] as Identifier // Identifier is either the only one present or is the last of 2 so this should fetch it in either scenario
136
+ }
137
+
138
+ /**
139
+ * Validates that a call expression is valid. Will error if the call expression is inappropriately constructed for the configuration function
140
+ * @param ce
141
+ * @param parameterName
142
+ */
143
+ private isValidCallExpression(ce: CallExpression, paramId: Identifier) {
144
+ if (!paramId) {
145
+ throw Error('No parameter provided to configuration function')
146
+ }
147
+
148
+ // Ensure the line of the function call starts with the argument of the configuration function
149
+ const firstId = this.getFirstProp(ce)
150
+
151
+ if (firstId?.getText() !== paramId?.getText()) {
152
+ throw Error(
153
+ `Call expression does not begin with "${paramId.getText()}", instead began with "${firstId.getText()}"`
154
+ )
155
+ }
156
+
157
+ // Verify that the function call is actually a valid function call based on the composer class this will be used with
158
+ const lastId = this.getLastProp(ce)
159
+ const functionName = lastId.getText()
160
+ const nameIsValid = Object.getOwnPropertyNames(this.composerClass.prototype).some((funcName) => {
161
+ return funcName != 'constructor' && functionName.startsWith(funcName)
162
+ })
163
+ if (!nameIsValid) {
164
+ throw Error(`Call expression function does not exist on the object ${paramId.getText()}`)
165
+ }
166
+
167
+ // Verify that call expression has the expected number of arguments
168
+ const funcArgs = ce.getFirstDescendantByKind(SyntaxKind.SyntaxList)?.getChildren() as Node[]
169
+ if (!funcArgs) {
170
+ throw Error('Call expression had no arguments at all')
171
+ }
172
+
173
+ if (funcArgs.length != 1) {
174
+ throw Error('Call expressions only support a single argument')
175
+ }
176
+
177
+ if ((funcArgs[0] as Node).getKind() != SyntaxKind.ObjectLiteralExpression) {
178
+ throw Error('Call expression must have an object as its argument')
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Returns the step number of the step containing a identifier which matches the type of the queried identfier
184
+ *
185
+ * If it cannot find the identifier, it returns -1
186
+ * @param identifier
187
+ * @param data
188
+ */
189
+ private findReferencedIdentifier(identifier: Identifier) {
190
+ const idName = identifier.getSymbol()?.getName()
191
+ // Identifier index lines up with step index, removed from step object so identifiers are not returned in extracted object
192
+ for (let i = 0; i < this.identifiers.length; i++) {
193
+ const identifier = this.identifiers[i]
194
+ // Maybe there's another way to more accurately identify but this should be fine?
195
+ const name = identifier?.getSymbol()?.getName()
196
+ if (name && idName && name == idName) {
197
+ return i
198
+ }
199
+ }
200
+ return -1
201
+ }
202
+
203
+ private getRootIdentifier(prop: PropertyAccessExpression): Identifier {
204
+ const children = prop.getChildrenOfKind(SyntaxKind.PropertyAccessExpression)
205
+ if (children.length == 0) {
206
+ const id = prop.getFirstChildByKind(SyntaxKind.Identifier)
207
+ if (!id) {
208
+ throw Error('Could not find identifier in property access expression')
209
+ }
210
+ return id
211
+ }
212
+ return this.getRootIdentifier(children[0] as PropertyAccessExpression)
213
+ }
214
+
215
+ private parseValue(valueNode: Node): any {
216
+ if (valueNode.getKind() == SyntaxKind.ArrayLiteralExpression) {
217
+ const values: any[] = []
218
+ const syntaxList = valueNode.getFirstDescendantByKind(SyntaxKind.SyntaxList)
219
+ if (!syntaxList) {
220
+ return
221
+ }
222
+ const children = syntaxList.getChildren()
223
+
224
+ for (const child of children) {
225
+ if (child.getKind() == SyntaxKind.CommaToken) {
226
+ continue
227
+ }
228
+ const value = this.parseValue(child)
229
+ values.push(value)
230
+ }
231
+ return values
232
+ }
233
+
234
+ if (valueNode.getKind() == SyntaxKind.ObjectLiteralExpression) {
235
+ const objNode = valueNode as ObjectLiteralExpression
236
+ return this.objNodeToJSON(objNode)
237
+ }
238
+
239
+ if (valueNode.getKind() == SyntaxKind.PropertyAccessExpression) {
240
+ const propNode = valueNode as PropertyAccessExpression
241
+ const identifier = this.getRootIdentifier(propNode)
242
+ const refIdentifierIndex = this.findReferencedIdentifier(identifier)
243
+ if (refIdentifierIndex == -1) {
244
+ throw Error('Step corresponding to property access expression could not be found')
245
+ }
246
+
247
+ const parsedValue = valueNode.getText().split('.')
248
+ // Replace it just with a string here. Parsing into a real value will happen in the composer
249
+ return { __references: refIdentifierIndex, __value: parsedValue[parsedValue.length - 1] }
250
+ }
251
+
252
+ if (valueNode.getKind() == SyntaxKind.TemplateExpression) {
253
+ // console.info(`literal: ${(valueNode as TemplateExpression).getVal}`)
254
+ const templateNode = valueNode as TemplateExpression
255
+ const templateValue = [templateNode.getHead().getLiteralText()]
256
+ templateNode.getTemplateSpans().forEach((s) => {
257
+ templateValue.push(this.parseValue(s.getExpression()))
258
+ templateValue.push(s.getLiteral().getLiteralText())
259
+ })
260
+ return { __template: templateValue }
261
+ }
262
+
263
+ // A variety of Identifiers. Will be handled here
264
+ // TODO: This is swallowing diagnostics and not handling the case where no data was extracted
265
+ return this.context.extractAst(valueNode)['data'][0]?.getValue()
266
+ }
267
+
268
+ private getKVPair(prop: PropertyAssignment) {
269
+ const props = prop.getChildren()
270
+ const keyNode = props[0] as Node<ts.Node>
271
+ const valueNode = props[2] as Node<ts.Node>
272
+
273
+ const key = (keyNode as any).getLiteralValue ? (keyNode as any).getLiteralValue() : keyNode.getText()
274
+ const value = this.parseValue(valueNode)
275
+
276
+ if (typeof key !== 'undefined' && typeof value !== 'undefined') {
277
+ return { key, value }
278
+ }
279
+ return { key: undefined, value: undefined }
280
+ }
281
+
282
+ private objNodeToJSON(obj: ObjectLiteralExpression | undefined): { [name: string]: any } {
283
+ if (!obj) {
284
+ throw Error('objNodeToJSON received an undefined object node')
285
+ }
286
+ const syntaxList = obj.getFirstDescendantByKind(SyntaxKind.SyntaxList)
287
+
288
+ if (!syntaxList) {
289
+ throw Error('objNodeToJSON could not find a syntax list in the ObjectLiteralExpression')
290
+ }
291
+
292
+ const json = {}
293
+ syntaxList.getChildrenOfKind(SyntaxKind.PropertyAssignment).forEach((arg) => {
294
+ const propArg = arg
295
+ const { key, value } = this.getKVPair(propArg)
296
+ // TODO: What if the value is actually undefined?
297
+ if (key !== undefined && value !== undefined) {
298
+ json[key] = value
299
+ }
300
+ })
301
+ return json
302
+ }
303
+
304
+ private getName(node: CallExpression) {
305
+ // This may not be robust but should be a good enough
306
+ const lastProp = this.getLastProp(node)
307
+ return lastProp.getText()
308
+ }
309
+
310
+ private getConfigFunctionComponents(kind: string, node: CallExpression<ts.CallExpression>) {
311
+ kind
312
+ const configArgs = node.getArguments()
313
+ if (configArgs.length != 2) {
314
+ throw Error('The plugin must have exactly 2 arguments')
315
+ }
316
+
317
+ if ((configArgs[0] as any).getKind() != SyntaxKind.ObjectLiteralExpression) {
318
+ throw Error("The plugin's first argument was not an object")
319
+ }
320
+
321
+ if (
322
+ (configArgs[1] as any).getKind() != SyntaxKind.ArrowFunction &&
323
+ (configArgs[1] as any).getKind() != SyntaxKind.FunctionExpression
324
+ ) {
325
+ throw Error("The plugin's second argument was not a function")
326
+ }
327
+
328
+ const objectArg = configArgs[0] as ObjectLiteralExpression
329
+ const configFunction = configArgs[1] as ArrowFunction | FunctionExpression
330
+
331
+ // TODO: This is swallowing diagnostics and not handling the case where no data was extracted
332
+ const objectArgValue = this.context.extractAst(objectArg!)['data'][0]?.getValue()
333
+
334
+ if (!('$id' in objectArgValue)) {
335
+ throw Error('No $id defined in first argument')
336
+ }
337
+
338
+ const parameters = configFunction.getParameters()
339
+ // Checking the type is parameter is probably redundant but can not hurt
340
+ if (parameters.length != 1 || !parameters[0] || parameters[0].getKind() != SyntaxKind.Parameter) {
341
+ throw Error('Configuration function must have an parameter as its only argument')
342
+ }
343
+
344
+ const paramId = parameters[0].getChildrenOfKind(SyntaxKind.Identifier)[0] as Identifier
345
+ const block = configFunction.getFirstChildByKind(SyntaxKind.Block)
346
+ if (!block) {
347
+ throw Error(`Failed to find block in configuration function`)
348
+ }
349
+
350
+ const syntaxList = block.getFirstChildByKind(SyntaxKind.SyntaxList)
351
+ if (!syntaxList) {
352
+ throw Error(`Failed to find syntax list in configuration function`)
353
+ }
354
+ return { paramId, syntaxList, objectArgValue }
355
+ }
356
+
357
+ private extractCallExpression(node: Node, paramId: Identifier) {
358
+ const callExpressions = node.getDescendantsOfKind(SyntaxKind.CallExpression)
359
+
360
+ if (callExpressions.length == 0) {
361
+ throw Error(`Expression statement did not have any call expression`)
362
+ }
363
+
364
+ if (callExpressions.length > 1) {
365
+ throw Error(`Found more than one call expression inside of an expression statement`)
366
+ }
367
+
368
+ const callExpression = callExpressions[0] as CallExpression
369
+ this.isValidCallExpression(callExpression, paramId)
370
+ return callExpression
371
+ }
372
+
373
+ extractConfigFunction(
374
+ kind: string, // Name of function for now
375
+ logger: Logger
376
+ ): EntityData | undefined {
377
+ try {
378
+ return this.extractConfigFunctionUnsafe(kind, logger)
379
+ } catch (e) {
380
+ console.error('Could not extract configuration function: ', e)
381
+ // swallows error, maybe fully re-echo but probably should just swallow silently to behave as expected
382
+ return undefined
383
+ }
384
+ }
385
+
386
+ /**
387
+ * Returns a single entity of the kind specified which contains all data of nodes within the configuration function
388
+ *
389
+ * Will attempt to generalize this for use by flow team
390
+ * @param kind
391
+ * @param node
392
+ * @param context
393
+ * @returns
394
+ */
395
+ extractConfigFunctionUnsafe(
396
+ kind: string, // Name of function for now,
397
+ logger: Logger
398
+ ): EntityData | undefined {
399
+ // If this node is not the correct kind, return undefined. Some other extractor will handle it
400
+ const name = getCallExpressionName(this.rootNode).toLowerCase()
401
+ if (name !== kind) {
402
+ return undefined
403
+ }
404
+
405
+ // Below here we assume anything that makes it this far is being extracted by the right type of extractor so failing to get any other part will result in an error stopping compilation instead
406
+ const { paramId, syntaxList, objectArgValue } = this.getConfigFunctionComponents(kind, this.rootNode)
407
+ const config: ConfigEntity = { inputObject: objectArgValue, configEntries: {} }
408
+ const data = config.configEntries
409
+ let stepNumber = 0
410
+ syntaxList.getChildren().forEach((statement) => {
411
+ try {
412
+ switch (statement.getKind()) {
413
+ case SyntaxKind.ExpressionStatement: {
414
+ /**
415
+ * (param) => {
416
+ * param.foo({arg: 'value'}) <- Expression statement
417
+ * }
418
+ */
419
+ // This formatting of casting is used throughout the class. Try to keep casting close to where the value is determined and then save it to a cast variable and use
420
+ // that below. This prevents accidentally using a variable as multiple different types by forgetting to cast and also keeps casting near where type is determined
421
+ // so its more clear why it is being cast
422
+ const ce = this.extractCallExpression(statement, paramId)
423
+ data[stepNumber++] = {
424
+ name: this.getName(ce),
425
+ info: this.objNodeToJSON(ce.getFirstDescendantByKind(SyntaxKind.ObjectLiteralExpression)),
426
+ }
427
+ this.identifiers.push(null)
428
+ break
429
+ }
430
+ case SyntaxKind.VariableStatement: {
431
+ /**
432
+ * (param) => {
433
+ * const output = param.foo({arg: 'value'}) <- Variable statement
434
+ * }
435
+ */
436
+ const ce = this.extractCallExpression(statement, paramId)
437
+ const variableDeclaration = statement.getFirstDescendantByKind(SyntaxKind.VariableDeclaration)
438
+
439
+ if (!variableDeclaration) {
440
+ throw Error('Could not find variable declaration in variable statement')
441
+ }
442
+
443
+ const assignment = variableDeclaration.getChildren() // Format: identifier, =, value
444
+ const identifier = assignment[0] as Identifier
445
+ const value = assignment[2] as CallExpression // Already been validated above to be a valid call expression
446
+
447
+ data[stepNumber++] = {
448
+ name: this.getName(ce),
449
+ info: this.objNodeToJSON(
450
+ value.getFirstDescendantByKind(SyntaxKind.ObjectLiteralExpression)
451
+ ),
452
+ }
453
+ this.identifiers.push(identifier)
454
+ break
455
+ }
456
+ case SyntaxKind.MultiLineCommentTrivia:
457
+ /** This is a multiline comment */
458
+ break
459
+ case SyntaxKind.SingleLineCommentTrivia:
460
+ // This is a single line comment
461
+ break
462
+ default:
463
+ throw Error(
464
+ `Found an invalid node in the configuration function block, found: ${
465
+ statement?.getKindName() ? statement.getKindName() : 'Kind name undefined'
466
+ }`
467
+ )
468
+ }
469
+ } catch (e) {
470
+ // Provides easy context w/o having to worry about having blah.getText() in all errors
471
+ logger.error(`Encountered an error while parsing statement: ${statement.getText()}`)
472
+ throw e
473
+ }
474
+ })
475
+ return new ConfigurationFunctionEntityData(config, this.rootNode)
476
+ }
477
+ }
@@ -0,0 +1,37 @@
1
+ import * as ts from 'ts-morph'
2
+
3
+ /**
4
+ * Retrieves the property assignment with the specified name and subpath from the given node.
5
+ * @param node - The node to search for the property assignment.
6
+ * @param property - The name of the property assignment to retrieve.
7
+ * @param subPath - Optional subpath to navigate within the property assignment.
8
+ * @returns The property assignment node if found, otherwise undefined.
9
+ */
10
+ export function getPropertyAssignment(node: ts.Node | undefined, property: string, ...subPath: string[]) {
11
+ let current = node
12
+ ?.asKind(ts.SyntaxKind.ObjectLiteralExpression)
13
+ // get the property assignment with the specified name
14
+ ?.getProperty((p) => hasPropertyAssignment(p, property))
15
+ ?.asKind(ts.SyntaxKind.PropertyAssignment)
16
+
17
+ for (const pathPart of subPath) {
18
+ current = current
19
+ ?.getInitializerIfKind(ts.SyntaxKind.ObjectLiteralExpression)
20
+ // get the property assignment with the specified name
21
+ ?.getProperty((p) => hasPropertyAssignment(p, pathPart))
22
+ ?.asKind(ts.SyntaxKind.PropertyAssignment)
23
+ }
24
+
25
+ return current
26
+ }
27
+
28
+ /**
29
+ * Checks if the given property assignment has the specified path.
30
+ * @param property - The property assignment to check.
31
+ * @param path - The path to compare against.
32
+ * @returns True if the property assignment has the specified path, otherwise false.
33
+ */
34
+ function hasPropertyAssignment(property: ts.ObjectLiteralElementLike | undefined, path: string): boolean {
35
+ const child = property?.getChildren().find((c) => ts.Node.isStringLiteral(c) || ts.Node.isIdentifier(c))
36
+ return child?.getSymbol()?.getEscapedName() === path
37
+ }
@@ -0,0 +1,4 @@
1
+ export * from './CallExpression'
2
+ export * from './CodeTransformation'
3
+ export * from './ObjectLiteral'
4
+ export * from './ConfigurationFunction'
@@ -0,0 +1,46 @@
1
+ import { inspect as nodeInspect } from 'util'
2
+ import { Data, Document, File } from '../plugins/behaviors'
3
+ import { Logger } from '@servicenow/sdk-project'
4
+
5
+ export function inspect(val: unknown, depth = 2) {
6
+ return nodeInspect(val, {
7
+ colors: true,
8
+ compact: false,
9
+ depth,
10
+ })
11
+ }
12
+
13
+ export function debugData(data: ({ data: unknown } | Data)[], logger: Logger, message = 'DATA:') {
14
+ logger.info(message)
15
+ logger.info(
16
+ inspect(
17
+ data.map((d) => {
18
+ if (d instanceof Data) {
19
+ return d.getValue()
20
+ } else {
21
+ return d
22
+ }
23
+ }),
24
+ 5
25
+ )
26
+ )
27
+ }
28
+
29
+ export function debugDocuments(documents: Document[], logger: Logger, message = 'DOCUMENTS:') {
30
+ logger.info(message)
31
+ logger.info(
32
+ inspect(
33
+ documents.map((d) => {
34
+ const { node, ...rest } = d
35
+ node
36
+ return rest
37
+ }),
38
+ 5
39
+ )
40
+ )
41
+ }
42
+
43
+ export function debugFiles(files: File[], logger: Logger, message = 'FILES:') {
44
+ logger.info(message)
45
+ logger.info(inspect(files, 5))
46
+ }
@@ -0,0 +1,21 @@
1
+ import * as path from 'path'
2
+ import { FileSystem } from '@servicenow/sdk-project'
3
+
4
+ export const NOW_DIR = '.now'
5
+
6
+ export function safeWriteFileToPath(
7
+ fs: FileSystem,
8
+ dirPath: string,
9
+ fileName: string,
10
+ content: string,
11
+ encoding: FileSystem.Encoding = 'utf-8'
12
+ ) {
13
+ createDirIfNotExists(dirPath, fs)
14
+ fs.writeFileSync(path.join(dirPath, fileName), content, { encoding: encoding })
15
+ }
16
+
17
+ export function createDirIfNotExists(dirPath: string, fs: FileSystem) {
18
+ if (!FileSystem.existsSync(fs, dirPath)) {
19
+ fs.mkdirSync(dirPath, { recursive: true })
20
+ }
21
+ }
@@ -0,0 +1,64 @@
1
+ export class XMLJsonBuilder {
2
+ private rootXmlElement: XMLJsonElement
3
+ constructor(version: string) {
4
+ this.rootXmlElement = new XMLJsonElement('_root')
5
+ this.rootXmlElement.addJsonObj('?xml', undefined, { version })
6
+ }
7
+
8
+ createRoot(element: string, text: string = '', attrs: Record<string, string | number | boolean> = {}) {
9
+ return this.rootXmlElement.addJsonObj(element, text, attrs)
10
+ }
11
+
12
+ buildJsonObj() {
13
+ const {
14
+ _root: { '#text': _rootText, ...jsonObj },
15
+ } = this.rootXmlElement.getData()
16
+ return jsonObj
17
+ }
18
+ }
19
+
20
+ export class XMLJsonElement {
21
+ private xmlEleMap: Map<string, XMLJsonElement[]> = new Map()
22
+
23
+ constructor(
24
+ private element: string,
25
+ private text: string = '',
26
+ private attrs: Record<string, string | number | boolean> = {},
27
+ private isCDATA: boolean = false
28
+ ) {}
29
+
30
+ addJsonObj(
31
+ element: string,
32
+ text: string = '',
33
+ attrs: Record<string, string | number | boolean> = {},
34
+ isCDATA: boolean = false
35
+ ) {
36
+ const xmlJsonObj = new XMLJsonElement(element, text, attrs, isCDATA)
37
+ if (!this.xmlEleMap.has(element)) {
38
+ this.xmlEleMap.set(element, [])
39
+ }
40
+ this.xmlEleMap.get(element)!.push(xmlJsonObj)
41
+ return xmlJsonObj
42
+ }
43
+
44
+ getData() {
45
+ const additionalElements: any = []
46
+ this.xmlEleMap.forEach((value, key) => {
47
+ if (value.length === 1) {
48
+ additionalElements.push({ ...value[0]!.getData() })
49
+ return
50
+ }
51
+ additionalElements.push({ [key]: value.map((value) => value.getData()[key]) })
52
+ })
53
+
54
+ const additionalObj = additionalElements.reduce((acc, ele) => ({ ...acc, ...ele }), {})
55
+
56
+ return {
57
+ [this.element]: {
58
+ ...(this.isCDATA ? { __cdata: this.text } : { '#text': this.text }),
59
+ ...Object.fromEntries(Object.entries(this.attrs).map(([k, v]) => [`@_${k}`, v])),
60
+ ...additionalObj,
61
+ },
62
+ }
63
+ }
64
+ }