@servicenow/sdk-build-core 3.0.3 → 4.0.0

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 (252) hide show
  1. package/dist/app.d.ts +25 -0
  2. package/dist/app.js +8 -0
  3. package/dist/app.js.map +1 -0
  4. package/dist/compiler.d.ts +60 -0
  5. package/dist/compiler.js +320 -0
  6. package/dist/compiler.js.map +1 -0
  7. package/dist/compression.d.ts +7 -0
  8. package/dist/compression.js +79 -0
  9. package/dist/compression.js.map +1 -0
  10. package/dist/crypto.d.ts +1 -0
  11. package/dist/crypto.js +9 -0
  12. package/dist/crypto.js.map +1 -0
  13. package/dist/diagnostic.d.ts +41 -0
  14. package/dist/diagnostic.js +130 -0
  15. package/dist/diagnostic.js.map +1 -0
  16. package/dist/{plugins/Diagnostic.d.ts → fluent-diagnostic.d.ts} +3 -2
  17. package/dist/fluent-diagnostic.js +23 -0
  18. package/dist/fluent-diagnostic.js.map +1 -0
  19. package/dist/fluent-directive.d.ts +8 -0
  20. package/dist/fluent-directive.js +54 -0
  21. package/dist/fluent-directive.js.map +1 -0
  22. package/dist/fluent-file.d.ts +5 -0
  23. package/dist/fluent-file.js +15 -0
  24. package/dist/fluent-file.js.map +1 -0
  25. package/dist/formatter.d.ts +11 -0
  26. package/dist/formatter.js +77 -0
  27. package/dist/formatter.js.map +1 -0
  28. package/dist/fs.d.ts +174 -0
  29. package/dist/fs.js +313 -0
  30. package/dist/fs.js.map +1 -0
  31. package/dist/guid.d.ts +2 -0
  32. package/dist/{GUID.js → guid.js} +3 -6
  33. package/dist/guid.js.map +1 -0
  34. package/dist/index.d.ts +19 -5
  35. package/dist/index.js +19 -5
  36. package/dist/index.js.map +1 -1
  37. package/dist/json.d.ts +5 -0
  38. package/dist/json.js +43 -0
  39. package/dist/json.js.map +1 -0
  40. package/dist/keys-registry.d.ts +64 -0
  41. package/dist/keys-registry.js +339 -0
  42. package/dist/keys-registry.js.map +1 -0
  43. package/dist/logger.d.ts +8 -0
  44. package/dist/logger.js +17 -0
  45. package/dist/logger.js.map +1 -0
  46. package/dist/now-config.d.ts +348 -0
  47. package/dist/now-config.js +283 -0
  48. package/dist/now-config.js.map +1 -0
  49. package/dist/path.d.ts +3 -0
  50. package/dist/path.js +12 -0
  51. package/dist/path.js.map +1 -0
  52. package/dist/plugins/cache.d.ts +20 -0
  53. package/dist/plugins/cache.js +46 -0
  54. package/dist/plugins/cache.js.map +1 -0
  55. package/dist/plugins/context.d.ts +85 -0
  56. package/dist/plugins/{Context.js → context.js} +1 -1
  57. package/dist/plugins/context.js.map +1 -0
  58. package/dist/plugins/database.d.ts +27 -0
  59. package/dist/plugins/database.js +102 -0
  60. package/dist/plugins/database.js.map +1 -0
  61. package/dist/plugins/file.d.ts +10 -0
  62. package/dist/plugins/{behaviors/Arranger.js → file.js} +1 -1
  63. package/dist/plugins/file.js.map +1 -0
  64. package/dist/plugins/index.d.ts +9 -5
  65. package/dist/plugins/index.js +9 -6
  66. package/dist/plugins/index.js.map +1 -1
  67. package/dist/plugins/plugin.d.ts +478 -0
  68. package/dist/plugins/plugin.js +533 -0
  69. package/dist/plugins/plugin.js.map +1 -0
  70. package/dist/plugins/product.d.ts +15 -0
  71. package/dist/plugins/product.js +38 -0
  72. package/dist/plugins/product.js.map +1 -0
  73. package/dist/plugins/project.d.ts +25 -0
  74. package/dist/plugins/{behaviors/Generator.js → project.js} +1 -1
  75. package/dist/plugins/project.js.map +1 -0
  76. package/dist/plugins/shape.d.ts +424 -0
  77. package/dist/plugins/shape.js +1181 -0
  78. package/dist/plugins/shape.js.map +1 -0
  79. package/dist/plugins/time.d.ts +12 -0
  80. package/dist/plugins/time.js +84 -0
  81. package/dist/plugins/time.js.map +1 -0
  82. package/dist/plugins/usage.d.ts +11 -0
  83. package/dist/plugins/usage.js +26 -0
  84. package/dist/plugins/usage.js.map +1 -0
  85. package/dist/prettier/config-loader.d.ts +13 -0
  86. package/dist/prettier/config-loader.js +105 -0
  87. package/dist/prettier/config-loader.js.map +1 -0
  88. package/dist/telemetry/index.d.ts +25 -0
  89. package/dist/telemetry/index.js +18 -0
  90. package/dist/telemetry/index.js.map +1 -0
  91. package/dist/typescript.d.ts +293 -0
  92. package/dist/typescript.js +454 -0
  93. package/dist/typescript.js.map +1 -0
  94. package/dist/util/get-file-type.d.ts +2 -0
  95. package/dist/util/get-file-type.js +13 -0
  96. package/dist/util/get-file-type.js.map +1 -0
  97. package/dist/util/index.d.ts +2 -6
  98. package/dist/util/index.js +2 -6
  99. package/dist/util/index.js.map +1 -1
  100. package/dist/util/{Scope.js → is-sn-scope.js} +1 -1
  101. package/dist/util/is-sn-scope.js.map +1 -0
  102. package/dist/xml.d.ts +24 -0
  103. package/dist/xml.js +71 -0
  104. package/dist/xml.js.map +1 -0
  105. package/now.config.schema.json +336 -0
  106. package/package.json +22 -12
  107. package/src/app.ts +33 -0
  108. package/src/compiler.ts +384 -0
  109. package/src/compression.ts +93 -0
  110. package/src/crypto.ts +5 -0
  111. package/src/diagnostic.ts +108 -0
  112. package/src/{plugins/Diagnostic.ts → fluent-diagnostic.ts} +3 -10
  113. package/src/fluent-directive.ts +63 -0
  114. package/src/fluent-file.ts +13 -0
  115. package/src/formatter.ts +58 -0
  116. package/src/fs.ts +438 -0
  117. package/src/{GUID.ts → guid.ts} +2 -6
  118. package/src/index.ts +19 -5
  119. package/src/json.ts +20 -0
  120. package/src/keys-registry.ts +384 -0
  121. package/src/logger.ts +20 -0
  122. package/src/now-config.ts +337 -0
  123. package/src/path.ts +9 -0
  124. package/src/plugins/cache.ts +45 -0
  125. package/src/plugins/context.ts +93 -0
  126. package/src/plugins/database.ts +121 -0
  127. package/src/plugins/file.ts +19 -0
  128. package/src/plugins/index.ts +9 -5
  129. package/src/plugins/plugin.ts +995 -0
  130. package/src/plugins/product.ts +44 -0
  131. package/src/plugins/project.ts +39 -0
  132. package/src/plugins/shape.ts +1532 -0
  133. package/src/plugins/time.ts +108 -0
  134. package/src/plugins/usage.ts +26 -0
  135. package/src/prettier/config-loader.ts +130 -0
  136. package/src/telemetry/index.ts +27 -0
  137. package/src/typescript.ts +502 -0
  138. package/src/util/get-file-type.ts +11 -0
  139. package/src/util/index.ts +2 -6
  140. package/src/xml.ts +86 -0
  141. package/dist/GUID.d.ts +0 -2
  142. package/dist/GUID.js.map +0 -1
  143. package/dist/IncludePaths.d.ts +0 -25
  144. package/dist/IncludePaths.js +0 -97
  145. package/dist/IncludePaths.js.map +0 -1
  146. package/dist/Keys.d.ts +0 -32
  147. package/dist/Keys.js +0 -245
  148. package/dist/Keys.js.map +0 -1
  149. package/dist/TypeScript.d.ts +0 -5
  150. package/dist/TypeScript.js +0 -58
  151. package/dist/TypeScript.js.map +0 -1
  152. package/dist/XML.d.ts +0 -32
  153. package/dist/XML.js +0 -83
  154. package/dist/XML.js.map +0 -1
  155. package/dist/plugins/Context.d.ts +0 -190
  156. package/dist/plugins/Context.js.map +0 -1
  157. package/dist/plugins/Diagnostic.js +0 -28
  158. package/dist/plugins/Diagnostic.js.map +0 -1
  159. package/dist/plugins/Plugin.d.ts +0 -175
  160. package/dist/plugins/Plugin.js +0 -15
  161. package/dist/plugins/Plugin.js.map +0 -1
  162. package/dist/plugins/behaviors/Arranger.d.ts +0 -26
  163. package/dist/plugins/behaviors/Arranger.js.map +0 -1
  164. package/dist/plugins/behaviors/Composer.d.ts +0 -102
  165. package/dist/plugins/behaviors/Composer.js +0 -15
  166. package/dist/plugins/behaviors/Composer.js.map +0 -1
  167. package/dist/plugins/behaviors/Diagnostics.d.ts +0 -7
  168. package/dist/plugins/behaviors/Diagnostics.js +0 -3
  169. package/dist/plugins/behaviors/Diagnostics.js.map +0 -1
  170. package/dist/plugins/behaviors/Generator.d.ts +0 -21
  171. package/dist/plugins/behaviors/Generator.js.map +0 -1
  172. package/dist/plugins/behaviors/OwnedTables.d.ts +0 -6
  173. package/dist/plugins/behaviors/OwnedTables.js +0 -3
  174. package/dist/plugins/behaviors/OwnedTables.js.map +0 -1
  175. package/dist/plugins/behaviors/PostProcessor.d.ts +0 -5
  176. package/dist/plugins/behaviors/PostProcessor.js +0 -3
  177. package/dist/plugins/behaviors/PostProcessor.js.map +0 -1
  178. package/dist/plugins/behaviors/Serializer.d.ts +0 -30
  179. package/dist/plugins/behaviors/Serializer.js +0 -3
  180. package/dist/plugins/behaviors/Serializer.js.map +0 -1
  181. package/dist/plugins/behaviors/Transformer.d.ts +0 -23
  182. package/dist/plugins/behaviors/Transformer.js +0 -3
  183. package/dist/plugins/behaviors/Transformer.js.map +0 -1
  184. package/dist/plugins/behaviors/extractors/Data.d.ts +0 -119
  185. package/dist/plugins/behaviors/extractors/Data.js +0 -244
  186. package/dist/plugins/behaviors/extractors/Data.js.map +0 -1
  187. package/dist/plugins/behaviors/extractors/Extractors.d.ts +0 -63
  188. package/dist/plugins/behaviors/extractors/Extractors.js +0 -3
  189. package/dist/plugins/behaviors/extractors/Extractors.js.map +0 -1
  190. package/dist/plugins/behaviors/extractors/index.d.ts +0 -2
  191. package/dist/plugins/behaviors/extractors/index.js +0 -19
  192. package/dist/plugins/behaviors/extractors/index.js.map +0 -1
  193. package/dist/plugins/behaviors/index.d.ts +0 -9
  194. package/dist/plugins/behaviors/index.js +0 -26
  195. package/dist/plugins/behaviors/index.js.map +0 -1
  196. package/dist/plugins/util/CallExpression.d.ts +0 -5
  197. package/dist/plugins/util/CallExpression.js +0 -88
  198. package/dist/plugins/util/CallExpression.js.map +0 -1
  199. package/dist/plugins/util/CodeTransformation.d.ts +0 -95
  200. package/dist/plugins/util/CodeTransformation.js +0 -624
  201. package/dist/plugins/util/CodeTransformation.js.map +0 -1
  202. package/dist/plugins/util/ObjectLiteral.d.ts +0 -9
  203. package/dist/plugins/util/ObjectLiteral.js +0 -37
  204. package/dist/plugins/util/ObjectLiteral.js.map +0 -1
  205. package/dist/plugins/util/index.d.ts +0 -3
  206. package/dist/plugins/util/index.js +0 -20
  207. package/dist/plugins/util/index.js.map +0 -1
  208. package/dist/util/Debug.d.ts +0 -4
  209. package/dist/util/Debug.js +0 -20
  210. package/dist/util/Debug.js.map +0 -1
  211. package/dist/util/Directive.d.ts +0 -16
  212. package/dist/util/Directive.js +0 -107
  213. package/dist/util/Directive.js.map +0 -1
  214. package/dist/util/RuntimeTableSchema.d.ts +0 -5
  215. package/dist/util/RuntimeTableSchema.js +0 -58
  216. package/dist/util/RuntimeTableSchema.js.map +0 -1
  217. package/dist/util/Scope.js.map +0 -1
  218. package/dist/util/Util.d.ts +0 -1
  219. package/dist/util/Util.js +0 -12
  220. package/dist/util/Util.js.map +0 -1
  221. package/dist/util/XMLUploadParser.d.ts +0 -22
  222. package/dist/util/XMLUploadParser.js +0 -67
  223. package/dist/util/XMLUploadParser.js.map +0 -1
  224. package/src/IncludePaths.ts +0 -122
  225. package/src/Keys.ts +0 -274
  226. package/src/TypeScript.ts +0 -65
  227. package/src/XML.ts +0 -98
  228. package/src/plugins/Context.ts +0 -239
  229. package/src/plugins/Plugin.ts +0 -278
  230. package/src/plugins/behaviors/Arranger.ts +0 -42
  231. package/src/plugins/behaviors/Composer.ts +0 -125
  232. package/src/plugins/behaviors/Diagnostics.ts +0 -12
  233. package/src/plugins/behaviors/Generator.ts +0 -31
  234. package/src/plugins/behaviors/OwnedTables.ts +0 -5
  235. package/src/plugins/behaviors/PostProcessor.ts +0 -6
  236. package/src/plugins/behaviors/Serializer.ts +0 -40
  237. package/src/plugins/behaviors/Transformer.ts +0 -32
  238. package/src/plugins/behaviors/extractors/Data.ts +0 -332
  239. package/src/plugins/behaviors/extractors/Extractors.ts +0 -73
  240. package/src/plugins/behaviors/extractors/index.ts +0 -2
  241. package/src/plugins/behaviors/index.ts +0 -9
  242. package/src/plugins/util/CallExpression.ts +0 -110
  243. package/src/plugins/util/CodeTransformation.ts +0 -731
  244. package/src/plugins/util/ObjectLiteral.ts +0 -37
  245. package/src/plugins/util/index.ts +0 -3
  246. package/src/util/Debug.ts +0 -24
  247. package/src/util/Directive.ts +0 -123
  248. package/src/util/RuntimeTableSchema.ts +0 -44
  249. package/src/util/Util.ts +0 -7
  250. package/src/util/XMLUploadParser.ts +0 -90
  251. /package/dist/util/{Scope.d.ts → is-sn-scope.d.ts} +0 -0
  252. /package/src/util/{Scope.ts → is-sn-scope.ts} +0 -0
