@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,36 @@
1
+ // https://jsdoc.app
2
+ // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/recommended-tags
3
+
4
+ export default props => {
5
+ const { ast } = props
6
+ if (!ast || !ast.length) {
7
+ return null
8
+ }
9
+ const targetAst = ast[0]
10
+ const { tags, } = targetAst
11
+ if (!tags || !tags.length) {
12
+ return null
13
+ }
14
+ let tagsByTitle = null
15
+ const params = []
16
+ tags.forEach(tag => {
17
+ const { title } = tag
18
+ if (!title) {
19
+ return
20
+ }
21
+ if (title === 'param') {
22
+ params.push(tag)
23
+ }
24
+ if (title.indexOf('servable-') !== 0) {
25
+ // return
26
+ }
27
+
28
+ if (!tagsByTitle) {
29
+ tagsByTitle = {}
30
+ }
31
+
32
+ tagsByTitle[title] = tag
33
+ })
34
+
35
+ return { ...targetAst, tags, tagsByTitle, params }
36
+ }
@@ -0,0 +1,70 @@
1
+ import performRoute from './route/index.js'
2
+ import templateDataForId from './templateDataForId.js'
3
+
4
+ const performItem = async (props) => {
5
+ const {
6
+ item,
7
+ reference = {},
8
+ parentLeafPath,
9
+ prefix } = props
10
+
11
+ const { id, routes, name, } = item
12
+
13
+ let computed = null
14
+ if (routes && routes.length) {
15
+ const _routes = routes.sort(a => a.priority)
16
+
17
+ for (var i = 0; i < _routes.length; i++) {
18
+ computed = await performRoute({
19
+ ...props,
20
+ route: _routes[i],
21
+ parentLeafPath
22
+ })
23
+ if (computed && computed.data && computed.data.length) {
24
+ break
25
+ }
26
+ }
27
+
28
+ let result = {
29
+ ...item,
30
+ }
31
+
32
+ if (computed) {
33
+ result = {
34
+ ...result,
35
+ ...computed
36
+ }
37
+ }
38
+
39
+ if (computed && computed.templateCollection && computed.templateCollection.folders && computed.templateCollection.folders.length) {
40
+ const { templateId } = computed
41
+ const templateData = await templateDataForId({ id: templateId, path: props.parentLeafPath })
42
+
43
+ result.children = await Promise.all(computed.templateCollection.folders.map(async (folder, index) => {
44
+ const { stat, name } = folder
45
+ return performItem({
46
+ ...props,
47
+ item: templateData,
48
+ parentLeafPath: folder.path,
49
+ prefix: `${templateId}-${name}`
50
+ })
51
+ }))
52
+ } else if (computed && computed.children && computed.children.length) {
53
+ result.children = await Promise.all(computed.children.map(async a =>
54
+ performItem({
55
+ ...props,
56
+ item: a,
57
+ parentLeafPath: computed.leafPath
58
+ })))
59
+ }
60
+
61
+ if (computed && computed.fullPath) {
62
+ // reference[result.route.fullPath] = result
63
+ reference[prefix ? `${prefix}-${result.id}` : result.id] = result
64
+ }
65
+
66
+ return result
67
+ }
68
+ }
69
+
70
+ export default performItem
@@ -0,0 +1,22 @@
1
+ import commentsParser from 'parse-comments'
2
+ import importFileAsText from "../../lib/importFileAsText.js"
3
+ import extractAst from './extractAst.js'
4
+
5
+ export default async ({ file }) => {
6
+ try {
7
+ const { path } = file
8
+ const text = await importFileAsText(path)
9
+ const ast = commentsParser.parse(text)
10
+ const astAdapted = extractAst({ ast })
11
+
12
+ return {
13
+ ...file,
14
+ ast,
15
+ astAdapted
16
+ }
17
+ } catch (e) {
18
+ console.error(e)
19
+ }
20
+
21
+ return file
22
+ }
@@ -0,0 +1,23 @@
1
+ import templateDataForId from './templateDataForId.js'
2
+ import extractItem from './extractItem.js'
3
+ import { DataTemplateType } from '../data/1.0.0/enums.js'
4
+ import ServableClass from "../../domain/servable/index.js"
5
+
6
+ export default async ({
7
+ path,
8
+ dataTemplateType = DataTemplateType.Feature
9
+ }) => {
10
+ const reference = {}
11
+ if (!global.Servable) {
12
+ global.Servable = new ServableClass()
13
+ }
14
+
15
+ const item = await templateDataForId({ id: dataTemplateType, path })
16
+ const tree = await extractItem({
17
+ item,
18
+ reference,
19
+ parentLeafPath: path
20
+ })
21
+
22
+ return { reference, tree }
23
+ }
@@ -0,0 +1,38 @@
1
+
2
+ import checkFileExists from "../../../lib/checkFileExists.js"
3
+ import sanitizePath from '../../../lib/sanitizePath.js'
4
+
5
+ export default async (props) => {
6
+ const {
7
+ payload,
8
+ fullPath,
9
+ variants,
10
+ moduleImporter,
11
+ extensionType
12
+ } = props
13
+
14
+ let files = await Promise.all(variants.map(async variant => {
15
+ const _variantFullPath = fullPathForVariant({ fullPath, variant, extensionType })
16
+ if (!(await checkFileExists(_variantFullPath))) {
17
+ return null
18
+ }
19
+ const _module = await moduleImporter({ path: _variantFullPath })
20
+ return {
21
+ ...payload,
22
+ path: _variantFullPath,
23
+ module: _module,
24
+ variant
25
+ }
26
+ }))
27
+
28
+ return files.filter(a => a)
29
+ }
30
+
31
+ const fullPathForVariant = ({ variant, fullPath, extensionType }) => {
32
+ if (!variant) {
33
+ return `${sanitizePath(`${fullPath}.${extensionType}`)}`
34
+ }
35
+
36
+ // return `/${sanitizePath(`${fullPath}${variant.trim()}.${extensionType}`)}`
37
+ return `${fullPath}${variant.trim()}.${extensionType}`
38
+ }
@@ -0,0 +1,87 @@
1
+ import checkFileExists from "../../../lib/checkFileExists.js"
2
+ import importJSONAsync from "../../../lib/importJSONAsync.js"
3
+ import sanitizePath from '../../../lib/sanitizePath.js'
4
+ import fs from "fs"
5
+ import fse from 'fs-extra'
6
+ import imageToBase64 from './imageToBase64.js'
7
+ import mime from 'mime'
8
+ import extractFileWithVariants from './extractFileWithVariants.js'
9
+
10
+ export default async (props) => {
11
+ const {
12
+ mimeType,
13
+ fullPath,
14
+ variants,
15
+ params
16
+ } = props
17
+
18
+ const extensionType = mime.getExtension(mimeType)
19
+
20
+ let documentation = null
21
+ let md = `${sanitizePath(`${props.fullPath}.md`)}`
22
+ if (!(await checkFileExists(md))) {
23
+ md = `${sanitizePath(`${props.fullPath}.mdx`)}`
24
+ }
25
+
26
+ if (await checkFileExists(md)) {
27
+ documentation = await fs.promises.readFile(md, 'utf8')
28
+ }
29
+
30
+ let moduleImporter = {}
31
+
32
+ switch (mimeType) {
33
+ case 'image/png':
34
+ case 'image/jpeg':
35
+ case 'image/jpg':
36
+ case 'image/gif':
37
+ case 'image/webp': {
38
+ moduleImporter = async ({ path }) => imageToBase64({
39
+ path,
40
+ mimeType,
41
+ maxWidth: params.maxWidth,
42
+ maxHeight: params.maxHeight
43
+ })
44
+ break
45
+ }
46
+ default:
47
+ case 'text/markdown':
48
+ case 'text/yaml':
49
+ {
50
+ moduleImporter = async ({ path }) => fse.readFile(path, 'utf8')
51
+ break
52
+ }
53
+ case 'image/svg+xml': {
54
+ moduleImporter = async ({ path }) => {
55
+ let data = await fse.readFile(path, 'utf8')
56
+ data = data.replace(/(\r\n|\n|\r)/gm, "")
57
+ return data
58
+ }
59
+ break
60
+ }
61
+ case 'text/javascript': {
62
+ moduleImporter = async ({ path }) => import(path)
63
+ break
64
+ }
65
+ case 'application/json': {
66
+ moduleImporter = async ({ path }) => importJSONAsync(path)
67
+ break
68
+ }
69
+ case 'text/yaml': {
70
+ moduleImporter = async ({ path }) => fse.readFile(path, 'utf8')
71
+ break
72
+ }
73
+ }
74
+
75
+ const files = await extractFileWithVariants({
76
+ variants,
77
+ extensionType,
78
+ moduleImporter,
79
+ fullPath,
80
+ payload: {
81
+ mimeType,
82
+ documentation,
83
+ },
84
+ })
85
+
86
+ return files
87
+ }
@@ -0,0 +1,42 @@
1
+ import sharp from 'sharp'
2
+
3
+ /**
4
+ * @description Transforms a local image file into a data payload
5
+ * @param path Path to file
6
+ */
7
+ export default async (props) => {
8
+ const {
9
+ path,
10
+ input,
11
+ width = 600,
12
+ height = 200,
13
+ maxWidth = 2048,
14
+ maxHeight = 1024,
15
+ quality = 90,
16
+ mimeType
17
+ } = props
18
+
19
+ try {
20
+ const buffer = await sharp(input ? input : path)
21
+ // .resize(width, height, {
22
+ .resize(maxWidth, maxHeight, {
23
+ fit: 'inside',
24
+ })
25
+ .webp({ quality })
26
+ .toBuffer({ resolveWithObject: true })
27
+
28
+ if (!buffer || !buffer.data) {
29
+ return null
30
+ }
31
+
32
+ const data = { base64: `${buildPrefix(mimeType)},${buffer.data.toString("base64")}` }
33
+ return data
34
+ } catch (e) {
35
+ console.error(e)
36
+ }
37
+ return null
38
+ }
39
+
40
+ const buildPrefix = mimeType => {
41
+ return `data:${mimeType};base64`
42
+ }
@@ -0,0 +1,102 @@
1
+ import checkFileExists from "../../../lib/checkFileExists.js"
2
+ import directoryFilesRecursive from '../../../lib/directoryFilesRecursive.js'
3
+ import formatFile from '../formatFile.js'
4
+ import foldersInFolder from '../../../lib/foldersInFolder.js'
5
+ import extractFiles from './extractFiles.js'
6
+ import sanitizePath from '../../../lib/sanitizePath.js'
7
+
8
+ export default async (props) => {
9
+ const { item, route, parentLeafPath } = props
10
+ const { type,
11
+ mimeTypes,
12
+ variants = [''],
13
+ params = {}
14
+ } = route
15
+
16
+ let fullPath = `${sanitizePath(`${parentLeafPath}/${route.path}`)}`
17
+ let files = null
18
+ const result = {
19
+ ...route,
20
+ type,
21
+ mimeTypes,
22
+ fullPath,
23
+ leafPath: parentLeafPath
24
+ }
25
+ try {
26
+ switch (type) {
27
+ case 'folder': {
28
+ result.leafPath = `${parentLeafPath}/${route.path}`
29
+ break
30
+ }
31
+ case 'templateCollection': {
32
+ result.leafPath = `${parentLeafPath}/${route.path}`
33
+
34
+ if (!(await checkFileExists(fullPath))) {
35
+ break
36
+ }
37
+ const folders = await foldersInFolder({ path: fullPath, })
38
+ result.templateCollection = {
39
+ folders
40
+ }
41
+
42
+ break
43
+ }
44
+ case 'filesCollection': {
45
+ if (!(await checkFileExists(fullPath))) {
46
+ break
47
+ }
48
+
49
+ files = await directoryFilesRecursive({
50
+ path: fullPath,
51
+ includeMeta: true
52
+ })
53
+
54
+ if (files && files.length) {
55
+ files = files.map(file => ({
56
+ path: file.path,
57
+ module: file.module,
58
+ //TODO: extensionType,
59
+ }))
60
+ }
61
+
62
+ } break
63
+ case 'file': {
64
+ let _variants = variants
65
+ if (!_variants.includes('')) {
66
+ _variants = ['', ..._variants]
67
+ }
68
+ _variants = _variants.sort()
69
+ for (var i = 0; i < mimeTypes.length; i++) {
70
+ const mimeType = mimeTypes[i]
71
+
72
+ const _files = await extractFiles({
73
+ mimeType,
74
+ fullPath,
75
+ variants: _variants,
76
+ params
77
+ })
78
+ if (_files && _files.length) {
79
+ if (!files) {
80
+ files = []
81
+ }
82
+ files = files.concat(_files)
83
+ }
84
+ }
85
+ } break
86
+ default: break
87
+ }
88
+
89
+
90
+ result.leafPath = `${sanitizePath(result.leafPath)}`
91
+
92
+ if (files && files.length) {
93
+ files = files.filter(a => a.module)
94
+ if (files.length) {
95
+ result.data = await Promise.all(files.map(async file => formatFile({ file })))
96
+ }
97
+ }
98
+ } catch (e) {
99
+ console.error('[SERVABLE]', 'performRoute', item, e)
100
+ }
101
+ return result
102
+ }
@@ -0,0 +1,44 @@
1
+ import checkFileExists from "../../lib/checkFileExists.js"
2
+ import importJSONAsync from "../../lib/importJSONAsync.js"
3
+
4
+ import path from 'path'
5
+ // import callerPath from 'caller-path'
6
+ import { fileURLToPath } from 'url'
7
+ import { dirname } from 'path'
8
+ const __filename = fileURLToPath(import.meta.url)
9
+ const __dirname = dirname(__filename)
10
+
11
+
12
+ export default async ({ id, path: featurePath }) => {
13
+ let version = "1.0.0"
14
+ let index = await loadIndex(featurePath)
15
+ if (index && index.apiVersion) {
16
+ version = index.apiVersion
17
+ }
18
+ const basePath = '../data'
19
+ let url = `${basePath}/${version}/${id}.json`
20
+ url = path.resolve(__dirname, url)
21
+ if (!(await checkFileExists(url))) {
22
+ throw { message: `Could not find template data ${id} at path ${featurePath}` }
23
+ }
24
+ let data = await importJSONAsync(url)
25
+ return data
26
+ }
27
+
28
+
29
+ const loadIndex = async (__path) => {
30
+
31
+ let _path = `${__path}/index.json`
32
+ if (!(await checkFileExists(_path))) {
33
+ _path = `${__path}/manifest.json`
34
+ if (!(await checkFileExists(_path))) {
35
+ _path = `${__path}/module.json`
36
+ if (!(await checkFileExists(_path))) {
37
+ return null
38
+ }
39
+ }
40
+ }
41
+
42
+ const data = await importJSONAsync(_path)
43
+ return data
44
+ }
@@ -0,0 +1,29 @@
1
+ import _ from 'underscore'
2
+ import adaptFeaturePayload from '../../../../lib/adaptFeaturePayload.js'
3
+ import cleanFeatures from '../../../../lib/cleanFeatures.js'
4
+
5
+ export default async ({
6
+ featuresPayloads,
7
+ featureFactory,
8
+ instancesPathId }) => {
9
+
10
+ let items = []
11
+
12
+ for (var i in featuresPayloads) {
13
+ const featurePayload = featuresPayloads[i]
14
+ const feature = await featureFactory({
15
+ featurePayload,
16
+ instancesPathId
17
+ })
18
+ //#TODO: feature.loader
19
+ const ownFeatures = await feature.loader.ownFeatures()
20
+ let a = (ownFeatures && ownFeatures.length) ? ownFeatures.map(adaptFeaturePayload) : null
21
+ if (a && a.length) {
22
+ items = items.concat(a)
23
+ }
24
+ }
25
+
26
+ items = _.flatten(items.filter(a => a)).filter(a => a)
27
+ items = cleanFeatures(items)
28
+ return items
29
+ }
@@ -0,0 +1,36 @@
1
+ import preserveMostRestricted from './preserveMostRestricted.js'
2
+
3
+ export default async (props) => {
4
+ const { classSchema, feature } = props
5
+ let _classSchema = { ...classSchema }
6
+ try {
7
+ //#TODO: feature.loader
8
+ const _fields = await feature.loader.schemaFields()
9
+ if (_fields) {
10
+ _classSchema.fields = {
11
+ ..._classSchema.fields,
12
+ ..._fields
13
+ }
14
+ }
15
+
16
+ //#TODO: feature.loader
17
+ const _indexes = await feature.loader.schemaIndexes()
18
+ if (_indexes) {
19
+ _classSchema.indexes = {
20
+ ..._classSchema.indexes,
21
+ ..._indexes
22
+ }
23
+ }
24
+
25
+ //#TODO: feature.loader
26
+ const _classLevelPermissions = await feature.loader.schemaClassLevelPermissions()
27
+ if (_classLevelPermissions) {
28
+ const __classLevelPermissions = preserveMostRestricted([_classSchema.classLevelPermissions, _classLevelPermissions])
29
+ _classSchema.classLevelPermissions = __classLevelPermissions
30
+ }
31
+ } catch (e) {
32
+ console.error(e)
33
+ }
34
+
35
+ return _classSchema
36
+ }
@@ -0,0 +1,62 @@
1
+ import featureInClassClassSchema from './featureInClassClassSchema.js'
2
+ import adaptFeaturePayload from '../../../../lib/adaptFeaturePayload.js'
3
+ import extractFeaturesFeatures from './extractFeaturesFeatures.js'
4
+ import cleanFeatures from '../../../../lib/cleanFeatures.js'
5
+
6
+ export default async ({
7
+ feature,
8
+ classSchema,
9
+ featureFactory,
10
+ instancesPathId
11
+ }) => {
12
+
13
+ const { className } = classSchema
14
+
15
+ //#TODO: feature.loader
16
+ let featuresPayloads = await feature.loader.classFeatures({
17
+ className,
18
+ withFeaturesFeatures: false
19
+ })
20
+
21
+ if (!featuresPayloads
22
+ || !featuresPayloads.length) {
23
+ return { classSchema }
24
+ }
25
+
26
+ featuresPayloads = featuresPayloads.map(adaptFeaturePayload)
27
+
28
+ if (!featuresPayloads || !featuresPayloads.length) {
29
+ return { classSchema }
30
+ }
31
+
32
+ let featuresFeatures = await extractFeaturesFeatures({
33
+ featuresPayloads,
34
+ featureFactory,
35
+ instancesPathId
36
+ })
37
+ featuresPayloads = cleanFeatures([...featuresPayloads, ...featuresFeatures])
38
+
39
+ let _classSchema = { ...classSchema }
40
+
41
+ for (var i in featuresPayloads) {
42
+ const featurePayload = featuresPayloads[i]
43
+ const feature = await featureFactory({
44
+ featurePayload,
45
+ instancesPathId
46
+ })
47
+ _classSchema = await featureInClassClassSchema({
48
+ feature,
49
+ classSchema: _classSchema,
50
+ })
51
+ }
52
+
53
+
54
+ //#TODO: feature.loader
55
+ const _class = await feature.loader.getClass({ className })
56
+
57
+ return {
58
+ classSchema: _classSchema,
59
+ featuresPayloads,
60
+ _class
61
+ }
62
+ }
@@ -0,0 +1,68 @@
1
+ export default (items) => {
2
+ const permissions = dedupePermissions(items)
3
+ const protectedFields = dedupeProtectedFields(items)
4
+
5
+ return {
6
+ ...permissions,
7
+ protectedFields
8
+ }
9
+ }
10
+
11
+ const dedupeProtectedFields = (items) => {
12
+ const a = items[0].protectedFields ? items[0].protectedFields : {}
13
+ const b = items[1].protectedFields ? items[1].protectedFields : {}
14
+ const aKeys = Object.keys(a)
15
+ const bKeys = Object.keys(b)
16
+
17
+ const result = {}
18
+ const keys = [...(new Set([...aKeys, ...bKeys]))]
19
+ keys.forEach(key => {
20
+ result[key] = [
21
+ ...(a[key] ? a[key] : []),
22
+ ...(b[key] ? b[key] : []),
23
+ ]
24
+ result[key] = result[key].filter(a => a)
25
+ })
26
+
27
+ return result
28
+ }
29
+
30
+ const dedupePermissions = (items) => {
31
+ const a = items[0] ? items[0] : {}
32
+ const b = items[1] ? items[1] : {}
33
+ const aKeys = Object.keys(a).filter(a => a !== "protectedFields")
34
+ const bKeys = Object.keys(b).filter(a => a !== "protectedFields")
35
+ const result = {}
36
+ const keys = [...(new Set([...aKeys, ...bKeys]))]
37
+ keys.forEach(key => {
38
+ result[key] = dedupePermission(a[key], b[key])
39
+ })
40
+
41
+ return result
42
+ }
43
+
44
+ const dedupePermission = (a, b) => {
45
+ if (!a) {
46
+ return b
47
+ }
48
+
49
+ if (!b) {
50
+ return a
51
+ }
52
+
53
+ const aKeys = Object.keys(a)
54
+ const bKeys = Object.keys(b)
55
+ const result = {}
56
+ const keys = [...(new Set([...aKeys, ...bKeys]))]
57
+
58
+ keys.forEach(key => {
59
+ if (key === 'requiresAuthentication') {
60
+ result.requiresAuthentication = (a.requiresAuthentication || b.aRequiresAuthentication)
61
+ delete result["*"]
62
+ }
63
+
64
+ result[key] = b.hasOwnProperty(key) ? b[key] : a[key]
65
+ })
66
+
67
+ return result
68
+ }