@servable/tools 1.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 (155) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -0
  3. package/package.json +106 -0
  4. package/src/config/candidates/featureCandidates/index.js +48 -0
  5. package/src/config/candidates/index.js +18 -0
  6. package/src/config/index.js +2 -0
  7. package/src/config/validate/feature/index.js +7 -0
  8. package/src/config/validate/index.js +5 -0
  9. package/src/document/1.0.0/all/build/class/index.js +67 -0
  10. package/src/document/1.0.0/all/build/feature/index.js +135 -0
  11. package/src/document/1.0.0/all/index.js +17 -0
  12. package/src/document/1.0.0/all/write/class/index.js +25 -0
  13. package/src/document/1.0.0/all/write/feature/index.js +43 -0
  14. package/src/document/1.0.0/all/write/index.js +10 -0
  15. package/src/document/1.0.0/all/write/utils/chunk.js +19 -0
  16. package/src/document/1.0.0/chunks/build/class/class.js +20 -0
  17. package/src/document/1.0.0/chunks/build/class/functions.js +21 -0
  18. package/src/document/1.0.0/chunks/build/class/lib.js +21 -0
  19. package/src/document/1.0.0/chunks/build/class/seed.js +21 -0
  20. package/src/document/1.0.0/chunks/build/class/triggers.js +26 -0
  21. package/src/document/1.0.0/chunks/build/feature/afterInit.js +20 -0
  22. package/src/document/1.0.0/chunks/build/feature/beforeInit.js +20 -0
  23. package/src/document/1.0.0/chunks/build/feature/class.js +42 -0
  24. package/src/document/1.0.0/chunks/build/feature/config.js +20 -0
  25. package/src/document/1.0.0/chunks/build/feature/functions.js +20 -0
  26. package/src/document/1.0.0/chunks/build/feature/lib.js +21 -0
  27. package/src/document/1.0.0/chunks/build/feature/liveClasses.js +20 -0
  28. package/src/document/1.0.0/chunks/build/feature/schema.js +206 -0
  29. package/src/document/1.0.0/chunks/build/feature/seed.js +20 -0
  30. package/src/document/1.0.0/chunks/build/feature/system.js +20 -0
  31. package/src/document/1.0.0/chunks/build/feature/triggers.js +20 -0
  32. package/src/document/1.0.0/generateGithubReadme/build/chunks/_generateBadge.js +4 -0
  33. package/src/document/1.0.0/generateGithubReadme/build/chunks/_githubPackage.js +4 -0
  34. package/src/document/1.0.0/generateGithubReadme/build/chunks/adapters.js +40 -0
  35. package/src/document/1.0.0/generateGithubReadme/build/chunks/apis.js +39 -0
  36. package/src/document/1.0.0/generateGithubReadme/build/chunks/categories.js +32 -0
  37. package/src/document/1.0.0/generateGithubReadme/build/chunks/dependencies/build.js +29 -0
  38. package/src/document/1.0.0/generateGithubReadme/build/chunks/dependencies/index.js +40 -0
  39. package/src/document/1.0.0/generateGithubReadme/build/chunks/footer.js +33 -0
  40. package/src/document/1.0.0/generateGithubReadme/build/chunks/githubTags.js +42 -0
  41. package/src/document/1.0.0/generateGithubReadme/build/chunks/install.js +29 -0
  42. package/src/document/1.0.0/generateGithubReadme/build/chunks/packages.js +26 -0
  43. package/src/document/1.0.0/generateGithubReadme/build/chunks/registry.js +42 -0
  44. package/src/document/1.0.0/generateGithubReadme/build/chunks/usage.js +68 -0
  45. package/src/document/1.0.0/generateGithubReadme/build/index.js +224 -0
  46. package/src/document/1.0.0/generateGithubReadme/index.js +28 -0
  47. package/src/document/1.1.0/all/build/class/index.js +67 -0
  48. package/src/document/1.1.0/all/build/feature/index.js +135 -0
  49. package/src/document/1.1.0/all/index.js +17 -0
  50. package/src/document/1.1.0/all/write/class/index.js +25 -0
  51. package/src/document/1.1.0/all/write/feature/index.js +43 -0
  52. package/src/document/1.1.0/all/write/index.js +10 -0
  53. package/src/document/1.1.0/all/write/utils/chunk.js +19 -0
  54. package/src/document/1.1.0/chunks/build/class/class.js +20 -0
  55. package/src/document/1.1.0/chunks/build/class/functions.js +21 -0
  56. package/src/document/1.1.0/chunks/build/class/lib.js +21 -0
  57. package/src/document/1.1.0/chunks/build/class/seed.js +21 -0
  58. package/src/document/1.1.0/chunks/build/class/triggers.js +26 -0
  59. package/src/document/1.1.0/chunks/build/feature/afterInit.js +20 -0
  60. package/src/document/1.1.0/chunks/build/feature/beforeInit.js +20 -0
  61. package/src/document/1.1.0/chunks/build/feature/class.js +42 -0
  62. package/src/document/1.1.0/chunks/build/feature/config.js +20 -0
  63. package/src/document/1.1.0/chunks/build/feature/functions.js +20 -0
  64. package/src/document/1.1.0/chunks/build/feature/lib.js +21 -0
  65. package/src/document/1.1.0/chunks/build/feature/liveClasses.js +20 -0
  66. package/src/document/1.1.0/chunks/build/feature/schema.js +206 -0
  67. package/src/document/1.1.0/chunks/build/feature/seed.js +20 -0
  68. package/src/document/1.1.0/chunks/build/feature/system.js +20 -0
  69. package/src/document/1.1.0/chunks/build/feature/triggers.js +20 -0
  70. package/src/document/1.1.0/generateGithubReadme/build/chunks/_generateBadge.js +4 -0
  71. package/src/document/1.1.0/generateGithubReadme/build/chunks/_githubPackage.js +4 -0
  72. package/src/document/1.1.0/generateGithubReadme/build/chunks/adapters.js +40 -0
  73. package/src/document/1.1.0/generateGithubReadme/build/chunks/apis.js +39 -0
  74. package/src/document/1.1.0/generateGithubReadme/build/chunks/categories.js +32 -0
  75. package/src/document/1.1.0/generateGithubReadme/build/chunks/dependencies/build.js +29 -0
  76. package/src/document/1.1.0/generateGithubReadme/build/chunks/dependencies/index.js +40 -0
  77. package/src/document/1.1.0/generateGithubReadme/build/chunks/footer.js +33 -0
  78. package/src/document/1.1.0/generateGithubReadme/build/chunks/githubTags.js +42 -0
  79. package/src/document/1.1.0/generateGithubReadme/build/chunks/install.js +29 -0
  80. package/src/document/1.1.0/generateGithubReadme/build/chunks/packages.js +26 -0
  81. package/src/document/1.1.0/generateGithubReadme/build/chunks/registry.js +42 -0
  82. package/src/document/1.1.0/generateGithubReadme/build/chunks/usage.js +68 -0
  83. package/src/document/1.1.0/generateGithubReadme/build/index.js +224 -0
  84. package/src/document/1.1.0/generateGithubReadme/index.js +28 -0
  85. package/src/document/gateway/all/index.js +23 -0
  86. package/src/document/gateway/generateGithubReadme/index.js +24 -0
  87. package/src/domain/feature/factory/absoluteModuleSrcPath.js +10 -0
  88. package/src/domain/feature/factory/featurePath.js +41 -0
  89. package/src/domain/feature/factory/index.js +71 -0
  90. package/src/domain/feature/index.js +117 -0
  91. package/src/domain/feature/lib/instancesPathIdUniqueId.js +17 -0
  92. package/src/domain/featureInstance/factory/getLoaderWithPayload.js +7 -0
  93. package/src/domain/featureInstance/factory/index.js +65 -0
  94. package/src/domain/featureInstance/factory/instancesWithPath.js +12 -0
  95. package/src/domain/featureInstance/index.js +199 -0
  96. package/src/domain/featureLoader/base.js +100 -0
  97. package/src/domain/featureLoader/factory/getModule.js +40 -0
  98. package/src/domain/featureLoader/factory/index.js +9 -0
  99. package/src/domain/featureLoader/lib/importJSDefault.js +12 -0
  100. package/src/domain/featureLoader/lib/importJSONDefault.js +14 -0
  101. package/src/domain/featureLoader/lib/mergeClassFeatures.js +17 -0
  102. package/src/domain/featureLoader/lib/triggerItems.js +8 -0
  103. package/src/domain/featureLoader/v1.0.0.js +861 -0
  104. package/src/domain/featureLoader/v1.1.0.js +871 -0
  105. package/src/domain/lib/checkFileExists.js +6 -0
  106. package/src/domain/servable/index.js +19 -0
  107. package/src/domain/servable/parse/index.js +5 -0
  108. package/src/domain/servable/parse/utils/index.js +216 -0
  109. package/src/index.js +29 -0
  110. package/src/lib/adaptConfig/basic.js +85 -0
  111. package/src/lib/adaptFeaturePayload.js +18 -0
  112. package/src/lib/checkFileExists.js +6 -0
  113. package/src/lib/cleanFeatures.js +25 -0
  114. package/src/lib/directories.js +49 -0
  115. package/src/lib/directoryFilesRecursive.js +83 -0
  116. package/src/lib/directoryGlob.js +14 -0
  117. package/src/lib/ensureDirectoryExists.js +14 -0
  118. package/src/lib/featureApiVersion.js +30 -0
  119. package/src/lib/foldersInFolder.js +46 -0
  120. package/src/lib/getModuleDir.js +19 -0
  121. package/src/lib/importFileAsText.js +15 -0
  122. package/src/lib/importJSONAsync.js +16 -0
  123. package/src/lib/sanitizePath.js +4 -0
  124. package/src/manifest/access/accessChildInTreeWithRoute.js +49 -0
  125. package/src/manifest/access/index.js +58 -0
  126. package/src/manifest/data/1.0.0/class.json +233 -0
  127. package/src/manifest/data/1.0.0/enums.js +255 -0
  128. package/src/manifest/data/1.0.0/feature.json +503 -0
  129. package/src/manifest/data/1.0.0/schema.json +145 -0
  130. package/src/manifest/data/1.1.0/class.json +233 -0
  131. package/src/manifest/data/1.1.0/enums.js +267 -0
  132. package/src/manifest/data/1.1.0/feature.json +542 -0
  133. package/src/manifest/data/1.1.0/schema.json +145 -0
  134. package/src/manifest/extract/extractAst.js +36 -0
  135. package/src/manifest/extract/extractItem.js +70 -0
  136. package/src/manifest/extract/formatFile.js +22 -0
  137. package/src/manifest/extract/index.js +23 -0
  138. package/src/manifest/extract/route/extractFileWithVariants.js +38 -0
  139. package/src/manifest/extract/route/extractFiles.js +87 -0
  140. package/src/manifest/extract/route/imageToBase64.js +42 -0
  141. package/src/manifest/extract/route/index.js +102 -0
  142. package/src/manifest/extract/templateDataForId.js +44 -0
  143. package/src/schema/build/buildFeature/classStruct/extractFeaturesFeatures.js +29 -0
  144. package/src/schema/build/buildFeature/classStruct/featureInClassClassSchema.js +36 -0
  145. package/src/schema/build/buildFeature/classStruct/index.js +62 -0
  146. package/src/schema/build/buildFeature/classStruct/preserveMostRestricted.js +68 -0
  147. package/src/schema/build/buildFeature/extractClassesFeature.js +49 -0
  148. package/src/schema/build/buildFeature/index.js +119 -0
  149. package/src/schema/build/index.js +54 -0
  150. package/src/schema/build/lib/getFeaturesLiveClasses.js +14 -0
  151. package/src/schema/build/lib/updateFeaturesExcerpt.js +24 -0
  152. package/src/schema/index.js +57 -0
  153. package/src/schema/validate/handleFeature/computeVersions.js +48 -0
  154. package/src/schema/validate/handleFeature/index.js +5 -0
  155. package/src/schema/validate/index.js +52 -0
