mushroom-cli 1.2.0-alpha.11 → 1.2.0-alpha.12

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 (43) hide show
  1. package/README.md +233 -223
  2. package/dist/check-update.js +5 -0
  3. package/dist/common.js +260 -0
  4. package/dist/config.js +394 -0
  5. package/{docs → dist/docs}/doc-mt.js +471 -523
  6. package/dist/docs/docs.v2.js +1758 -0
  7. package/{docs → dist/docs}/mt-dom-1.0.js +1025 -1268
  8. package/{docs → dist/docs}/mt.extension.js +142 -174
  9. package/dist/gen/gen-api-docs-v1.js +106 -0
  10. package/dist/gen/gen-api-docs.js +292 -0
  11. package/{gen → dist/gen}/gen-csharp.js +1265 -1399
  12. package/{gen → dist/gen}/gen-db-docs.js +204 -222
  13. package/{gen → dist/gen}/gen-js-driver-tarball.js +81 -101
  14. package/{gen → dist/gen}/gen-js-driver.js +116 -133
  15. package/{gen → dist/gen}/gen-ts-driver.js +427 -467
  16. package/{gen.js → dist/gen.js} +109 -124
  17. package/{help.js → dist/help.js} +53 -55
  18. package/dist/init/init-json.js +91 -0
  19. package/dist/init/init-netcore.js +30 -0
  20. package/dist/init.js +23 -0
  21. package/dist/master-file/master-file.js +185 -0
  22. package/dist/merge.js +70 -0
  23. package/dist/mr.js +75 -0
  24. package/dist/types.js +1 -0
  25. package/dist/update/tsCode.js +82 -0
  26. package/dist/update.js +20 -0
  27. package/package.json +54 -29
  28. package/check-update.js +0 -9
  29. package/common.js +0 -283
  30. package/config.js +0 -445
  31. package/docs/docs.html +0 -5190
  32. package/docs/docs.v2.html +0 -5095
  33. package/docs/docs.v2.js +0 -1633
  34. package/gen/gen-api-docs-v1.js +0 -125
  35. package/gen/gen-api-docs.js +0 -298
  36. package/init/init-json.js +0 -106
  37. package/init/init-netcore.js +0 -32
  38. package/init.js +0 -28
  39. package/master-file/master-file.js +0 -234
  40. package/merge.js +0 -84
  41. package/mr.js +0 -82
  42. package/update/tsCode.js +0 -20
  43. package/update.js +0 -24
