@servicenow/sdk-build-plugins 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 (166) hide show
  1. package/dist/AttachmentPlugin.d.ts +253 -0
  2. package/dist/AttachmentPlugin.js +216 -0
  3. package/dist/AttachmentPlugin.js.map +1 -0
  4. package/dist/BusinessRulePlugin.d.ts +56 -0
  5. package/dist/BusinessRulePlugin.js +171 -0
  6. package/dist/BusinessRulePlugin.js.map +1 -0
  7. package/dist/CrossScopePrivilegePlugin.d.ts +22 -0
  8. package/dist/CrossScopePrivilegePlugin.js +42 -0
  9. package/dist/CrossScopePrivilegePlugin.js.map +1 -0
  10. package/dist/DefaultPlugin.d.ts +71 -0
  11. package/dist/DefaultPlugin.js +238 -0
  12. package/dist/DefaultPlugin.js.map +1 -0
  13. package/dist/IdPlugin.d.ts +17 -0
  14. package/dist/IdPlugin.js +45 -0
  15. package/dist/IdPlugin.js.map +1 -0
  16. package/dist/ListPlugin.d.ts +91 -0
  17. package/dist/ListPlugin.js +398 -0
  18. package/dist/ListPlugin.js.map +1 -0
  19. package/dist/PropertyPlugin.d.ts +122 -0
  20. package/dist/PropertyPlugin.js +165 -0
  21. package/dist/PropertyPlugin.js.map +1 -0
  22. package/dist/ScriptTemplatePlugin.d.ts +31 -0
  23. package/dist/ScriptTemplatePlugin.js +208 -0
  24. package/dist/ScriptTemplatePlugin.js.map +1 -0
  25. package/dist/UserPreferencePlugin.d.ts +16 -0
  26. package/dist/UserPreferencePlugin.js +30 -0
  27. package/dist/UserPreferencePlugin.js.map +1 -0
  28. package/dist/aclAndRole/AclPlugin.d.ts +117 -0
  29. package/dist/aclAndRole/AclPlugin.js +285 -0
  30. package/dist/aclAndRole/AclPlugin.js.map +1 -0
  31. package/dist/aclAndRole/RolePlugin.d.ts +58 -0
  32. package/dist/aclAndRole/RolePlugin.js +152 -0
  33. package/dist/aclAndRole/RolePlugin.js.map +1 -0
  34. package/dist/aclAndRole/Util.d.ts +3 -0
  35. package/dist/aclAndRole/Util.js +106 -0
  36. package/dist/aclAndRole/Util.js.map +1 -0
  37. package/dist/app/ApplicationMenuPlugin.d.ts +32 -0
  38. package/dist/app/ApplicationMenuPlugin.js +106 -0
  39. package/dist/app/ApplicationMenuPlugin.js.map +1 -0
  40. package/dist/atf/ATFComposer.d.ts +492 -0
  41. package/dist/atf/ATFComposer.js +2717 -0
  42. package/dist/atf/ATFComposer.js.map +1 -0
  43. package/dist/atf/TestPlugin.d.ts +31 -0
  44. package/dist/atf/TestPlugin.js +95 -0
  45. package/dist/atf/TestPlugin.js.map +1 -0
  46. package/dist/atf/index.d.ts +1 -0
  47. package/dist/atf/index.js +9 -0
  48. package/dist/atf/index.js.map +1 -0
  49. package/dist/db/ColumnPlugins.d.ts +278 -0
  50. package/dist/db/ColumnPlugins.js +112 -0
  51. package/dist/db/ColumnPlugins.js.map +1 -0
  52. package/dist/db/RecordPlugin.d.ts +208 -0
  53. package/dist/db/RecordPlugin.js +287 -0
  54. package/dist/db/RecordPlugin.js.map +1 -0
  55. package/dist/db/TablePlugin.d.ts +742 -0
  56. package/dist/db/TablePlugin.js +1249 -0
  57. package/dist/db/TablePlugin.js.map +1 -0
  58. package/dist/db/index.d.ts +3 -0
  59. package/dist/db/index.js +27 -0
  60. package/dist/db/index.js.map +1 -0
  61. package/dist/index.d.ts +16 -0
  62. package/dist/index.js +51 -0
  63. package/dist/index.js.map +1 -0
  64. package/dist/scriptedRESTAPI/RESTDeserializationUtils.d.ts +12 -0
  65. package/dist/scriptedRESTAPI/RESTDeserializationUtils.js +371 -0
  66. package/dist/scriptedRESTAPI/RESTDeserializationUtils.js.map +1 -0
  67. package/dist/scriptedRESTAPI/RESTSerializationUtils.d.ts +15 -0
  68. package/dist/scriptedRESTAPI/RESTSerializationUtils.js +177 -0
  69. package/dist/scriptedRESTAPI/RESTSerializationUtils.js.map +1 -0
  70. package/dist/scriptedRESTAPI/RestApiPlugin.d.ts +144 -0
  71. package/dist/scriptedRESTAPI/RestApiPlugin.js +318 -0
  72. package/dist/scriptedRESTAPI/RestApiPlugin.js.map +1 -0
  73. package/dist/scriptedRESTAPI/RestSchemaUtils.d.ts +190 -0
  74. package/dist/scriptedRESTAPI/RestSchemaUtils.js +53 -0
  75. package/dist/scriptedRESTAPI/RestSchemaUtils.js.map +1 -0
  76. package/dist/scriptedRESTAPI/RestUtils.d.ts +75 -0
  77. package/dist/scriptedRESTAPI/RestUtils.js +469 -0
  78. package/dist/scriptedRESTAPI/RestUtils.js.map +1 -0
  79. package/dist/scripts/ClientScriptPlugin.d.ts +43 -0
  80. package/dist/scripts/ClientScriptPlugin.js +190 -0
  81. package/dist/scripts/ClientScriptPlugin.js.map +1 -0
  82. package/dist/scripts/scriptUtils.d.ts +15 -0
  83. package/dist/scripts/scriptUtils.js +83 -0
  84. package/dist/scripts/scriptUtils.js.map +1 -0
  85. package/dist/uxf/ExperiencePlugin.d.ts +22 -0
  86. package/dist/uxf/ExperiencePlugin.js +55 -0
  87. package/dist/uxf/ExperiencePlugin.js.map +1 -0
  88. package/dist/uxf/RoutesPlugin.d.ts +22 -0
  89. package/dist/uxf/RoutesPlugin.js +176 -0
  90. package/dist/uxf/RoutesPlugin.js.map +1 -0
  91. package/dist/uxf/UxfFormulaParser/cleanUxValue.d.ts +4 -0
  92. package/dist/uxf/UxfFormulaParser/cleanUxValue.js +65 -0
  93. package/dist/uxf/UxfFormulaParser/cleanUxValue.js.map +1 -0
  94. package/dist/uxf/UxfFormulaParser/grammerParser/api.d.ts +189 -0
  95. package/dist/uxf/UxfFormulaParser/grammerParser/api.js +158 -0
  96. package/dist/uxf/UxfFormulaParser/grammerParser/api.js.map +1 -0
  97. package/dist/uxf/UxfFormulaParser/grammerParser/clientTransformMap.d.ts +13 -0
  98. package/dist/uxf/UxfFormulaParser/grammerParser/clientTransformMap.js +604 -0
  99. package/dist/uxf/UxfFormulaParser/grammerParser/clientTransformMap.js.map +1 -0
  100. package/dist/uxf/UxfFormulaParser/grammerParser/grammarParser.d.ts +12 -0
  101. package/dist/uxf/UxfFormulaParser/grammerParser/grammarParser.js +551 -0
  102. package/dist/uxf/UxfFormulaParser/grammerParser/grammarParser.js.map +1 -0
  103. package/dist/uxf/UxfFormulaParser/grammerParser/spanHelpers.d.ts +31 -0
  104. package/dist/uxf/UxfFormulaParser/grammerParser/spanHelpers.js +64 -0
  105. package/dist/uxf/UxfFormulaParser/grammerParser/spanHelpers.js.map +1 -0
  106. package/dist/uxf/UxfFormulaParser/index.d.ts +3 -0
  107. package/dist/uxf/UxfFormulaParser/index.js +11 -0
  108. package/dist/uxf/UxfFormulaParser/index.js.map +1 -0
  109. package/dist/uxf/UxfFormulaParser/parser.d.ts +8 -0
  110. package/dist/uxf/UxfFormulaParser/parser.js +87 -0
  111. package/dist/uxf/UxfFormulaParser/parser.js.map +1 -0
  112. package/dist/uxf/UxfFormulaParser/utils/getErrorMsg.d.ts +8 -0
  113. package/dist/uxf/UxfFormulaParser/utils/getErrorMsg.js +17 -0
  114. package/dist/uxf/UxfFormulaParser/utils/getErrorMsg.js.map +1 -0
  115. package/dist/uxf/constants.d.ts +2 -0
  116. package/dist/uxf/constants.js +8 -0
  117. package/dist/uxf/constants.js.map +1 -0
  118. package/dist/uxf/index.d.ts +2 -0
  119. package/dist/uxf/index.js +11 -0
  120. package/dist/uxf/index.js.map +1 -0
  121. package/dist/uxf/tectonicIdGenerator.d.ts +12 -0
  122. package/dist/uxf/tectonicIdGenerator.js +102 -0
  123. package/dist/uxf/tectonicIdGenerator.js.map +1 -0
  124. package/license +9 -0
  125. package/package.json +42 -0
  126. package/src/AttachmentPlugin.ts +262 -0
  127. package/src/BusinessRulePlugin.ts +251 -0
  128. package/src/CrossScopePrivilegePlugin.ts +54 -0
  129. package/src/DefaultPlugin.ts +272 -0
  130. package/src/IdPlugin.ts +47 -0
  131. package/src/ListPlugin.ts +497 -0
  132. package/src/PropertyPlugin.ts +218 -0
  133. package/src/ScriptTemplatePlugin.ts +223 -0
  134. package/src/UserPreferencePlugin.ts +36 -0
  135. package/src/aclAndRole/AclPlugin.ts +410 -0
  136. package/src/aclAndRole/RolePlugin.ts +225 -0
  137. package/src/aclAndRole/Util.ts +104 -0
  138. package/src/app/ApplicationMenuPlugin.ts +158 -0
  139. package/src/atf/ATFComposer.ts +3356 -0
  140. package/src/atf/TestPlugin.ts +119 -0
  141. package/src/atf/index.ts +1 -0
  142. package/src/db/ColumnPlugins.ts +117 -0
  143. package/src/db/RecordPlugin.ts +391 -0
  144. package/src/db/TablePlugin.ts +1581 -0
  145. package/src/db/index.ts +3 -0
  146. package/src/index.ts +16 -0
  147. package/src/scriptedRESTAPI/RESTDeserializationUtils.ts +410 -0
  148. package/src/scriptedRESTAPI/RESTSerializationUtils.ts +227 -0
  149. package/src/scriptedRESTAPI/RestApiPlugin.ts +438 -0
  150. package/src/scriptedRESTAPI/RestSchemaUtils.ts +72 -0
  151. package/src/scriptedRESTAPI/RestUtils.ts +507 -0
  152. package/src/scripts/ClientScriptPlugin.ts +251 -0
  153. package/src/scripts/scriptUtils.ts +81 -0
  154. package/src/uxf/ExperiencePlugin.ts +64 -0
  155. package/src/uxf/RoutesPlugin.ts +215 -0
  156. package/src/uxf/UxfFormulaParser/cleanUxValue.ts +73 -0
  157. package/src/uxf/UxfFormulaParser/grammerParser/api.js +166 -0
  158. package/src/uxf/UxfFormulaParser/grammerParser/clientTransformMap.js +606 -0
  159. package/src/uxf/UxfFormulaParser/grammerParser/grammarParser.js +551 -0
  160. package/src/uxf/UxfFormulaParser/grammerParser/spanHelpers.js +65 -0
  161. package/src/uxf/UxfFormulaParser/index.ts +4 -0
  162. package/src/uxf/UxfFormulaParser/parser.ts +64 -0
  163. package/src/uxf/UxfFormulaParser/utils/getErrorMsg.ts +13 -0
  164. package/src/uxf/constants.ts +4 -0
  165. package/src/uxf/index.ts +2 -0
  166. package/src/uxf/tectonicIdGenerator.ts +81 -0
