@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,6 @@
1
+ import fs from 'fs'
2
+ export default async (file) => {
3
+ return fs.promises.access(file, fs.constants.F_OK)
4
+ .then(() => true)
5
+ .catch(() => false)
6
+ }
@@ -0,0 +1,19 @@
1
+ import _parse from './parse/index.js'
2
+
3
+ export default class Servable {
4
+
5
+
6
+ constructor() {
7
+ const __Parse = global.Parse ? global.Parse : {}
8
+ this.App = {
9
+ ...__Parse,
10
+ ..._parse
11
+ }
12
+ }
13
+
14
+ async hydrate({ servableConfig, frameworkBridge, app }) {
15
+
16
+ }
17
+
18
+
19
+ }
@@ -0,0 +1,5 @@
1
+ import * as Utils from './utils/index.js'
2
+
3
+ export default {
4
+ Utils
5
+ }
@@ -0,0 +1,216 @@
1
+ export const getOnePlain = async props => {
2
+ const { objectId, className, useMasterKey = false } = props;
3
+ const query = new Servable.App.Query(className);
4
+ query.equalTo("objectId", objectId);
5
+
6
+ return query.first({ useMasterKey });
7
+ };
8
+
9
+ export const formatQuery = props => {
10
+ const { options, query } = props;
11
+ const { sort, limit = 100, skip = 0, page = 0, withCount = false } = options;
12
+ query.limit(limit);
13
+ if (page) {
14
+ query.skip(page * limit);
15
+ } else {
16
+ query.skip(skip);
17
+ }
18
+
19
+ if (withCount) {
20
+ query.withCount();
21
+ }
22
+
23
+ switch (sort) {
24
+ case "desc":
25
+ query.descending("createdAt");
26
+ break;
27
+ default:
28
+ query.ascending("createdAt");
29
+ break;
30
+ }
31
+ };
32
+
33
+ export const getOneGeneric = async ({ objectId, className, include } = {}) => {
34
+ const query = new Servable.App.Query(className);
35
+ include && query.include(include);
36
+ query.equalTo("objectId", objectId);
37
+ return query.first({ useMasterKey: true });
38
+ };
39
+
40
+ export const performBatchOnQuery = async ({
41
+ query,
42
+ batchSize = 100,
43
+ action
44
+ }) => {
45
+ const count = await query.count({ useMasterKey: true });
46
+ const expectedLoops = parseInt(count / batchSize);
47
+ let currentBatchResultsLength = 0;
48
+ let loops = 0;
49
+
50
+ do {
51
+ query.skip(batchSize * loops);
52
+ query.limit(batchSize);
53
+ const items = await query.find({ useMasterKey: true });
54
+ if (items && items.length) {
55
+ await Promise.all(items.map(action));
56
+ }
57
+
58
+ currentBatchResultsLength = items.length;
59
+ loops++;
60
+ } while (loops < expectedLoops || currentBatchResultsLength === batchSize);
61
+ };
62
+
63
+ export const fetchObjectIfNeeded = async ({
64
+ object,
65
+ className,
66
+ forceFetch = false,
67
+ includes = [],
68
+ excludes = [],
69
+ useMasterKey = false
70
+ } = {}) => {
71
+ if (typeof object === "string") {
72
+ return fetchObject({
73
+ objectId: object,
74
+ className,
75
+ includes,
76
+ excludes,
77
+ useMasterKey
78
+ });
79
+ }
80
+
81
+ if (forceFetch) {
82
+ return fetchObject({
83
+ objectId: object.id,
84
+ className,
85
+ includes,
86
+ excludes,
87
+ useMasterKey
88
+ });
89
+ }
90
+
91
+ return object;
92
+ };
93
+
94
+ export const fetchObject = async ({
95
+ objectId,
96
+ className,
97
+ forceFetch,
98
+ includes = [],
99
+ excludes = [],
100
+ useMasterKey = false
101
+ } = {}) => {
102
+ const query = new Servable.App.Query(className);
103
+ query.equalTo("objectId", objectId);
104
+ query.include(includes);
105
+ query.exclude(excludes);
106
+
107
+ return query.first({ useMasterKey });
108
+ };
109
+
110
+ export const destroyRowsWithQuery = async ({ query, limitPerBatch } = {}) => {
111
+ query
112
+ .limit(limitPerBatch)
113
+ .find()
114
+ .then(function(results) {
115
+ return Servable.App.Object.destroyAll(results).then(function() {
116
+ return Promise.resolve(results.length);
117
+ });
118
+ });
119
+ };
120
+
121
+ export const destroyAllRowsWithQuery = async ({
122
+ query,
123
+ limitPerBatch = 1000
124
+ } = {}) => {
125
+ return destroyRowsWithQuery({ query, limitPerBatch }).then(function(count) {
126
+ return count
127
+ ? destroyAllRowsWithQuery({ query, limitPerBatch })
128
+ : Promise.resolve();
129
+ });
130
+ };
131
+
132
+ export const prepareRequestWithUser = async ({
133
+ params,
134
+ headers,
135
+ user,
136
+ fetchOptions = {}
137
+ } = {}) => {
138
+ if (user) {
139
+ if (!fetchOptions.includes && !fetchOptions.excludes) {
140
+ return user;
141
+ }
142
+
143
+ return user.fetchWithInclude(fetchOptions.includes, { useMasterKey: true });
144
+ }
145
+
146
+ const { masterKey, userID } = params;
147
+ const headerMasterKey = headers["x-parse-master-key"];
148
+ const _masterKey = masterKey ? masterKey : headerMasterKey;
149
+ // #TODO: remove process.env
150
+ if (_masterKey !== process.env.SERVABLE_MASTER_KEY) {
151
+ throw new Error("Please provide a master key");
152
+ }
153
+
154
+ const _user = await fetchObjectIfNeeded({
155
+ object: userID,
156
+ className: "_User",
157
+ ...fetchOptions
158
+ });
159
+ if (!_user) {
160
+ throw new Error("Could not find user.");
161
+ }
162
+
163
+ return _user;
164
+ };
165
+
166
+ export const destroyItems = async ({ object, keys }) => {
167
+ return Promise.all(keys.map(key => destroyItem({ key, object })));
168
+ };
169
+
170
+ export const destroyItem = async props => {
171
+ const { object, key } = props;
172
+ const item = object.get(key);
173
+ if (!item) {
174
+ return;
175
+ }
176
+
177
+ return item.destroy({ useMasterKey: true });
178
+ };
179
+
180
+ export const destroyItemsInArray = async props => {
181
+ const { object, key } = props;
182
+ const items = object.get(key);
183
+ if (!items) {
184
+ return;
185
+ }
186
+
187
+ return Promise.all(items.map(_destroyItemInArray));
188
+ };
189
+
190
+ export const saveFileDataToFS = async ({ file, path = "temp/files" }) => {
191
+ if (!file) {
192
+ return null;
193
+ }
194
+
195
+ try {
196
+ const data = await file.getData();
197
+ if (!data) {
198
+ return null;
199
+ }
200
+
201
+ const dirPath = "/uploads";
202
+ if (!fs.existsSync()) {
203
+ await fs.promises.mkdir(dirPath);
204
+ }
205
+
206
+ const filePath = `${dirPath}/${file.name}`;
207
+ await fs.promises.writeFile(filePath, data);
208
+ } catch (e) {
209
+ console.error(e);
210
+ return null;
211
+ }
212
+ };
213
+
214
+ const _destroyItemInArray = async item => {
215
+ return item.destroy({ useMasterKey: true });
216
+ };
package/src/index.js ADDED
@@ -0,0 +1,29 @@
1
+ import extractFeature from './manifest/extract/index.js'
2
+ import accessManifest from './manifest/access/index.js'
3
+ import * as ManifestEnums from './manifest/data/1.0.0/enums.js'
4
+ import * as ManifestEnumsV1_1_0 from './manifest/data/1.0.0/enums.js'
5
+
6
+ import buildSchema from './schema/build/index.js'
7
+ import validateSchema from './schema/validate/index.js'
8
+ // import validateFeature from './lib/config/validate/feature/index.js'
9
+ import cleanFeatures from './lib/cleanFeatures.js'
10
+
11
+
12
+ import documentFeature from './document/gateway/all/index.js'
13
+ import generateGithubReadme from './document/gateway/generateGithubReadme/index.js'
14
+
15
+ import Servable from './domain/servable/index.js'
16
+ const Domain = { Servable }
17
+
18
+ export {
19
+ accessManifest,
20
+ documentFeature,
21
+ extractFeature,
22
+ ManifestEnums,
23
+ ManifestEnumsV1_1_0,
24
+ buildSchema,
25
+ validateSchema,
26
+ cleanFeatures,
27
+ generateGithubReadme,
28
+ Domain
29
+ }
@@ -0,0 +1,85 @@
1
+
2
+ import path from 'path'
3
+ // import callerPath from 'caller-path'
4
+ import { fileURLToPath } from 'url'
5
+ import { dirname } from 'path'
6
+ const __filename = fileURLToPath(import.meta.url)
7
+ const __dirname = dirname(__filename)
8
+
9
+
10
+ export default ({ servableConfig }) => {
11
+ if (servableConfig.adaptedBasic) {
12
+ return
13
+ }
14
+ // const __filename = callerPath({ depth: 1 })
15
+ // let __dirname = dirname(__filename)
16
+ // __dirname = __dirname.split('file://')[1]
17
+
18
+ // if (!servableConfig.nodeModulesPath) {
19
+ // servableConfig.nodeModulesPath = path.resolve(__dirname, `../node_modules`)
20
+ // }
21
+
22
+ if (!servableConfig.distribution) {
23
+ servableConfig.distribution = {
24
+ enabled: false,
25
+ }
26
+ }
27
+
28
+ if (!servableConfig.distribution.databaseURI) {
29
+ servableConfig.distribution.databaseURI = process.env.SERVABLE_UTILS_DATABASE_URI
30
+ }
31
+
32
+
33
+ if (!servableConfig.system) {
34
+ servableConfig.system = {}
35
+ }
36
+
37
+ if (!servableConfig.system.docker) {
38
+ servableConfig.system.docker = {
39
+ enabled: true,
40
+ environments: ['development']
41
+ }
42
+ }
43
+
44
+ if (!servableConfig.features) {
45
+ servableConfig.features = {}
46
+ }
47
+
48
+ if (!servableConfig.features.local || !servableConfig.features.local.length) {
49
+ servableConfig.features.local = [
50
+ path.resolve('', 'lib/features')
51
+ ]
52
+ }
53
+
54
+ servableConfig.features.local = [
55
+ path.resolve(__dirname, `../../features`),
56
+ ...servableConfig.features.local,
57
+ ]
58
+
59
+ if (!servableConfig.rootFeaturePayload) {
60
+ servableConfig.rootFeaturePayload = {
61
+ type: 'app',
62
+ id: 'app',
63
+ // path: path.resolve(__dirname, "./app")
64
+ path: path.resolve('', 'lib/app')
65
+ }
66
+ }
67
+
68
+ if (!servableConfig.rootFeaturePayload.path) {
69
+ servableConfig.rootFeaturePayload = {
70
+ ...servableConfig.rootFeaturePayload,
71
+ path: path.resolve('', 'lib/app')
72
+ // path: path.resolve(__dirname, "./app")
73
+ }
74
+ }
75
+
76
+ if (!servableConfig.rootFeaturePayload.id || !servableConfig.rootFeaturePayload.type) {
77
+ servableConfig.rootFeaturePayload = {
78
+ ...servableConfig.rootFeaturePayload,
79
+ id: 'app',
80
+ type: 'app',
81
+ }
82
+ }
83
+
84
+ servableConfig.adaptedBasic = true
85
+ }
@@ -0,0 +1,18 @@
1
+ export default (payload) => {
2
+ if (typeof payload === 'object') {
3
+ return payload
4
+ }
5
+
6
+ if (typeof payload === 'string') {
7
+ return {
8
+ id: payload,
9
+ slug: payload,
10
+ name: payload,
11
+ params: {
12
+
13
+ }
14
+ }
15
+ }
16
+
17
+ return payload
18
+ }
@@ -0,0 +1,6 @@
1
+ import fs from 'fs'
2
+ export default async (file) => {
3
+ return fs.promises.access(file, fs.constants.F_OK)
4
+ .then(() => true)
5
+ .catch(() => false)
6
+ }
@@ -0,0 +1,25 @@
1
+ export default (items) => {
2
+ if (!items) {
3
+ return null
4
+ }
5
+ let _items = items.filter(a => a)
6
+ const res = []
7
+ const ids = []
8
+ _items.forEach(i => {
9
+ if (!i.id) {
10
+ ids.push(i.id)
11
+ res.push(i)
12
+ return
13
+ }
14
+
15
+ if (!ids.includes(i.id)) {
16
+ ids.push(i.id)
17
+ res.push(i)
18
+ }
19
+ else {
20
+ // console.log("[Servable]", '')
21
+ }
22
+ })
23
+ // _items = _.uniq(_items, a => a.id)
24
+ return res
25
+ }
@@ -0,0 +1,49 @@
1
+ import checkFileExists from '../domain/lib/checkFileExists.js'
2
+ import fs from 'fs'
3
+ import _path from 'path'
4
+ import _ from 'underscore'
5
+
6
+ const perform = async ({ path, exlusions = [] }) => {
7
+ try {
8
+ if (!(await checkFileExists(path))) {
9
+ return null
10
+ }
11
+
12
+ const items = await fs.promises.readdir(path)
13
+
14
+ if (!items || !items.length) {
15
+ return null
16
+ }
17
+
18
+ let results = (await Promise.all(items.map(async item => {
19
+ if (exlusions.includes(item)
20
+ || item.includes('.js')
21
+ || item.includes('.json')) {
22
+ return null
23
+ }
24
+
25
+ const __path = _path.join(path, item)
26
+
27
+ const stat = await fs.promises.stat(__path)
28
+ if (!stat) {
29
+ return null
30
+ }
31
+
32
+ const isDir = stat.isDirectory()
33
+ if (!isDir) {
34
+ return null
35
+ }
36
+
37
+ return { name: item, stat }
38
+ }))).filter(a => a)
39
+
40
+ return results
41
+ }
42
+ catch (e) {
43
+ console.error(e)
44
+ return null
45
+ }
46
+ }
47
+
48
+
49
+ export default perform
@@ -0,0 +1,83 @@
1
+ import checkFileExists from './checkFileExists.js'
2
+ import fs from 'fs'
3
+ import _path from 'path'
4
+ import _ from 'underscore'
5
+ //https://www.npmjs.com/package/directory-import
6
+
7
+
8
+ const perform = async ({ path,
9
+ includeMeta = false,
10
+ excludes = ['spec.js'],
11
+ includeExtensions = ['.js', '.ts', '.json']
12
+ }) => {
13
+ try {
14
+ if (!(await checkFileExists(path))) {
15
+ return null
16
+ }
17
+
18
+ const items = await fs.promises.readdir(path)
19
+
20
+ if (!items || !items.length) {
21
+ return null
22
+ }
23
+
24
+ let results = (await Promise.all(items.map(async item => {
25
+
26
+
27
+ const __path = _path.join(path, item)
28
+
29
+ const _stat = await fs.promises.stat(__path)
30
+ if (!_stat) {
31
+ return null
32
+ }
33
+
34
+ const isDir = _stat.isDirectory()
35
+ if (isDir) {
36
+ return perform({ path: __path })
37
+ }
38
+
39
+ if (!(await checkFileExists(__path))) {
40
+ return null
41
+ }
42
+
43
+ if (item.includes('spec.js')) {
44
+ return null
45
+ }
46
+ const extension = getExtension(item)
47
+ if (!includeExtensions.includes(extension)) {
48
+ return null
49
+ }
50
+
51
+ if (includeMeta) {
52
+ const racine = getFileRaw(__path)
53
+ const _module = await import(__path)
54
+ const md = `${racine}.md`
55
+ let documentation = null
56
+ if ((await checkFileExists(md))) {
57
+ documentation = await fs.promises.readFile(md, 'utf8')
58
+ }
59
+
60
+ return [{
61
+ module: _module,
62
+ path: __path,
63
+ documentation
64
+ }]
65
+ } else {
66
+ return [(await import(__path))]
67
+ }
68
+ }))).filter(a => a)
69
+
70
+ results = _.flatten(results)
71
+ return results
72
+ }
73
+ catch (e) {
74
+ console.error(e)
75
+ return null
76
+ }
77
+ }
78
+
79
+ const getExtension = str => str.slice(str.lastIndexOf("."))
80
+ const getFileRaw = str => str.split('.').slice(0, -1).join('.')
81
+
82
+
83
+ export default perform
@@ -0,0 +1,14 @@
1
+ import fg from 'fast-glob'
2
+
3
+ export default async ({ path,
4
+ globOptions = { mark: true },
5
+ }) => {
6
+ try {
7
+ const entries = await fg([path], globOptions,)
8
+ return Promise.all(entries.map(entry => import(entry)))
9
+ }
10
+ catch (e) {
11
+ console.error(e)
12
+ return null
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import checkFileExists from './checkFileExists'
4
+
5
+ const operation = async filePath => {
6
+ var dirname = path.dirname(filePath)
7
+ if (!(await checkFileExists(dirname))) {
8
+ return true
9
+ }
10
+ ensureDirectoryExistence(dirname)
11
+ fs.promises.mkdir(dirname)
12
+ }
13
+
14
+ export default operation
@@ -0,0 +1,30 @@
1
+ import checkFileExists from "./checkFileExists.js"
2
+ import importJSONAsync from "./importJSONAsync.js"
3
+ import semver from "semver"
4
+
5
+ export default async ({ path }) => {
6
+
7
+ let modulePath = `${path}/manifest.json`
8
+ if (!(await checkFileExists(modulePath))) {
9
+ modulePath = `${path}/index.json`
10
+ if (!(await checkFileExists(modulePath))) {
11
+ modulePath = `${path}/module.json`
12
+ if (!(await checkFileExists(modulePath))) {
13
+ return null
14
+ }
15
+ }
16
+ }
17
+
18
+ const module = await importJSONAsync(modulePath)
19
+ if (!module) {
20
+ return null
21
+ }
22
+
23
+ let value = module.apiVersion
24
+ if (!value || semver.valid(value)) {
25
+ return "1.0.0"
26
+ }
27
+
28
+ return value
29
+
30
+ }
@@ -0,0 +1,46 @@
1
+ import checkFileExists from './checkFileExists.js'
2
+ import fs from 'fs'
3
+ import _path from 'path'
4
+ import _ from 'underscore'
5
+
6
+ export default async ({ path, }) => {
7
+ try {
8
+ if (!(await checkFileExists(path))) {
9
+ return null
10
+ }
11
+
12
+ const items = await fs.promises.readdir(path)
13
+
14
+ if (!items || !items.length) {
15
+ return null
16
+ }
17
+
18
+ let results = (await Promise.all(items.map(async item => {
19
+ if (item.includes('spec.js')) {
20
+ return null
21
+ }
22
+
23
+ const __path = _path.join(path, item)
24
+
25
+ const _stat = await fs.promises.stat(__path)
26
+ if (!_stat) {
27
+ return null
28
+ }
29
+
30
+ const isDir = _stat.isDirectory()
31
+ if (!isDir) {
32
+ return null
33
+ }
34
+
35
+ return { path: __path, stat: _stat, name: item }
36
+
37
+ }))).filter(a => a)
38
+
39
+ results = _.flatten(results)
40
+ return results
41
+ }
42
+ catch (e) {
43
+ console.error(e)
44
+ return null
45
+ }
46
+ }
@@ -0,0 +1,19 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import { createRequire } from 'module'
4
+
5
+ /**
6
+ * Get's the file path to a module folder.
7
+ * @param {string} moduleEntry
8
+ * @param {string} fromFile
9
+ */
10
+ export default (moduleEntry) => {
11
+ const packageName = moduleEntry.includes('/')
12
+ ? moduleEntry.startsWith('@')
13
+ ? moduleEntry.split('/').slice(0, 2).join('/')
14
+ : moduleEntry.split('/')[0]
15
+ : moduleEntry;
16
+ const require = createRequire(import.meta.url);
17
+ const lookupPaths = require.resolve.paths(moduleEntry).map((p) => path.join(p, packageName));
18
+ return lookupPaths.find((p) => fs.existsSync(p));
19
+ };
@@ -0,0 +1,15 @@
1
+ import fs from "fs"
2
+ import checkFileExists from "./checkFileExists.js"
3
+
4
+ export default async _url => {
5
+ try {
6
+ if (!(await checkFileExists(_url))) {
7
+ return null
8
+ }
9
+ const fileUrl = new URL(_url, import.meta.url)
10
+ return fs.promises.readFile(fileUrl, 'utf8')
11
+ } catch (e) {
12
+ console.log("[Servable]", `importFileAsText → `, e)
13
+ return null
14
+ }
15
+ }