@@ -0,0 +1,292 @@
1
+ import fs from 'fs';
2
+ import open from 'open';
3
+ import * as common from '../common.js';
4
+ import { minify } from 'minify';
5
+ import path from 'path';
6
+ function reserveWhitelist(obj, whitelist) {
7
+ if (!obj && typeof obj !== 'object')
8
+ return null;
9
+ for (const m in obj) {
10
+ if (!whitelist.includes(m))
11
+ delete obj[m];
12
+ }
13
+ return obj;
14
+ }
15
+ const environmentVariableWhitelist = ['type', 'description'];
16
+ function filterEnvironmentVariables(environmentVariables) {
17
+ if (!environmentVariables)
18
+ return;
19
+ Object.values(environmentVariables).forEach((env) => reserveWhitelist(env, environmentVariableWhitelist));
20
+ }
21
+ const entityWhitelist = ['name', 'description', 'actions', 'views', 'features', 'fields'];
22
+ const actionWhitelist = ['grant', 'deny', 'matches', 'description', 'exclude'];
23
+ const findByIdWhitelist = [...actionWhitelist, 'returnableFields', 'defaultFields'];
24
+ const findManyWhitelist = [
25
+ ...findByIdWhitelist,
26
+ 'allowFilteringFields',
27
+ 'requiredFilteringFields',
28
+ 'allowSortings',
29
+ 'defaultSortings',
30
+ 'pagingType',
31
+ 'maxPageSize',
32
+ 'pageSize',
33
+ 'includeTotal',
34
+ 'cache',
35
+ ];
36
+ const generalWhitelist = [...actionWhitelist, 'codeHint'];
37
+ const entityFeatureWhitelist = ['vn_text'];
38
+ const vnTextWhitelist = ['fields', 'channels'];
39
+ const vnTextChannelWhitelist = ['fields', 'name'];
40
+ const actionCodeHintWhitelist = ['delegator'];
41
+ const actionCodeHintDelegatorWhitelist = ['param', 'requestBody'];
42
+ const fieldsOnlyWhitelist = ['fields'];
43
+ const viewWhitelist = ['grant', 'deny', 'description', 'parameters', 'emptyResult', 'codeHint'];
44
+ const viewCodeHintWhitelist = ['output'];
45
+ function filterEntities(entities) {
46
+ Object.values(entities).forEach((entity) => {
47
+ filterEntityFields(entity);
48
+ if (!reserveWhitelist(entity, entityWhitelist))
49
+ return;
50
+ const entityActions = entity.actions ?? {};
51
+ Object.keys(entityActions).forEach((actionName) => {
52
+ const actions = entityActions[actionName];
53
+ switch (actionName) {
54
+ case 'findMany':
55
+ actions.forEach((action) => reserveWhitelist(action, findManyWhitelist));
56
+ break;
57
+ case 'findById':
58
+ actions.forEach((action) => reserveWhitelist(action, findByIdWhitelist));
59
+ break;
60
+ case 'createOne':
61
+ case 'createMany':
62
+ case 'updateOne':
63
+ case 'updateMany':
64
+ case 'updatePartially':
65
+ case 'deleteOne':
66
+ case 'deleteMany':
67
+ actions.forEach((action) => reserveWhitelist(action, actionWhitelist));
68
+ break;
69
+ default:
70
+ actions.forEach((action) => {
71
+ reserveWhitelist(action, generalWhitelist);
72
+ if (!action.codeHint)
73
+ return;
74
+ if (!reserveWhitelist(action.codeHint, actionCodeHintWhitelist))
75
+ return;
76
+ if (!action.codeHint.delegator)
77
+ return;
78
+ if (!reserveWhitelist(action.codeHint.delegator, actionCodeHintDelegatorWhitelist))
79
+ return;
80
+ reserveWhitelist(action.codeHint.delegator.param, fieldsOnlyWhitelist);
81
+ reserveWhitelist(action.codeHint.delegator.requestBody, fieldsOnlyWhitelist);
82
+ });
83
+ break;
84
+ }
85
+ });
86
+ const entityViews = entity.views ?? {};
87
+ Object.keys(entityViews).forEach((viewName) => {
88
+ const views = entityViews[viewName];
89
+ views.forEach((view) => {
90
+ reserveWhitelist(view, viewWhitelist);
91
+ if (!view.codeHint)
92
+ return;
93
+ if (!reserveWhitelist(view.codeHint, viewCodeHintWhitelist))
94
+ return;
95
+ reserveWhitelist(view.codeHint.output, fieldsOnlyWhitelist);
96
+ });
97
+ });
98
+ if (entity.features) {
99
+ reserveWhitelist(entity.features, entityFeatureWhitelist);
100
+ reserveWhitelist(entity.features.vn_text, vnTextWhitelist);
101
+ entity.features.vn_text.channels?.forEach((channel) => reserveWhitelist(channel, vnTextChannelWhitelist));
102
+ }
103
+ });
104
+ }
105
+ function flattenFields(fields, dict, prefix) {
106
+ dict ?? (dict = {});
107
+ prefix ?? (prefix = '');
108
+ for (const m in fields) {
109
+ const path = prefix ? prefix + '.' + m : m;
110
+ dict[path] = false;
111
+ const config = fields[m];
112
+ if (config.type == 'object') {
113
+ flattenFields(config.fields ?? {}, dict, path);
114
+ }
115
+ else if (config.type == 'array' && config.items && config.items.type == 'object') {
116
+ flattenFields(config.items.fields ?? {}, dict, path);
117
+ }
118
+ }
119
+ return dict;
120
+ }
121
+ function removeDisallowedFields(fields, dict) {
122
+ const allowFields = Object.keys(dict);
123
+ if (!allowFields.length)
124
+ return;
125
+ reserveWhitelist(fields, allowFields);
126
+ for (const m in fields) {
127
+ const config = fields[m];
128
+ if (config.type == 'object') {
129
+ removeDisallowedFields(config.fields ?? {}, dict[m]);
130
+ }
131
+ else if (config.type == 'array' && config.items && config.items.type == 'object') {
132
+ removeDisallowedFields(config.items.fields ?? {}, dict[m]);
133
+ }
134
+ }
135
+ }
136
+ function markAllowFields(allowFields, dict) {
137
+ if (!Array.isArray(allowFields))
138
+ return;
139
+ allowFields.forEach((field) => (dict[field] = true));
140
+ }
141
+ function markIncludedFields(excludedFields, dict) {
142
+ if (!Array.isArray(excludedFields))
143
+ return;
144
+ for (const m in dict) {
145
+ if (!dict[m] && !excludedFields.includes(m))
146
+ dict[m] = true;
147
+ }
148
+ }
149
+ function filterEntityFields(entity) {
150
+ const dict = flattenFields(entity.fields);
151
+ const entityActions = entity.actions ?? {};
152
+ Object.keys(entityActions).forEach((actionName) => {
153
+ const actions = entityActions[actionName];
154
+ switch (actionName) {
155
+ case 'findMany':
156
+ case 'findById':
157
+ actions.forEach((action) => {
158
+ markAllowFields(action.returnableFields, dict);
159
+ markAllowFields(action.defaultFields, dict);
160
+ });
161
+ break;
162
+ case 'createOne':
163
+ case 'createMany':
164
+ case 'updateOne':
165
+ case 'updateMany':
166
+ case 'updatePartially':
167
+ actions.forEach((action) => markIncludedFields(action.exclude, dict));
168
+ break;
169
+ }
170
+ });
171
+ const allowFields = Object.keys(dict).filter((m) => dict[m]);
172
+ [...allowFields].forEach((shortField) => {
173
+ shortField += '.';
174
+ allowFields
175
+ .map((longerField, i) => (longerField.includes(shortField) ? i : -1))
176
+ .filter((i) => i !== -1)
177
+ .reverse()
178
+ .forEach((i) => allowFields.splice(i, 1));
179
+ });
180
+ const whitelist = {};
181
+ allowFields.forEach((fields) => {
182
+ let obj = whitelist;
183
+ fields.split('.').forEach((name) => {
184
+ let child = obj[name];
185
+ if (!child)
186
+ child = obj[name] = {};
187
+ obj = child;
188
+ });
189
+ });
190
+ Object.keys(entityActions).forEach((actionName) => {
191
+ const actions = entityActions[actionName];
192
+ switch (actionName) {
193
+ case 'createOne':
194
+ case 'createMany':
195
+ case 'updateOne':
196
+ case 'updateMany':
197
+ case 'updatePartially':
198
+ actions.forEach((action) => (action.exclude = action.exclude?.filter((field) => allowFields.includes(field))));
199
+ break;
200
+ }
201
+ });
202
+ removeDisallowedFields(entity.fields, whitelist);
203
+ }
204
+ async function writeDocs(filePath, strTemplate, strData) {
205
+ const placeholder = '<script id="INJECT-JAVASCRIPT-HERE"></script>';
206
+ const idx = strTemplate.indexOf(placeholder);
207
+ fs.writeFileSync(filePath, strTemplate.substring(0, idx));
208
+ fs.appendFileSync(filePath, '<script>window.docs = ');
209
+ fs.appendFileSync(filePath, strData);
210
+ fs.appendFileSync(filePath, ';\n');
211
+ fs.appendFileSync(filePath, fs.readFileSync(path.join(__dirname, '../docs/docs.v2.js')));
212
+ fs.appendFileSync(filePath, '</script>');
213
+ fs.appendFileSync(filePath, strTemplate.substring(idx + placeholder.length));
214
+ fs.writeFileSync(filePath, await minify(filePath, {
215
+ html: {
216
+ removeComments: true,
217
+ collapseWhitespace: true,
218
+ collapseBooleanAttributes: true,
219
+ removeAttributeQuotes: true,
220
+ removeRedundantAttributes: true,
221
+ useShortDoctype: true,
222
+ removeEmptyAttributes: true,
223
+ removeEmptyElements: false,
224
+ removeOptionalTags: true,
225
+ removeScriptTypeAttributes: false,
226
+ removeStyleLinkTypeAttributes: true,
227
+ minifyJS: true,
228
+ minifyCSS: true,
229
+ html5: true,
230
+ },
231
+ }));
232
+ }
233
+ export default async function genDocs(args, projectName, project, alias, source) {
234
+ const aliasProject = ((project.docs || {}).alias || {})[alias];
235
+ if (!aliasProject) {
236
+ throw new Error(`Not found gen docs configuration. Please run 'npx mushroom-cli config ${projectName} gen docs OUTPUT_FILE.html${alias === 'default' ? '' : ' alias ' + alias}' first`);
237
+ }
238
+ common.logInfo('Use GenDocEngine v1.2');
239
+ const entities = common.getEntities(source, aliasProject.allowRoles);
240
+ if (Array.isArray(aliasProject.entities)) {
241
+ for (const entityName in entities) {
242
+ if (Object.hasOwnProperty.call(entities, entityName)) {
243
+ if (aliasProject.entities.every((re) => !new RegExp(re).test(entityName))) {
244
+ delete entities[entityName];
245
+ }
246
+ }
247
+ }
248
+ }
249
+ filterEnvironmentVariables(source.environmentVariables);
250
+ filterEntities(entities);
251
+ const data = JSON.stringify({
252
+ info: {
253
+ name: source.name,
254
+ rootUrl: source.codeHint?.rootUrl ?? source.rootUrl,
255
+ description: source.description,
256
+ security: source.security === undefined
257
+ ? undefined
258
+ : {
259
+ crossDomain: source.security.crossDomain,
260
+ allowOrigins: source.security.allowOrigins,
261
+ },
262
+ },
263
+ features: aliasProject.features,
264
+ roles: common.filterRoles(source.roles ?? [], aliasProject.allowRoles ?? ['*']),
265
+ environmentVariables: source.environmentVariables,
266
+ entities: entities,
267
+ });
268
+ const html = fs.readFileSync(__dirname + '/../docs/docs.v2.html').toString();
269
+ if (args[3] === 'tmp') {
270
+ const htmlFilePath = aliasProject.output + '_tmp.html';
271
+ common.logInfo('Writing to', htmlFilePath);
272
+ await writeDocs(htmlFilePath, html, data);
273
+ common.logInfo('Opening file ', htmlFilePath);
274
+ try {
275
+ await open(htmlFilePath);
276
+ setTimeout(() => {
277
+ fs.unlink(htmlFilePath, (ex) => {
278
+ if (ex)
279
+ common.logError('Unable to delete file', htmlFilePath);
280
+ });
281
+ }, 1000);
282
+ }
283
+ catch {
284
+ common.logError('Unable to open', htmlFilePath);
285
+ }
286
+ return;
287
+ }
288
+ common.ensureFolder(aliasProject.output);
289
+ common.logInfo('Writing to', aliasProject.output);
290
+ await writeDocs(aliasProject.output, html, data);
291
+ common.logInfo(`Generated API docs for ${projectName}@${alias} to ${aliasProject.output}`);
292
+ }