package/src/Keys.ts DELETED
@@ -1,274 +0,0 @@
1
- import { path, ts } from '@servicenow/sdk-project'
2
- import { parseType } from './TypeScript'
3
- import { GUID, isGUID } from './GUID'
4
- import * as z from 'zod'
5
- import * as _ from 'lodash'
6
- import { Context } from './plugins'
7
-
8
- export const KEYS_MODULE = '@servicenow/sdk/global'
9
- export const KEYS_FILE_NAME = 'keys.ts'
10
-
11
- const KeysSchema = z.object({
12
- explicit: z
13
- .record(
14
- z.string().or(z.number()),
15
- z.object({
16
- table: z.string(),
17
- id: z.string(),
18
- })
19
- )
20
- .default({}),
21
- composite: z
22
- .array(
23
- z.object({
24
- table: z.string(),
25
- id: z.string(),
26
- key: z.record(z.string(), z.string().or(z.number())),
27
- })
28
- )
29
- .default([]),
30
- deleted: z.record(z.array(z.string())).default({}),
31
- })
32
-
33
- export class Keys implements Now.Internal.KeysRegistry {
34
- private readonly file: ts.SourceFile
35
- private readonly keys: Now.Internal.KeysRegistry
36
- private readonly usedIds: Set<string> = new Set()
37
-
38
- constructor(context: Context) {
39
- this.file = Keys.getKeysFile(context)
40
- this.keys = Keys.parseKeysFile(this.file)
41
- }
42
-
43
- private static parseKeysFile(keysFile: ts.SourceFile): Now.Internal.KeysRegistry {
44
- const keysInterface = Keys.getOrCreateKeysInterface(keysFile)
45
- return KeysSchema.parse(
46
- parseType(keysInterface.getType(), keysInterface, (unparsableType) => {
47
- if (unparsableType.isArray()) {
48
- return [] // If the keys file doesn't have composite keys, it will be an unparsable array so just return an empty one
49
- } else {
50
- throw `Unparsable type in keys.ts: ${unparsableType.getText()}`
51
- }
52
- })
53
- )
54
- }
55
-
56
- private static getKeysFile(context: Context): ts.SourceFile {
57
- const keysFilePath = Keys.getKeysFilePath(context)
58
- const keysFile =
59
- context.compiler.addSourceFileAtPathIfExists(keysFilePath) ??
60
- context.compiler.createSourceFile(keysFilePath, '', {
61
- overwrite: true,
62
- scriptKind: ts.ScriptKind.TS,
63
- })
64
-
65
- // Add global import if not already present
66
- if (!keysFile.getImportDeclaration((i) => i.getModuleSpecifierValue() === KEYS_MODULE)) {
67
- keysFile.addImportDeclaration({ moduleSpecifier: KEYS_MODULE })
68
- }
69
-
70
- return keysFile
71
- }
72
-
73
- public static getKeysFilePath(context: Context) {
74
- return path.resolve(context.app.rootDir, context.app.config.generatedDir, KEYS_FILE_NAME)
75
- }
76
-
77
- private static getOrCreateKeysInterface(keysFile: ts.SourceFile) {
78
- const declareGlobal =
79
- keysFile.getModule('global') ??
80
- keysFile.addModule({
81
- name: 'global',
82
- declarationKind: ts.ModuleDeclarationKind.Global,
83
- hasDeclareKeyword: true,
84
- })
85
-
86
- const nowNamespace =
87
- declareGlobal.getModule('Now') ??
88
- declareGlobal.addModule({
89
- name: 'Now',
90
- declarationKind: ts.ModuleDeclarationKind.Namespace,
91
- })
92
-
93
- const internalNamespace =
94
- nowNamespace.getModule('Internal') ??
95
- nowNamespace.addModule({
96
- name: 'Internal',
97
- })
98
-
99
- return (
100
- internalNamespace.getInterface('Keys') ??
101
- internalNamespace.addInterface({
102
- name: 'Keys',
103
- extends: (writer) => {
104
- writer.write('KeysRegistry')
105
- },
106
- })
107
- )
108
- }
109
-
110
- private use(id: string) {
111
- this.usedIds.add(id)
112
- return id
113
- }
114
-
115
- get explicit() {
116
- return this.keys.explicit
117
- }
118
-
119
- get composite() {
120
- return this.keys.composite
121
- }
122
-
123
- get deleted() {
124
- return this.keys.deleted
125
- }
126
-
127
- getSourceFile(): ts.SourceFile {
128
- return this.file
129
- }
130
-
131
- getNextAvailableExplicitKey() {
132
- for (let i = 0; ; i++) {
133
- const key = `generated${i}`
134
- if (!this.explicit[key]) {
135
- return key
136
- }
137
- }
138
- }
139
-
140
- explicitKeyExists(key: string | number) {
141
- return !!this.explicit[key]
142
- }
143
-
144
- getIdUsingExplicitKey(key: string | number) {
145
- return this.explicit[key]?.id
146
- }
147
-
148
- findExplicitKeyById(id: string) {
149
- return Object.entries(this.explicit).find(([, v]) => v.id === id)?.[0]
150
- }
151
-
152
- registerExplicitId(table: string, key: string | number, sysIdOverride?: string): string {
153
- if (isGUID(key)) {
154
- // WARNING:
155
- //
156
- // IT IS CRITICAL THAT KEYS ARE NOT REGISTERED FOR THINGS OUTSIDE OF THE APP. IF KEYS ARE
157
- // REGISTERED FOR THINGS THAT COME FROM THE CORE PLATFORM OR SOME OTHER APP, THERE IS RISK
158
- // THAT THEY WILL BE DELETED ACCIDENTALLY BY THE CUSTOMER.
159
- //
160
- // THIS CHECK IS HERE AS AN EXTRA DEFENSIVE MEASURE TO MAKE SURE EXTERNAL SYS IDS ARE NOT
161
- // ACCIDENTALLY REGISTERED AS LOCAL KEYS.
162
- //
163
- // TODO: We need to come up with a different system for key registration where external
164
- // keys can be resolved to sys IDs without registering them as local keys.
165
- return this.use(this.explicit[key]?.id ?? key)
166
- }
167
-
168
- const existing = this.explicit[key]
169
- if (existing) {
170
- return this.use(existing.id)
171
- }
172
-
173
- if (sysIdOverride) {
174
- const existingKey = this.findExplicitKeyById(sysIdOverride)
175
- if (existingKey) {
176
- const existingWithSysId = this.explicit[existingKey]!
177
- if (existingKey !== (key as string | number)) {
178
- throw `ID '${sysIdOverride}' is already registered for key '${existingKey}', cannot register for key '${key}'`
179
- }
180
-
181
- return this.use(existingWithSysId.id)
182
- }
183
- }
184
-
185
- const newKey = {
186
- table,
187
- id: isGUID(key) ? (key as string) : sysIdOverride ?? GUID(),
188
- }
189
-
190
- this.explicit[key] = newKey
191
- return this.use(newKey.id)
192
- }
193
-
194
- registerCompositeId(table: string, key: Record<string, string | number>, sysIdOverride?: string): string {
195
- const existing = this.composite.find((k) => k.table === table && _.isEqual(k.key, key))
196
- if (existing) {
197
- if (existing.table !== table) {
198
- throw `Composite key ${key} is already registered for table '${existing.table}', cannot register for table '${table}'`
199
- }
200
-
201
- return this.use(existing.id)
202
- }
203
-
204
- const newKey = {
205
- table,
206
- id: sysIdOverride ?? GUID(),
207
- key,
208
- }
209
-
210
- this.composite.push(newKey)
211
- return this.use(newKey.id)
212
- }
213
-
214
- getUsedExplicitIds() {
215
- return Object.fromEntries(Object.entries(this.explicit).filter(([, v]) => this.usedIds.has(v.id)))
216
- }
217
-
218
- getUsedCompositeIds() {
219
- return this.composite.filter((v) => this.usedIds.has(v.id))
220
- }
221
-
222
- // TODO: This is called in multiple places and is not very cheap. We should consider optimization options.
223
- getDeletedAndUnusedIds() {
224
- return [
225
- ...Object.values(this.explicit),
226
- ...this.composite,
227
- ...Object.entries(this.deleted).flatMap(([k, v]) => v.map((id) => ({ table: k, id }))),
228
- ]
229
- .filter((v) => !this.usedIds.has(v.id))
230
- .reduce(
231
- (deletedIds, v) => {
232
- deletedIds[v.table] = [...(deletedIds[v.table] ?? []), v.id]
233
- return deletedIds
234
- },
235
- {} as Record<string, string[]>
236
- )
237
- }
238
-
239
- async format(formatter?: (file: ts.SourceFile) => Promise<any>) {
240
- await formatter?.(this.file)
241
- }
242
-
243
- updateSourceFile() {
244
- const newProperties: ts.OptionalKind<ts.PropertySignatureStructure>[] = []
245
- const usedExplicitIds = this.getUsedExplicitIds()
246
- const usedCompositeIds = this.getUsedCompositeIds()
247
- const deletedOrUnusedIds = this.getDeletedAndUnusedIds()
248
-
249
- if (Object.keys(usedExplicitIds).length > 0) {
250
- newProperties.push({
251
- name: 'explicit',
252
- type: JSON.stringify(usedExplicitIds, undefined, 4),
253
- })
254
- }
255
-
256
- if (usedCompositeIds.length > 0) {
257
- newProperties.push({
258
- name: 'composite',
259
- type: JSON.stringify(usedCompositeIds, undefined, 4),
260
- })
261
- }
262
-
263
- if (Object.keys(deletedOrUnusedIds).length > 0) {
264
- newProperties.push({
265
- name: 'deleted',
266
- type: JSON.stringify(deletedOrUnusedIds, undefined, 4),
267
- })
268
- }
269
-
270
- const keysInterface = Keys.getOrCreateKeysInterface(this.file)
271
- keysInterface.getProperties().forEach((p) => p.remove())
272
- keysInterface.addProperties(newProperties)
273
- }
274
- }
package/src/TypeScript.ts DELETED
@@ -1,65 +0,0 @@
1
- import { ts, tsc } from '@servicenow/sdk-project'
2
-
3
- export function getValueDeclaration(node: ts.Node) {
4
- const symbol = node.getSymbolOrThrow(`Expected identifier to have symbol: ${node.getText()}`)
5
- const valueDeclaration = getValueDeclarationFromSymbolOrAlias(symbol)
6
-
7
- if (!ts.Node.isShorthandPropertyAssignment(valueDeclaration)) {
8
- return valueDeclaration
9
- }
10
-
11
- const valueSymbol = valueDeclaration.getValueSymbol()
12
- if (!valueSymbol) {
13
- throw `Expected value declaration for shorthand property assignment to have value symbol: ${valueDeclaration.getText()}`
14
- }
15
-
16
- return getValueDeclarationFromSymbolOrAlias(valueSymbol)
17
- }
18
-
19
- function getValueDeclarationFromSymbolOrAlias(symbol: ts.Symbol) {
20
- const valueDeclaration = symbol.getValueDeclaration() ?? symbol.getAliasedSymbol()?.getValueDeclaration()
21
- if (!valueDeclaration) {
22
- throw Error(`No value declaration found for symbol: ${symbol.getName()}`)
23
- }
24
-
25
- return valueDeclaration
26
- }
27
-
28
- export function parseType(
29
- type: ts.Type,
30
- location: ts.Node,
31
- unparsableTypeHandler: (unparsableType: ts.Type) => unknown = (unparsableType) => {
32
- throw `Unparsable type: ${unparsableType.getText()}`
33
- }
34
- ): unknown {
35
- if (type.isLiteral()) {
36
- return type.getLiteralValueOrThrow()
37
- } else if (type.isTuple()) {
38
- return type.getTupleElements().map((e) => parseType(e, location, unparsableTypeHandler))
39
- } else if (type.isObject() && !type.isArray()) {
40
- return type.getProperties().reduce(
41
- (result, property) => ({
42
- ...result,
43
- [property.getName()]: parseType(property.getTypeAtLocation(location), location, unparsableTypeHandler),
44
- }),
45
- {}
46
- )
47
- } else {
48
- return unparsableTypeHandler(type)
49
- }
50
- }
51
-
52
- export function traverseNode(node: ts.Node, visitor: (node: ts.Node) => void) {
53
- node.forEachChild((child) => {
54
- traverseNode(child, visitor)
55
- })
56
-
57
- visitor(node)
58
- }
59
-
60
- export function createPropertyIdentifier(name: string) {
61
- //Test that this is going to be a valid javascript property name (aaa, _aaa, 1, aaa1). If so use as identifier (aaa: ...) or as a string literal property ('1aaa': ...)
62
- return /^((?!\d)[\w$]+|\d+)$/i.test(name)
63
- ? tsc.factory.createIdentifier(name)
64
- : tsc.factory.createStringLiteral(name)
65
- }
package/src/XML.ts DELETED
@@ -1,98 +0,0 @@
1
- import { Action, type Context } from './plugins'
2
- import { create } from 'xmlbuilder2'
3
- import type { XMLBuilder } from 'xmlbuilder2/lib/interfaces'
4
- import { maybeGetFieldToTagMap } from './util/RuntimeTableSchema'
5
-
6
- export type XmlValue = string | number | boolean
7
-
8
- export function isValidXmlValue(value: unknown): value is XmlValue {
9
- return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'
10
- }
11
-
12
- export function unloadBuilder(context: Context) {
13
- const xml = create({ version: '1.0' }).ele('record_update')
14
- const end = () => xml.end({ prettyPrint: true }).replaceAll('&nbsp;', '&amp;nbsp;')
15
-
16
- // Root Builder
17
- return {
18
- xml,
19
- end,
20
- record: (tableName: string, id: string | number, action: Action = 'INSERT_OR_UPDATE') => {
21
- const rec = recordXml(xml, tableName, id, { attr: { action }, context })
22
- rec.addSysScope(context)
23
- return rec
24
- },
25
- }
26
- }
27
-
28
- /**
29
- * If the code contains the character sequence ']]>' it will be parsed as the end of
30
- * the CDATA section. To avoid this, we need to split the sequence by dividing the
31
- * content into two CDATA sections at that point.
32
- */
33
- export function sanitizeNestedCdataTags(content: string): string {
34
- return content.replace(/]]>/g, ']]]]><![CDATA[>')
35
- }
36
-
37
- export function recordXml(
38
- xml: XMLBuilder,
39
- tableName: string,
40
- id: string | number,
41
- options: {
42
- attr?: Record<string, string>
43
- excludeScopeElement?: boolean
44
- context?: Context
45
- } = {}
46
- ) {
47
- const recordXml = xml.ele(tableName, options.attr || { action: 'INSERT_OR_UPDATE' })
48
- recordXml.ele('sys_id', undefined).txt(`${id}`)
49
- const fieldToTagMap = maybeGetFieldToTagMap(tableName, options.context?.getAllDocuments() || [])
50
-
51
- // Record Builder
52
- return {
53
- fields(fields: Record<string, XmlValue>) {
54
- for (const [columnName, value] of Object.entries(fields)) {
55
- this.field(columnName, value)
56
- }
57
- },
58
- field(columnName: string, value: unknown, attributes: Record<string, string> = {}, needsCDATA = false) {
59
- if (!isValidXmlValue(value)) {
60
- throw new Error(`Invalid XML value for "${columnName}" column: ${value}`)
61
- }
62
-
63
- let fieldXml
64
- const tag = fieldToTagMap[columnName]
65
- if (
66
- columnName === 'script' ||
67
- columnName === 'operation_script' ||
68
- tag === 'script' ||
69
- tag === 'html' ||
70
- tag === 'xml' ||
71
- needsCDATA
72
- ) {
73
- value = tag === 'xml' ? sanitizeNestedCdataTags(value as string) : value
74
- fieldXml = recordXml.ele(columnName, attributes).dat(value as string)
75
- } else {
76
- /*
77
- &nbsp is not a predefined entity for XML, to overcome that the platform uses &amp;nbsp; in XMLs.
78
- When we transform XML that contains &amp;nbsp; that translates to `&nbsp;`.
79
- But when fluent generates the XML from source it creates XML with &nbsp; which makes the XML invalid since `nbsp` is not defined.
80
- So we are replacing &nbsp; with &amp;nbsp; to ensure the XML is valid.
81
- */
82
- fieldXml = recordXml.ele(columnName, attributes).txt(`${value}`.replaceAll('&nbsp;', '&amp;nbsp;'))
83
- }
84
-
85
- // Field Builder
86
- return {
87
- choices(_choices: unknown) {
88
- fieldXml // TODO: Implement
89
- },
90
- }
91
- },
92
- addSysScope(context: Context) {
93
- return recordXml
94
- .ele('sys_scope', { display_value: context.app.config.scope })
95
- .txt(context.app.config.scopeId)
96
- },
97
- }
98
- }
@@ -1,239 +0,0 @@
1
- import {
2
- Document,
3
- File,
4
- LinkedDocument,
5
- UnlinkedDocument,
6
- Xml,
7
- XmlData,
8
- EntityData,
9
- Arranged,
10
- DocumentMap,
11
- ExtractionResult,
12
- } from './behaviors'
13
- import { Keys } from '../Keys'
14
- import { Plugin } from './Plugin'
15
- import { FluentDiagnostic } from './Diagnostic'
16
- import { ProjectContext, SupportedNode, ts, type Diagnostic } from '@servicenow/sdk-project'
17
-
18
- // These are only imported so they can be referenced in JS docs
19
- import type {
20
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
- Extractors,
22
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- Composers,
24
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
25
- Arrangers,
26
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
27
- Serializers,
28
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
29
- Generators,
30
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
31
- Transformers,
32
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
33
- PostProcessors,
34
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
35
- Diagnostics,
36
- Data,
37
- } from './behaviors'
38
-
39
- /**
40
- * The context object is a collection of contextual information and methods
41
- * passed into all the various behaviors of a plugin when those behaviors are
42
- * invoked. It is also used by the build system to orchestrate the build.
43
- *
44
- * Each behavioral method on the context can accept an array of plugins as
45
- * an argument to control which plugins are used for that request. If no
46
- * plugins are provided as input, all available plugins will be used.
47
- */
48
- export type Context = ProjectContext & {
49
- /**
50
- * Accepts a parsed XML object and returns an array of {@linkcode XmlData}
51
- * extracted from that object.
52
- *
53
- * @see {@linkcode Extractors}
54
- * @param xml The parsed XML to extract.
55
- * @param plugins An optional array of {@linkcode Plugin}s to use.
56
- * @returns an array of extracted data.
57
- */
58
- extractXml(this: Context, xml: Xml, plugins?: Plugin[]): XmlData[]
59
-
60
- /**
61
- * Accepts an AST node and returns a result containing an array of
62
- * {@linkcode RawData} or {@linkcode EntityData} extracted from that node.
63
- *
64
- * @see {@linkcode Extractors}
65
- * @param node The node to extract.
66
- * @param plugins An optional array of {@linkcode Plugin}s to use.
67
- * @returns a result object containing an array of extracted data.
68
- */
69
- extractAst(this: Context, node: ts.Node, plugins?: Plugin[]): ExtractionResult
70
-
71
- /**
72
- * Accepts an AST node and returns a result containing an array of
73
- * {@linkcode RawData} extracted from that node.
74
- *
75
- * @see {@linkcode Extractors}
76
- * @param node The node to extract.
77
- * @param plugins An optional array of {@linkcode Plugin}s to use.
78
- * @returns a result object containing an array of extracted data.
79
- */
80
- extractRaw(this: Context, node: ts.Node, plugins?: Plugin[]): ExtractionResult<Data>
81
-
82
- /**
83
- * Accepts an AST node and returns a result containing an array of
84
- * {@linkcode EntityData} extracted from that node.
85
- *
86
- * @see {@linkcode Extractors}
87
- * @param node The node to extract.
88
- * @param plugins An optional array of {@linkcode Plugin}s to use.
89
- * @returns a result object containing an array of extracted entities.
90
- */
91
- extractEntities(this: Context, node: ts.Node, plugins?: Plugin[]): ExtractionResult<EntityData>
92
-
93
- /**
94
- * Accepts an array of entity data and returns a {@linkcode LinkedDocument}
95
- * array.
96
- *
97
- * @see {@linkcode Composers}
98
- * @param data The {@linkcode EntityData} to compose.
99
- * @param plugins An optional array of {@linkcode Plugin}s to use.
100
- * @returns a {@linkcode LinkedDocument} array.
101
- */
102
- composeEntities(this: Context, data: EntityData[], plugins?: Plugin[]): Promise<LinkedDocument[]>
103
-
104
- /**
105
- * Accepts an array of XML data and returns an {@linkcode UnlinkedDocument}
106
- * array.
107
- *
108
- * @see {@linkcode Composers}
109
- * @param data The {@linkcode XmlData} to compose.
110
- * @param context The {@linkcode Context} object.
111
- * @param plugins An optional array of {@linkcode Plugin}s to use.
112
- * @returns an {@linkcode UnlinkedDocument} array.
113
- */
114
- composeXml(this: Context, data: XmlData[], plugins?: Plugin[]): UnlinkedDocument[]
115
-
116
- /**
117
- * Accepts an array of documents and returns the same documents
118
- * as unresolved arranged documents.
119
- *
120
- * @see {@linkcode Arrangers}
121
- * @param documents The {@linkcode Document}s to arrange.
122
- * @param plugins An optional array of {@linkcode Plugin}s to use.
123
- * @returns a {@linkcode Document} & {@linkcode Arranged} array.
124
- */
125
- arrange<D extends Document>(this: Context, documents: D[], plugins?: Plugin[]): (D & Arranged<'unresolved'>)[]
126
-
127
- /**
128
- * Accepts an array of documents and returns an array of files.
129
- *
130
- * @see {@linkcode Serializers}
131
- * @param documents The {@linkcode Document}s to serialize.
132
- * @param plugins An optional array of {@linkcode Plugin}s to use.
133
- * @returns a {@linkcode File} array.
134
- */
135
- serialize(this: Context, documents: (Document & Arranged)[], plugins?: Plugin[]): File[]
136
-
137
- /**
138
- * Accepts an array of documents, generates new AST nodes for those
139
- * documents, and returns the same documents as linked documents.
140
- *
141
- * @see {@linkcode Generators}
142
- * @param documents The {@linkcode Document}s to generate AST nodes for.
143
- * @param plugins An optional array of {@linkcode Plugin}s to use.
144
- * @returns a {@linkcode LinkedDocument} array.
145
- */
146
- generate(this: Context, documents: (UnlinkedDocument & Arranged)[], plugins?: Plugin[]): LinkedDocument[]
147
-
148
- /**
149
- * Accepts an array of linked documents and performs transformations on
150
- * their original nodes to reflect any changes that may have been made to
151
- * those documents' data.
152
- *
153
- * @see {@linkcode Transformers}
154
- * @param documents The {@linkcode LinkedDocument}s to transform.
155
- * @param plugins An optional array of {@linkcode Plugin}s to use.
156
- */
157
- transform(this: Context, documents: (LinkedDocument & Arranged)[], plugins?: Plugin[]): void
158
-
159
- /**
160
- * Accepts an array of extracted entities and runs post-processors on that
161
- * data. Post-processors may perform a variety of arbitrary operations.
162
- *
163
- * @see {@linkcode PostProcessors}
164
- * @param data The {@linkcode EntityData} to process.
165
- * @param plugins An optional array of {@linkcode Plugin}s to use.
166
- */
167
- postProcessEntities(this: Context, data: EntityData[], plugins?: Plugin[]): void
168
-
169
- /**
170
- * Accepts an AST node and returns an array of {@linkcode FluentDiagnostic}s
171
- * for that node.
172
- *
173
- * @see {@linkcode Diagnostics}
174
- * @param node The node to get diagnostics for.
175
- * @param plugins An optional array of {@linkcode Plugin}s to use.
176
- * @returns an array of diagnostics.
177
- */
178
- getAstDiagnostics(this: Context, node: ts.Node, plugins?: Plugin[]): FluentDiagnostic[]
179
-
180
- /**
181
- * An object with metadata related to plugin like list of all tables that are claimed to be handled by a specific plugin, and
182
- * the level of enforcement they apply to that table
183
- * and API that the plugin is responsible for.
184
- */
185
- getPluginForTable(table: string): { logLevel: Diagnostic.Level; api: string } | undefined
186
-
187
- /**
188
- * An array of plugins that will be used (unless overridden) during various
189
- * build process operations.
190
- *
191
- * @see {@link Plugin}
192
- */
193
- plugins: Plugin[]
194
-
195
- /**
196
- * An array of plugins which implement entity-handling behaviors which will
197
- * be used during various build process operations. These plugins are always
198
- * executed BEFORE the plugins in the {@link Context#plugins} array.
199
- *
200
- * @see {@link Plugin}
201
- */
202
- entityPlugins: Plugin[]
203
-
204
- usageCount: { [key: string]: number }
205
-
206
- /**
207
- * Returns an object of XML file names that was marked as handled or ignored by code transformation during the build process.
208
- */
209
- getHandledXmls: () => Record<string, HandledInfo>
210
-
211
- updateHandledXMLs: (xml: string | undefined, status: HandledStates, info?: string, node?: SupportedNode) => void
212
- setAllDocuments: (documents: Document[]) => void
213
- getAllDocuments: () => Document[]
214
- getDocumentMap: () => DocumentMap
215
- getDocument: (sysId: string, kind?: string) => Document | undefined
216
-
217
- argTypesToFix: ts.CallExpression[]
218
-
219
- fixTypeArguments: () => void
220
-
221
- /**
222
- * @deprecated Use the key methods on the context object instead.
223
- */
224
- keys: Keys
225
-
226
- explicitKeyExists: Keys['explicitKeyExists']
227
- getIdUsingExplicitKey: Keys['getIdUsingExplicitKey']
228
- registerExplicitId: Keys['registerExplicitId']
229
- registerCompositeId: Keys['registerCompositeId']
230
- getDeletedAndUnusedIds: Keys['getDeletedAndUnusedIds']
231
- getKeysSourceFile(): ts.SourceFile
232
- }
233
-
234
- export type HandledInfo = {
235
- state: HandledStates
236
- info?: string | undefined
237
- node?: SupportedNode | undefined
238
- }
239
- export type HandledStates = 'handled' | 'handled as record' | 'ignored' | 'skipped' | 'unchanged'