@@ -0,0 +1,861 @@
1
+ import BaseClass from './base.js'
2
+
3
+ import directories from '../../lib/directories.js'
4
+ import cleanFeatures from '../../lib/cleanFeatures.js'
5
+ import mergeClassFeatures from './lib/mergeClassFeatures.js'
6
+
7
+ import importJSONAsync from '../../lib/importJSONAsync.js'
8
+ import directoryGlob from '../../lib/directoryGlob.js'
9
+
10
+ import checkFileExists from '../../lib/checkFileExists.js'
11
+ import directoryFilesRecursive from '../../lib/directoryFilesRecursive.js'
12
+ import { FeatureEnum } from '../../manifest/data/1.0.0/enums.js'
13
+
14
+ import triggerItems from './lib/triggerItems.js'
15
+
16
+ export default class FeatureLoaderV1_0_0 extends BaseClass {
17
+
18
+ async getClass({ className }) {
19
+ const cacheKey = 'class'
20
+ if (this._valueInCache(cacheKey)) {
21
+ return this._valueInCache(cacheKey)
22
+ }
23
+
24
+ const path = `${this.path}/classes/${className.toLowerCase()}/class/index.js`
25
+ // const path = `${this.path}/classes/${className.toLowerCase()}/class/index.js`
26
+
27
+ if (!(await checkFileExists(path))) {
28
+ //console.log('class>', className, 'file does not exist', path)
29
+ const externalClassesPath = `${this.path}/classes/external`
30
+ if (!(await checkFileExists(externalClassesPath))) {
31
+ return null
32
+ }
33
+ try {
34
+ const data = (await import(externalClassesPath)).default
35
+ if (!data) {
36
+ //console.log('class>', className, 'data null')
37
+ return null
38
+ }
39
+ // //console.log('class>', className, 'dataff', data)
40
+ return data[className]
41
+ } catch (e) {
42
+ console.error(e)
43
+ return null
44
+ }
45
+ }
46
+ // //console.log('class>', className, 'file exists')
47
+ const dd = await this._importJSDefault({ path, })
48
+ // //console.log('class>', className, 'file exists', dd)
49
+ return dd
50
+ }
51
+
52
+ async classFunctions({ className }) {
53
+ const cacheKey = 'classFunctions'
54
+ if (this._valueInCache(cacheKey)) {
55
+ return this._valueInCache(cacheKey)
56
+ }
57
+
58
+ let path = `${this.path}/classes/${className.toLowerCase()}/functions`
59
+ if (!(await checkFileExists(path))) {
60
+ path = `${this.path}/classes/${className.toLowerCase()}/cloudCode`
61
+ if (!(await checkFileExists(path))) {
62
+ return null
63
+ }
64
+ }
65
+
66
+ const data = (await directoryFilesRecursive({ path }))
67
+ //this.cache[cacheKey] = data
68
+ return data
69
+ }
70
+
71
+ async classTriggers({ className }) {
72
+ const cacheKey = 'classTriggers'
73
+ if (this._valueInCache(cacheKey)) {
74
+ return this._valueInCache(cacheKey)
75
+ }
76
+
77
+ const prefix = `${this.path}/classes/${className.toLowerCase()}/triggers`
78
+
79
+
80
+ const data = {}
81
+ for (var i = 0; i < triggerItems.length; i++) {
82
+ const item = triggerItems[i]
83
+ const path = `${prefix}/${item}.js`
84
+ if (!(await checkFileExists(path))) {
85
+ continue
86
+ }
87
+
88
+ const itemData = (await import(path))
89
+ if (!itemData || !itemData.default) {
90
+ continue
91
+ }
92
+
93
+ data[item] = itemData.default
94
+ }
95
+
96
+ if (Object.keys(data).length > 0) {
97
+ return data
98
+ }
99
+
100
+ return this.classTriggersMerged({ className })
101
+ }
102
+
103
+ async classTriggersMerged({ className }) {
104
+ const cacheKey = 'classTriggers'
105
+ if (this._valueInCache(cacheKey)) {
106
+ return this._valueInCache(cacheKey)
107
+ }
108
+
109
+ const path = `${this.path}/classes/${className.toLowerCase()}/triggers/index.js`
110
+ if (!(await checkFileExists(path))) {
111
+ return null
112
+ }
113
+
114
+ const data = (await import(path))
115
+ //this.cache[cacheKey] = data
116
+ return data
117
+ }
118
+
119
+ async classJobs({ className }) {
120
+ const cacheKey = 'classJobs'
121
+ if (this._valueInCache(cacheKey)) {
122
+ return this._valueInCache(cacheKey)
123
+ }
124
+
125
+ const path = `${this.path}/classes/${className.toLowerCase()}/jobs_`
126
+ if (!(await checkFileExists(path))) {
127
+ return null
128
+ }
129
+
130
+ const data = (await directoryFilesRecursive({ path }))
131
+ //this.cache[cacheKey] = data
132
+ return data
133
+ }
134
+
135
+ classSeedFolder({ className }) {
136
+ return `${this.path}/classes/${className.toLowerCase()}/seed`
137
+ }
138
+
139
+ async classSeedMode(props) {
140
+ const cacheKey = 'classSeedMode'
141
+ if (this._valueInCache(cacheKey)) {
142
+ return this._valueInCache(cacheKey)
143
+ }
144
+
145
+ const classSeedFolder = this.classSeedFolder(props)
146
+ let _path = `${classSeedFolder}/index.js`
147
+ if ((await checkFileExists(_path))) {
148
+ return 'manual'
149
+ }
150
+
151
+ _path = `${classSeedFolder}/transformer.js`
152
+ if ((await checkFileExists(_path))) {
153
+ return 'auto'
154
+ }
155
+
156
+ return 'none'
157
+ }
158
+
159
+ async classSeedManual(props) {
160
+ const cacheKey = 'classSeedManual'
161
+ if (this._valueInCache(cacheKey)) {
162
+ return this._valueInCache(cacheKey)
163
+ }
164
+
165
+ const classSeedFolder = this.classSeedFolder(props)
166
+ let _path = `${classSeedFolder}/index.js`
167
+ if (!(await checkFileExists(_path))) {
168
+ return null
169
+ }
170
+
171
+ const data = (await import(_path)).default
172
+ //this.cache[cacheKey] = data
173
+ return data
174
+ }
175
+
176
+ async classSeedMetadata(props) {
177
+ const cacheKey = 'classSeedMetadata'
178
+ if (this._valueInCache(cacheKey)) {
179
+ return this._valueInCache(cacheKey)
180
+ }
181
+
182
+ const classSeedFolder = this.classSeedFolder(props)
183
+
184
+ const _path = `${classSeedFolder}/metadata.json`
185
+ if (!(await checkFileExists(_path))) {
186
+ return null
187
+ }
188
+
189
+ const data = await importJSONAsync(_path)
190
+ //this.cache[cacheKey] = data
191
+ return data
192
+ }
193
+
194
+ async classSeedAutoFiles(props) {
195
+ const { className } = props
196
+ const cacheKey = 'classSeedAutoFiles'
197
+ if (this._valueInCache(cacheKey)) {
198
+ return this._valueInCache(cacheKey)
199
+ }
200
+
201
+ const path = this.classSeedFolder({ className })
202
+ const result = {}
203
+
204
+ let _path = `${path}/data.json`
205
+ if ((await checkFileExists(_path))) {
206
+ result.data = await importJSONAsync(_path)
207
+ }
208
+
209
+ _path = `${path}/transformer.js`
210
+ if ((await checkFileExists(_path))) {
211
+ result.transformer = (await import(_path)).default
212
+ // console.log("[Servable]", result.transformer)
213
+ }
214
+
215
+ _path = `${path}/validator.js`
216
+ if ((await checkFileExists(_path))) {
217
+ result.validator = (await import(_path)).default
218
+ }
219
+
220
+ _path = `${path}/ref.js`
221
+ if ((await checkFileExists(_path))) {
222
+ result.uniqueRef = (await import(_path)).default
223
+ }
224
+
225
+ return result
226
+ }
227
+
228
+ async afterInit() {
229
+ const cacheKey = 'afterInit'
230
+ if (this._valueInCache(cacheKey)) {
231
+ return this._valueInCache(cacheKey)
232
+ }
233
+
234
+ const path = `${this.path}/afterInit/index.js`
235
+ if (!(await checkFileExists(path))) {
236
+ return null
237
+ }
238
+
239
+ return this._importJSDefault({ path, })
240
+ }
241
+
242
+ async beforeInit() {
243
+ const cacheKey = 'beforeInit'
244
+ if (this._valueInCache(cacheKey)) {
245
+ return this._valueInCache(cacheKey)
246
+ }
247
+
248
+ const path = `${this.path}/beforeInit/index.js`
249
+ if (!(await checkFileExists(path))) {
250
+ return null
251
+ }
252
+
253
+ return this._importJSDefault({ path, })
254
+ }
255
+
256
+ configFolder() {
257
+ return `${this.path}/config`
258
+ }
259
+
260
+
261
+ async configDataFiles() {
262
+ const cacheKey = 'configData'
263
+ if (this._valueInCache(cacheKey)) {
264
+ return this._valueInCache(cacheKey)
265
+ }
266
+
267
+ const path = this.configFolder()
268
+ const result = {}
269
+
270
+ const valuesPath = `${path}/entries.json`
271
+ if ((await checkFileExists(valuesPath))) {
272
+ result.entries = await importJSONAsync(valuesPath)
273
+ }
274
+
275
+ const conditionsPath = `${path}/conditions.json`
276
+ if ((await checkFileExists(conditionsPath))) {
277
+ result.conditions = await importJSONAsync(conditionsPath)
278
+ }
279
+
280
+ const groupsPath = `${path}/groups.json`
281
+ if ((await checkFileExists(groupsPath))) {
282
+ result.groups = await importJSONAsync(groupsPath)
283
+ }
284
+
285
+ return result
286
+ }
287
+
288
+ classConfigFolder({ className }) {
289
+ return `${this.path}/classes/${className.toLowerCase()}/config`
290
+ }
291
+
292
+ async classConfigDataFiles({ className }) {
293
+ const cacheKey = 'classConfigDataFiles'
294
+ if (this._valueInCache(cacheKey)) {
295
+ return this._valueInCache(cacheKey)
296
+ }
297
+
298
+ const path = this.classConfigFolder({ className })
299
+ const result = {}
300
+
301
+ const valuesPath = `${path}/entries.json`
302
+ if ((await checkFileExists(valuesPath))) {
303
+ result.entries = await importJSONAsync(valuesPath)
304
+ }
305
+
306
+ const conditionsPath = `${path}/conditions.json`
307
+ if ((await checkFileExists(conditionsPath))) {
308
+ result.conditions = await importJSONAsync(conditionsPath)
309
+ }
310
+
311
+ const groupsPath = `${path}/groups.json`
312
+ if ((await checkFileExists(groupsPath))) {
313
+ result.groups = await importJSONAsync(groupsPath)
314
+ }
315
+
316
+ return result
317
+ }
318
+
319
+ seedFolder() {
320
+ return `${this.path}/seed`
321
+ }
322
+
323
+ async seedMode() {
324
+ const cacheKey = 'seedMode'
325
+ if (this._valueInCache(cacheKey)) {
326
+ return this._valueInCache(cacheKey)
327
+ }
328
+
329
+ const seedFolder = this.seedFolder()
330
+ let _path = `${seedFolder}/index.js`
331
+ if (await checkFileExists(_path)) {
332
+ return 'manual'
333
+ }
334
+
335
+ //Auto not handled yet for features
336
+ // _path = `${seedFolder}/transformer.js`
337
+ // if (await checkFileExists(_path)) {
338
+ // return 'auto'
339
+ // }
340
+
341
+ return 'none'
342
+ }
343
+
344
+ async seedManual() {
345
+ const cacheKey = 'seedManual'
346
+ if (this._valueInCache(cacheKey)) {
347
+ return this._valueInCache(cacheKey)
348
+ }
349
+ let _path = `${this.path}/seed/index.js`
350
+ if (!(await checkFileExists(_path))) {
351
+ return null
352
+ }
353
+
354
+ const data = (await import(_path)).default
355
+ //this.cache[cacheKey] = data
356
+ return data
357
+ }
358
+
359
+ async seedMetadata() {
360
+ const cacheKey = 'seedMetadata'
361
+ if (this._valueInCache(cacheKey)) {
362
+ return this._valueInCache(cacheKey)
363
+ }
364
+
365
+ const path = `${this.seedFolder()}/metadata.json`
366
+ if (!(await checkFileExists(path))) {
367
+ return null
368
+ }
369
+
370
+ const data = await importJSONAsync(path)
371
+ //this.cache[cacheKey] = data
372
+ return data
373
+ }
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+
388
+
389
+
390
+ async triggers() {
391
+ const cacheKey = 'triggers'
392
+ if (this._valueInCache(cacheKey)) {
393
+ return this._valueInCache(cacheKey)
394
+ }
395
+
396
+ const prefix = `${this.path}/triggers`
397
+
398
+ const data = {}
399
+ for (var i = 0; i < triggerItems.length; i++) {
400
+ const item = triggerItems[i]
401
+ const path = `${prefix}/${item}.js`
402
+ if (!(await checkFileExists(path))) {
403
+ continue
404
+ }
405
+
406
+ const itemData = (await import(path))
407
+ if (!itemData || !itemData.default) {
408
+ continue
409
+ }
410
+
411
+ data[item] = itemData.default
412
+ }
413
+
414
+ if (Object.keys(data).length > 0) {
415
+ return data
416
+ }
417
+
418
+ return this.triggersMerged()
419
+ }
420
+
421
+ async triggersMerged() {
422
+ const cacheKey = 'triggers'
423
+ if (this._valueInCache(cacheKey)) {
424
+ return this._valueInCache(cacheKey)
425
+ }
426
+
427
+ const path = `${this.path}/triggers/index.js`
428
+ if (!(await checkFileExists(path))) {
429
+ return null
430
+ }
431
+
432
+ const data = (await import(path))
433
+ // this.cache[cacheKey] = data
434
+ return data
435
+ }
436
+
437
+ async triggersMetadata() {
438
+ const cacheKey = 'triggersMetadata'
439
+ if (this._valueInCache(cacheKey)) {
440
+ return this._valueInCache(cacheKey)
441
+ }
442
+
443
+ const path = `${this.path}/triggers/metadata.json`
444
+ if (!(await checkFileExists(path))) {
445
+ return null
446
+ }
447
+
448
+ const data = await importJSONAsync(path)
449
+ this.cache[cacheKey] = data
450
+ return data
451
+ }
452
+
453
+
454
+
455
+
456
+ async classesSchemas(props) {
457
+ // if (this.valueInCache('classes')) {
458
+ // return this.valueInCache('classes')
459
+ // }
460
+
461
+ const schema = await this.schemaRaw(props)
462
+ if (schema && schema.managed) {
463
+ return schema.managed.classes
464
+ }
465
+
466
+ const schemaPath = await this._schemaPath()
467
+ let path = `${schemaPath}/classes.js`
468
+
469
+ if ((await checkFileExists(path))) {
470
+ const classes = await (await import(path)).default({ feature: this.featureInstance })
471
+ // this.cache['classes'] = classes
472
+ return classes
473
+ }
474
+
475
+ path = `${schemaPath}/classes.json`
476
+ if (!(await checkFileExists(path))) {
477
+ return []
478
+ }
479
+
480
+ const classes = await importJSONAsync(path)
481
+ // this.cache['classes'] = classes
482
+ return classes
483
+ }
484
+
485
+ async schemaFields(props) {
486
+ // const cacheKey = 'schemaFields'
487
+ // if (this.valueInCache(cacheKey)) {
488
+ // return this.valueInCache(cacheKey)
489
+ // }
490
+ const schema = await this.schemaRaw(props)
491
+ if (schema && schema.target) {
492
+ return schema.target.fields
493
+ }
494
+
495
+ const schemaPath = await this._schemaPath()
496
+ const path = `${schemaPath}/fields.json`
497
+ if (!(await checkFileExists(path))) {
498
+ return null
499
+ }
500
+
501
+ return this._importJSONDefault({ path, })
502
+ }
503
+
504
+ async schemaIndexes(props) {
505
+ // const cacheKey = 'schemaIndexes'
506
+ // if (this.valueInCache(cacheKey)) {
507
+ // return this.valueInCache(cacheKey)
508
+ // }
509
+ const schema = await this.schemaRaw(props)
510
+ if (schema && schema.target) {
511
+ return schema.target.indexes
512
+ }
513
+
514
+ const schemaPath = await this._schemaPath()
515
+ const path = `${schemaPath}/indexes.json`
516
+
517
+ if (!(await checkFileExists(path))) {
518
+ return null
519
+ }
520
+
521
+ return this._importJSONDefault({ path, })
522
+ }
523
+
524
+ async schemaRaw(props = {}) {
525
+ const { ad } = props
526
+ const a = await this._accessManifestItem({
527
+ item: FeatureEnum.Schema,
528
+ })
529
+ // const cacheKey = 'schemaIndexes'
530
+ // if (this.valueInCache(cacheKey)) {
531
+ // return this.valueInCache(cacheKey)
532
+ // }
533
+
534
+ const schemaPath = await this._schemaPath()
535
+
536
+ //#TODO: A dynamic schema needs features to be instantiated per class as to avoid class defintion collusions and edge case.
537
+ // let path = `${schemaPath}/index.js`
538
+
539
+ // if ((await checkFileExists(path))) {
540
+ // let data = (await import(path)).default
541
+ // //#TODO: don't use up feature params
542
+ // data = await data({ ...props, params: this.featureInstance.params })
543
+ // return data
544
+ // }
545
+
546
+ let path = `${schemaPath}/index.json`
547
+
548
+ if (!(await checkFileExists(path))) {
549
+ return null
550
+ }
551
+
552
+ const b = await this._importJSONDefault({ path, })
553
+ return b
554
+ }
555
+
556
+ async schemaClassLevelPermissions(props) {
557
+ // const cacheKey = 'schemaClassLevelPermissions'
558
+ // if (this.valueInCache(cacheKey)) {
559
+ // return this.valueInCache(cacheKey)
560
+ // }
561
+
562
+ const schema = await this.schemaRaw(props)
563
+ if (schema && schema.target) {
564
+ return schema.target.classLevelPermissions
565
+ }
566
+
567
+ const schemaPath = await this._schemaPath()
568
+ const path = `${schemaPath}/classlevelpermissions.json`
569
+
570
+ if (!(await checkFileExists(path))) {
571
+ return null
572
+ }
573
+
574
+ return this._importJSONDefault({ path, })
575
+ }
576
+
577
+ async _schemaPath() {
578
+ //let version = this.currentFeatureVersion ? this.currentFeatureVersion : this.featureInstanceVersion
579
+ let version = this.featureInstance.version
580
+ let versions = await this.schemaVersions()
581
+
582
+ if (!version) {
583
+ return `${this.path}/schema`
584
+ }
585
+
586
+ if (!versions
587
+ || !versions.length) {
588
+ return `${this.path}/schema`
589
+ }
590
+
591
+ if (versions
592
+ && versions.length
593
+ && !versions.includes(version)) {
594
+ return `${this.path}/schema`
595
+ }
596
+
597
+ if (versions
598
+ && versions.length
599
+ && version === 'latest') {
600
+ version = versions[versions.length - 1] //#TODO: sort by semver
601
+ }
602
+
603
+ const path = `${this.path}/schema/${version}`
604
+ return path
605
+ }
606
+
607
+
608
+ async schemaVersions() {
609
+ const cacheKey = '_schemaVersions'
610
+ if (this._valueInCache(cacheKey)) {
611
+ return this._valueInCache(cacheKey)
612
+ }
613
+ const items = await directories({ path: `${this.path}/schema` })
614
+ if (!items || !items.length) {
615
+ return null
616
+ }
617
+ let data = items.map(i => i.name)
618
+ data = data.sort()
619
+ this.cache[cacheKey] = data
620
+ return data
621
+ }
622
+
623
+ async schemaVersionOf({ version, subPath }) {
624
+ const cacheKey = `_schemaVersionOf${subPath}`
625
+ if (this._valueInCache(cacheKey)) {
626
+ return this._valueInCache(cacheKey)
627
+ }
628
+
629
+ const versions = await this.schemaVersions()
630
+ if (!versions || !versions.includes(version)) {
631
+ return null
632
+ }
633
+
634
+ const path = `${this.path}/schema/${version}/${subPath}`
635
+ if (!(await checkFileExists(path))) {
636
+ return null
637
+ }
638
+
639
+ const data = await import(path)
640
+ this.cache[cacheKey] = data
641
+ return data
642
+ }
643
+
644
+ async classFeatures({ className, withFeaturesFeatures = false }) {
645
+ const cacheKey = 'class'
646
+ // //console.log('classFeatures>', className, 'enter')
647
+ if (this._valueInCache(cacheKey)) {
648
+ return this._valueInCache(cacheKey)
649
+ }
650
+ const items = []
651
+ const seedMode = await this.classSeedMode({ className })
652
+ // //console.log('classFeatures>', className, 'seedmode', seedMode)
653
+ if (seedMode === 'auto') {
654
+ items.push({ id: 'servableautoseedable' })
655
+ }
656
+ const _class = await this.getClass({ className })
657
+ if (!_class) {
658
+ //console.log('classFeatures>', className, 'no _class')
659
+ return items
660
+ }
661
+
662
+ let data = mergeClassFeatures({ items, _class, withFeaturesFeatures })
663
+ const path = `${this.path}/classes/${className.toLowerCase()}/class/protocols.js`
664
+
665
+ if ((await checkFileExists(path))) {
666
+
667
+ let _data = (await import(path)).default
668
+ // //console.log('classFeatures>', className, '_data', _data)
669
+ _data = _data ? _data : []
670
+ data = [...data,
671
+ ..._data]
672
+ }
673
+
674
+ //this.cache[cacheKey] = data
675
+ data = cleanFeatures(data)
676
+ // result = _.uniq(result, a => a.id)
677
+ // //console.log('classFeatures>', className, 'finaldata', data)
678
+ return data
679
+ }
680
+
681
+
682
+ async ownFeatures() {
683
+ const cacheKey = 'ownFeatures'
684
+ if (this._valueInCache(cacheKey)) {
685
+ return this._valueInCache(cacheKey)
686
+ }
687
+
688
+ let path = `${this.path}/class/protocols.js`
689
+ if (!(await checkFileExists(path))) {
690
+ return null
691
+ }
692
+
693
+ const data = (await import(path)).default
694
+
695
+ //this.cache[cacheKey] = data
696
+ return data
697
+ }
698
+
699
+ async ownFeaturesClass() {
700
+ const cacheKey = 'ownFeaturesClass'
701
+ if (this._valueInCache(cacheKey)) {
702
+ return this._valueInCache(cacheKey)
703
+ }
704
+
705
+ const path = `${this.path}/class/index.js`
706
+ if (!(await checkFileExists(path))) {
707
+ return null
708
+ }
709
+ // const _data = (await import(path))
710
+ return this._importJSDefault({ path, })
711
+ }
712
+
713
+ async liveClasses() {
714
+ const cacheKey = 'liveclasses'
715
+ if (this._valueInCache(cacheKey)) {
716
+ return this._valueInCache(cacheKey)
717
+ }
718
+
719
+ const path = `${this.path}/liveclasses.js`
720
+ if (!(await checkFileExists(path))) {
721
+ return null
722
+ }
723
+
724
+ return this._importJSDefault({ path, })
725
+ }
726
+
727
+ async systemDockerCompose() {
728
+ const cacheKey = 'systemDockerCompose'
729
+ if (this._valueInCache(cacheKey)) {
730
+ return this._valueInCache(cacheKey)
731
+ }
732
+
733
+ const path = `${this.path}/system/docker/docker-compose.yaml`
734
+ if (!(await checkFileExists(path))) {
735
+ return null
736
+ }
737
+
738
+ const data = (await import(path))
739
+ //this.cache[cacheKey] = data
740
+ return data
741
+ }
742
+
743
+ async systemDockerComposeExists() {
744
+ const path = this.systemDockerComposePath()
745
+ return checkFileExists(path)
746
+ }
747
+
748
+ systemDockerComposePath() {
749
+ return `${this.systemDockerComposeDirPath()}/docker-compose.yaml`
750
+ }
751
+
752
+ systemDockerDataPath() {
753
+ return `${this.systemDockerComposeDirPath()}/data`
754
+ }
755
+
756
+ systemDockerComposeDirPath() {
757
+ return `${this.path}/system/docker`
758
+ }
759
+
760
+ configDirPath_obs() {
761
+ return `${this.path}/config`
762
+ }
763
+
764
+ async configData_obs() {
765
+ // const cacheKey = 'schemaClassLevelPermissions'
766
+ // if (this.valueInCache(cacheKey)) {
767
+ // return this.valueInCache(cacheKey)
768
+ // }
769
+
770
+ const schemaPath = await this._schemaPath()
771
+ const path = `${this.configDirPath_obs()}/index.json`
772
+
773
+ if (!(await checkFileExists(path))) {
774
+ return null
775
+ }
776
+
777
+ return this._importJSONDefault({ path, })
778
+ }
779
+
780
+ async systemDockerPayloadAdapter() {
781
+ const cacheKey = 'systemDockerPayloadAdapter'
782
+ if (this._valueInCache(cacheKey)) {
783
+ return this._valueInCache(cacheKey)
784
+ }
785
+
786
+ const path = `${this.path}/system/docker/adaptpayload.js`
787
+ if (!(await checkFileExists(path))) {
788
+ return null
789
+ }
790
+
791
+ return this._importJSDefault({ path, })
792
+ }
793
+
794
+
795
+
796
+ async functions() {
797
+ const cacheKey = 'functions'
798
+ if (this._valueInCache(cacheKey)) {
799
+ return this._valueInCache(cacheKey)
800
+ }
801
+
802
+ let path = `${this.path}/cloudCode`
803
+ if (!(await checkFileExists(path))) {
804
+ path = `${this.path}/functions`
805
+ if (!(await checkFileExists(path))) {
806
+ path = `${this.path}/cloudcode`
807
+ if (!(await checkFileExists(path))) {
808
+ return null
809
+ }
810
+ }
811
+ }
812
+
813
+ const data = (await directoryFilesRecursive({ path }))
814
+ //this.cache[cacheKey] = data
815
+ return data
816
+ }
817
+
818
+
819
+ async services() {
820
+ let path = `${this.path}/services`
821
+ if (!(await checkFileExists(path))) {
822
+ return null
823
+ }
824
+
825
+ const data = (await directoryFilesRecursive({ path }))
826
+ //this.cache[cacheKey] = data
827
+ return data
828
+ }
829
+
830
+ async routes() {
831
+ const cacheKey = 'routes'
832
+ if (this._valueInCache(cacheKey)) {
833
+ return this._valueInCache(cacheKey)
834
+ }
835
+
836
+ let path = `${this.path}/routes`
837
+ if (!(await checkFileExists(path))) {
838
+ return null
839
+ }
840
+
841
+ const data = (await directoryGlob({ path: `${path}/**/*.js` }))
842
+ return data
843
+ }
844
+
845
+ async jobFiles() {
846
+ const cacheKey = 'jobFiles'
847
+ if (this._valueInCache(cacheKey)) {
848
+ return this._valueInCache(cacheKey)
849
+ }
850
+
851
+ const path = `${this.path}/jobs_`
852
+ if (!(await checkFileExists(path))) {
853
+ return null
854
+ }
855
+
856
+ const data = (await directoryFilesRecursive({ path }))
857
+ //this.cache[cacheKey] = data
858
+ return data
859
+ }
860
+ }
861
+