mushroom-cli 1.2.0-alpha.2 → 1.2.0-alpha.20

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