@@ -0,0 +1,262 @@
1
+ import {
2
+ Plugin,
3
+ extractCallExpression,
4
+ generateCallExpression,
5
+ getSysUpdateName,
6
+ getCallExpressionName,
7
+ getOrCreateEntitySourceFile,
8
+ linkDocument,
9
+ recordXml,
10
+ removeNode,
11
+ transformFunctionArguments,
12
+ unloadBuilder,
13
+ } from '@servicenow/sdk-build-core'
14
+ import * as zlib from 'node:zlib'
15
+ import * as path from 'path'
16
+ import * as crypto from 'crypto'
17
+ import { RecordXmlSchema } from './db/RecordPlugin'
18
+ import * as z from 'zod'
19
+ import { SysAttachment, SysAttachmentConfig } from '@servicenow/sdk-core/runtime/sys'
20
+ import { SourceFile } from 'ts-morph'
21
+ import { Diagnostic, FileSystem } from '@servicenow/sdk-project'
22
+
23
+ export const AttachmentPlugin = Plugin({
24
+ name: 'Attachment',
25
+ ownedTables: {
26
+ sys_attachment: { diagnosticLevel: Diagnostic.Level.Warn },
27
+ },
28
+ extractors: {
29
+ entity: {
30
+ CallExpression: (node, context) => {
31
+ return extractCallExpression(SysAttachment, 'attachment', node, context, (attachment) => attachment.id)
32
+ },
33
+ },
34
+ },
35
+
36
+ composers: {
37
+ entity: {
38
+ attachment(entity) {
39
+ return {
40
+ kind: 'attachment',
41
+ node: entity.getNode(),
42
+ guid: entity.getGuid(),
43
+ data: entity.getValue() as SysAttachmentConfig,
44
+ }
45
+ },
46
+ },
47
+
48
+ xml: {
49
+ record(xml) {
50
+ const parseResult = AttachmentXmlSchema.safeParse(xml.data['record_update'])
51
+ if (!parseResult.success) {
52
+ return undefined
53
+ }
54
+
55
+ const { sys_attachment, sys_attachment_doc } = parseResult.data
56
+
57
+ const data: AttachmentEntityData = {
58
+ attachment: sys_attachment,
59
+ attachmentDocs: Array.isArray(sys_attachment_doc) ? sys_attachment_doc : [sys_attachment_doc],
60
+ }
61
+
62
+ return {
63
+ action: sys_attachment['@_action'],
64
+ kind: 'attachment',
65
+ guid: sys_attachment.sys_id,
66
+ data,
67
+ }
68
+ },
69
+ },
70
+ },
71
+
72
+ generators: {
73
+ attachment(document, context) {
74
+ return linkDocument(
75
+ document,
76
+ generateCallExpression(
77
+ context,
78
+ getOrCreateEntitySourceFile(context, getSysUpdateName(document, 'sys_attachment')),
79
+ '@servicenow/sdk/core',
80
+ SysAttachment,
81
+ {}
82
+ )
83
+ )
84
+ },
85
+ },
86
+
87
+ transformers: {
88
+ attachment: {
89
+ CallExpression(document, context) {
90
+ const expressionName = getCallExpressionName(document.node)
91
+ if (expressionName !== SysAttachment.name) {
92
+ return false
93
+ }
94
+
95
+ if (document.action === 'DELETE') {
96
+ removeNode(document.node)
97
+ return true
98
+ }
99
+
100
+ if (document.action !== 'INSERT_OR_UPDATE') {
101
+ return false
102
+ }
103
+
104
+ const { attachment, attachmentDocs } = document.data as AttachmentEntityData
105
+
106
+ transformFunctionArguments(document.node, SysAttachment, {
107
+ id: document.guid,
108
+ fileName: attachment.file_name,
109
+ contentType: attachment.content_type,
110
+ filePath: `./${attachment.file_name}`,
111
+ tableName: attachment.table_name as SysAttachmentConfig['tableName'],
112
+ tableSysId: attachment.table_sys_id,
113
+ })
114
+
115
+ /**
116
+ * Attachments are gzip compressed and base64 encoded into multiple sys_attachment_doc chunks
117
+ * Rebuild the archive and decompress it to disk
118
+ */
119
+
120
+ const dataBuffer = Buffer.alloc(attachment.size_compressed)
121
+
122
+ let position = 0
123
+
124
+ attachmentDocs
125
+ .sort((a, b) => a.position - b.position)
126
+ .forEach((doc) => {
127
+ position += dataBuffer.write(doc.data, position, 'base64')
128
+ })
129
+
130
+ const filePath = getSourceFileDirectory(document.node.getSourceFile())
131
+
132
+ if (!FileSystem.existsSync(context.fs, filePath)) {
133
+ context.fs.mkdirSync(filePath, { recursive: true })
134
+ }
135
+
136
+ context.fs.writeFileSync(path.join(filePath, attachment.file_name), zlib.gunzipSync(dataBuffer), {
137
+ encoding: 'binary',
138
+ })
139
+
140
+ return true
141
+ },
142
+ },
143
+ },
144
+
145
+ serializers: {
146
+ attachment(document, context) {
147
+ /**
148
+ <unload>
149
+ <sys_attachment/>
150
+ <sys_attachment_doc/>
151
+ <sys_attachment_doc/>
152
+ </unload>
153
+ */
154
+ const data = document.data as SysAttachmentConfig
155
+
156
+ const xmlBuilder = unloadBuilder(context)
157
+
158
+ const CHUNK_SIZE = 4 * 1024
159
+
160
+ const attachmentEle = recordXml(xmlBuilder.xml, 'sys_attachment', document.guid)
161
+ attachmentEle.addSysScope(context)
162
+ attachmentEle.fields({
163
+ compressed: true,
164
+ content_type: data.contentType,
165
+ file_name: data.fileName,
166
+ table_name: data.tableName,
167
+ table_sys_id: data.tableSysId,
168
+ chunk_size: CHUNK_SIZE,
169
+ })
170
+
171
+ const directoryPath = getSourceFileDirectory(document.node!.getSourceFile())
172
+ const contentFilePath = path.join(directoryPath, data.filePath)
173
+ if (!FileSystem.existsSync(context.fs, contentFilePath)) {
174
+ throw new Error(`Attachment file not found for attachment ${document.guid}: ${contentFilePath}`)
175
+ }
176
+
177
+ // Compress the file with gzip, then break into chuncks and base64 encode for each sys_attachment_doc
178
+ const hash = crypto.createHash('md5')
179
+
180
+ const contents = context.fs.readFileSync(contentFilePath, { flag: 'r' })
181
+
182
+ attachmentEle.field('size_bites', contents.length)
183
+
184
+ const zipEncoded = zlib.gzipSync(contents)
185
+ attachmentEle.field('size_compressed', zipEncoded.length)
186
+
187
+ hash.update(zipEncoded)
188
+ attachmentEle.field('hash', hash.digest('hex'))
189
+
190
+ const dataArray = Uint8Array.from(zipEncoded)
191
+
192
+ let position = 0
193
+ let readOffset = position * CHUNK_SIZE
194
+ while (readOffset < dataArray.length) {
195
+ const dataChunk = dataArray.slice(readOffset, readOffset + CHUNK_SIZE)
196
+
197
+ const dataChunkBase64 = Buffer.from(dataChunk).toString('base64')
198
+ const attachmentDoc = recordXml(
199
+ xmlBuilder.xml,
200
+ 'sys_attachment_doc',
201
+ context.keys.registerCompositeId('sys_attachment_doc', {
202
+ attachment: document.guid,
203
+ data: dataChunkBase64,
204
+ })
205
+ )
206
+
207
+ attachmentDoc.fields({
208
+ data: dataChunkBase64,
209
+ length: dataChunkBase64.length,
210
+ position,
211
+ sys_attachment: document.guid,
212
+ })
213
+
214
+ position++
215
+
216
+ readOffset = position * CHUNK_SIZE
217
+ }
218
+
219
+ return {
220
+ name: `sys_attachment_${document.guid}.xml`,
221
+ directory: 'update',
222
+ content: xmlBuilder.end(),
223
+ }
224
+ },
225
+ },
226
+ })
227
+
228
+ function getSourceFileDirectory(sourceFile: SourceFile): string {
229
+ const parsedPath = path.parse(path.normalize(sourceFile.getFilePath()))
230
+ return parsedPath.dir
231
+ }
232
+
233
+ const TextStringSchema = z.object({ '#text': z.string() }).transform((val) => val['#text'])
234
+ const TextIntSchema = z.object({ '#text': z.number() }).transform((val) => val['#text'])
235
+
236
+ const SysAttachmentSchema = RecordXmlSchema.extend({
237
+ sys_id: TextStringSchema,
238
+ content_type: TextStringSchema,
239
+ file_name: TextStringSchema,
240
+ hash: TextStringSchema,
241
+ table_name: TextStringSchema,
242
+ table_sys_id: TextStringSchema,
243
+ size_bytes: TextIntSchema,
244
+ size_compressed: TextIntSchema,
245
+ })
246
+
247
+ const SysAttachmentDocSchema = RecordXmlSchema.extend({
248
+ data: TextStringSchema,
249
+ length: TextIntSchema,
250
+ position: TextIntSchema,
251
+ sys_attachment: TextStringSchema,
252
+ })
253
+
254
+ const AttachmentXmlSchema = z.object({
255
+ sys_attachment: SysAttachmentSchema,
256
+ sys_attachment_doc: z.union([z.array(SysAttachmentDocSchema), SysAttachmentDocSchema]),
257
+ })
258
+
259
+ type AttachmentEntityData = {
260
+ attachment: z.infer<typeof SysAttachmentSchema>
261
+ attachmentDocs: Array<z.infer<typeof SysAttachmentDocSchema>>
262
+ }
@@ -0,0 +1,251 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import {
3
+ BusinessRule,
4
+ BusinessRuleSchemaInc,
5
+ getRolesString,
6
+ getRolesArray,
7
+ BusinessRuleSchema,
8
+ } from '@servicenow/sdk-core/runtime/app'
9
+ import {
10
+ Plugin,
11
+ generateCallExpressionExportForDocument,
12
+ getSysUpdateName,
13
+ getCallExpressionName,
14
+ getOrCreateEntitySourceFile,
15
+ linkDocument,
16
+ removeNode,
17
+ stringify,
18
+ transformFunctionArguments,
19
+ writeArrayPropertyAsReference,
20
+ writeCustomProperty,
21
+ getOrCreatePropertyAssignment,
22
+ EntityData,
23
+ ObjectData,
24
+ } from '@servicenow/sdk-build-core'
25
+ import { processScript } from './ScriptTemplatePlugin'
26
+ import { scriptInfo, type ScriptInfo, buildScriptImport } from './scripts/scriptUtils'
27
+ import { extractCallExpressionAsRecord } from './db/RecordPlugin'
28
+ import { merge } from 'lodash'
29
+ import { SyntaxKind, Node } from 'ts-morph'
30
+ import * as z from 'zod'
31
+ import { Diagnostic } from '@servicenow/sdk-project'
32
+ import { Record, TableName } from '@servicenow/sdk-core/runtime/db'
33
+
34
+ function businessRuleAsRecord<const T extends TableName>(config: BusinessRule<T>) {
35
+ const br = BusinessRuleSchema.parse(config)
36
+ return Record({
37
+ $id: br.$id,
38
+ table: 'sys_script',
39
+ data: {
40
+ name: br.name,
41
+ collection: br.table,
42
+ script: br.script,
43
+ order: br.order ?? 100,
44
+ action_insert: br?.action?.includes('insert') ?? false,
45
+ action_update: br?.action?.includes('update') ?? false,
46
+ action_query: br?.action?.includes('query') ?? false,
47
+ action_delete: br?.action?.includes('delete') ?? false,
48
+ active: br.active ?? true,
49
+ when: br.when ?? 'before',
50
+ message: br.message!,
51
+ add_message: br.add_message ?? br.message ? true : false,
52
+ condition: br.condition,
53
+ filter_condition: br.filter_condition,
54
+ template: br.set_field_value,
55
+ description: br.description,
56
+ },
57
+ })
58
+ }
59
+
60
+ export default Plugin({
61
+ name: 'BusinessRule',
62
+ ownedTables: {
63
+ sys_script: { diagnosticLevel: Diagnostic.Level.Warn },
64
+ },
65
+ extractors: {
66
+ entity: {
67
+ CallExpression(node, context) {
68
+ const result = extractCallExpressionAsRecord(
69
+ BusinessRule,
70
+ businessRuleAsRecord,
71
+ (br) => context.registerExplicitId('sys_script', br.$id as string),
72
+ node,
73
+ context
74
+ )
75
+
76
+ if (!result.handled || !(0 in result.data)) {
77
+ return result
78
+ }
79
+
80
+ const record = result.data[0]
81
+ const recordData = record.getProperty('data').getValue()
82
+ const scriptData = recordData.script as unknown as ScriptInfo
83
+ const role_conditions = recordData.role_conditions as unknown as any[]
84
+ let scriptVal: string
85
+ if (scriptData && scriptData.filePath) {
86
+ const { filePath, functionName, isDefault } = scriptData
87
+ scriptVal = buildScriptImport(
88
+ filePath,
89
+ functionName,
90
+ isDefault,
91
+ context,
92
+ (funcName: string) => `${funcName}(current, previous)\n`
93
+ )
94
+ } else {
95
+ scriptVal = recordData.script as unknown as string
96
+ }
97
+
98
+ return {
99
+ handled: true,
100
+ diagnostics: result.diagnostics,
101
+ data: [
102
+ new EntityData(
103
+ record.getKind(),
104
+ record.getGuid(),
105
+ ObjectData.fromObjectValue(
106
+ merge(record.getValue(), {
107
+ data: {
108
+ script: scriptVal,
109
+ role_conditions: role_conditions ? getRolesString(role_conditions) : '',
110
+ },
111
+ }),
112
+ record.getNode()
113
+ ),
114
+ record.getNode()
115
+ ),
116
+ ],
117
+ }
118
+ },
119
+ },
120
+ raw: {
121
+ FunctionDeclaration(node, context) {
122
+ const info = scriptInfo(node, context, BusinessRule.name)
123
+ if (!info) {
124
+ return { handled: false }
125
+ }
126
+
127
+ return { handled: true, diagnostics: [], data: [info] }
128
+ },
129
+
130
+ FunctionExpression(node, context) {
131
+ const info = scriptInfo(node, context, BusinessRule.name)
132
+ if (!info) {
133
+ return { handled: false }
134
+ }
135
+
136
+ return { handled: true, diagnostics: [], data: [info] }
137
+ },
138
+ },
139
+ },
140
+ generators: {
141
+ record(document, context) {
142
+ if (!BusinessRuleSchemaInc.safeParse(document.data!['data']).success) {
143
+ return undefined
144
+ }
145
+
146
+ return linkDocument(
147
+ document,
148
+ generateCallExpressionExportForDocument(
149
+ context,
150
+ {
151
+ sourceFile: getOrCreateEntitySourceFile(context, getSysUpdateName(document, 'sys_script')),
152
+ moduleSpecifier: '@servicenow/sdk/core',
153
+ },
154
+ BusinessRule,
155
+ { $id: document.guid }
156
+ ).getExpressionIfKindOrThrow(SyntaxKind.CallExpression)
157
+ )
158
+ },
159
+ },
160
+ transformers: {
161
+ record: {
162
+ CallExpression(document, context) {
163
+ if (getCallExpressionName(document.node) !== BusinessRule.name) {
164
+ return false
165
+ }
166
+
167
+ if (document.action === 'DELETE') {
168
+ removeNode(document.node)
169
+ return true
170
+ }
171
+
172
+ const modified_properties = BusinessRuleSchemaInc.deepPartial().safeParse(
173
+ (document.changedData as any).data
174
+ )
175
+ const merged_properties = BusinessRuleSchemaInc.safeParse((document.data as any).data)
176
+ const incoming_properties = BusinessRuleSchemaInc.safeParse((document.xmlData as any).data)
177
+ if (!modified_properties.success || !merged_properties.success || !incoming_properties.success) {
178
+ return false
179
+ }
180
+
181
+ const [args] = document.node.getArguments()
182
+ if (!Node.isObjectLiteralExpression(args)) {
183
+ return false
184
+ }
185
+ const {
186
+ role_conditions: roles,
187
+ filter_condition: filters,
188
+ collection,
189
+ script,
190
+ template,
191
+ action_delete,
192
+ action_insert,
193
+ action_query,
194
+ action_update,
195
+ ...rest
196
+ } = modified_properties.data
197
+
198
+ const xml_actions = getActionsArray(incoming_properties.data)
199
+ if (xml_actions.length > 0 || getActionsArray(merged_properties.data).length > 0) {
200
+ writeCustomProperty(args, 'action', '[]', stringify(xml_actions))
201
+ } else {
202
+ const actionsProp = getOrCreatePropertyAssignment(args, 'action', '[]')
203
+ actionsProp.remove()
204
+ }
205
+
206
+ if (roles) {
207
+ writeArrayPropertyAsReference(args, 'role_conditions', '[]', getRolesArray(roles))
208
+ }
209
+
210
+ if (filters) {
211
+ writeCustomProperty(args, 'filter_condition', '', `\`${filters}\``)
212
+ }
213
+
214
+ if (script !== undefined) {
215
+ processScript(args, 'script', script, document.guid, 'sys_script', context)
216
+ }
217
+
218
+ if (template) {
219
+ writeCustomProperty(args, 'set_field_value', '', stringify(template))
220
+ }
221
+
222
+ const reqSchema = BusinessRuleSchemaInc.required()
223
+ const result = transformFunctionArguments(document.node, BusinessRule, {
224
+ table: collection,
225
+ ...(rest as Partial<z.infer<typeof reqSchema>>),
226
+ })
227
+ return result
228
+ },
229
+ },
230
+ },
231
+ })
232
+
233
+ function getActionsArray(data) {
234
+ const actions: BR_ACTIONS[] = []
235
+ if (data.action_update) {
236
+ actions.push('update')
237
+ }
238
+ if (data.action_delete) {
239
+ actions.push('delete')
240
+ }
241
+ if (data.action_insert) {
242
+ actions.push('insert')
243
+ }
244
+ if (data.action_query) {
245
+ actions.push('query')
246
+ }
247
+
248
+ return actions
249
+ }
250
+
251
+ type BR_ACTIONS = 'insert' | 'update' | 'delete' | 'query'
@@ -0,0 +1,54 @@
1
+ import { CrossScopePrivilege, CrossScopePrivilegeSchema } from '@servicenow/sdk-core/runtime/app'
2
+ import { EntityData, extractCallExpression, ObjectData, Plugin } from '@servicenow/sdk-build-core'
3
+ import { RecordPlugin } from './db/RecordPlugin'
4
+ import { Diagnostic } from '@servicenow/sdk-project'
5
+ import { Record } from '@servicenow/sdk-core/runtime/db'
6
+
7
+ export default Plugin({
8
+ name: 'CrossScopePrivilege',
9
+ ownedTables: {
10
+ sys_scope_privilege: { diagnosticLevel: Diagnostic.Level.Warn },
11
+ },
12
+ extractors: {
13
+ entity: {
14
+ CallExpression: (node, context) =>
15
+ extractCallExpression(CrossScopePrivilege, 'sys_scope_privilege', node, context, (privilege) =>
16
+ context.registerExplicitId('sys_scope_privilege', privilege.$id as string)
17
+ ),
18
+ },
19
+ },
20
+ composers: {
21
+ entity: {
22
+ sys_scope_privilege(entity, context) {
23
+ const scopeId = context.app.config.scopeId
24
+ const privilege = CrossScopePrivilegeSchema.safeParse(entity.getValue())
25
+ if (!privilege.success) {
26
+ return undefined
27
+ }
28
+
29
+ const { $id, ...rest } = privilege.data
30
+
31
+ const privilegeRecord = Record({
32
+ table: 'sys_scope_privilege',
33
+ $id,
34
+ data: {
35
+ source_scope: scopeId,
36
+ ...rest,
37
+ },
38
+ })
39
+
40
+ return context.composeEntities(
41
+ [
42
+ new EntityData(
43
+ 'record',
44
+ entity.getGuid(),
45
+ ObjectData.fromObjectValue(privilegeRecord, entity.getNode()),
46
+ entity.getNode()
47
+ ),
48
+ ],
49
+ [RecordPlugin]
50
+ )
51
+ },
52
+ },
53
+ },
54
+ })