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

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.
@@ -63,37 +63,38 @@ function getCSharpTypeFromMushroomType(type) {
63
63
  return "UNKNOWN_TYPE_" + type;
64
64
  }
65
65
 
66
- function generateTypescriptClass(objectDefination, indent, options) {
67
- if (!indent) indent = "";
68
- let code = "{\n";
66
+ function generateTypescriptClass(objectDefination, className, options) {
67
+ let childrenCode = '';
68
+ let code = `export interface ${className} {\n`;
69
69
  for (let fieldName in objectDefination) {
70
70
  const optionalMark = options?.checkRequired && objectDefination[fieldName].required ? "" : "?";
71
71
  if (objectDefination[fieldName].type == "object") {
72
- code += `${indent} ${fieldName}${optionalMark}: ${generateTypescriptClass(objectDefination[fieldName].fields, indent + " ")},
73
- `;
72
+ const fieldClassName = className + "_" + toTypeScriptClassName(fieldName);
73
+ code += ` ${fieldName}${optionalMark}: ${fieldClassName},\n`;
74
+ childrenCode += generateTypescriptClass(objectDefination[fieldName].fields, fieldClassName);
74
75
  }
75
76
  else if (objectDefination[fieldName].type == "array") {
76
77
  if (objectDefination[fieldName].items.type == "object") {
77
- code += `${indent} ${fieldName}${optionalMark}: ${generateTypescriptClass(objectDefination[fieldName].items.fields, indent + " ")}[],
78
- `;
78
+ const fieldClassName = className + "_" + toTypeScriptClassName(fieldName);
79
+ code += ` ${fieldName}${optionalMark}: ${fieldClassName}[],\n`;
80
+ childrenCode += generateTypescriptClass(objectDefination[fieldName].items.fields, fieldClassName);
79
81
  }
80
82
  else {
81
- code += `${indent} ${fieldName}${optionalMark}: ${getCSharpTypeFromMushroomType(objectDefination[fieldName].items.type)}[],
82
- `;
83
+ code += ` ${fieldName}${optionalMark}: ${getCSharpTypeFromMushroomType(objectDefination[fieldName].items.type)}[],\n`;
83
84
  }
84
85
  }
85
86
  else {
86
- code += `${indent} ${fieldName}${optionalMark}: ${getCSharpTypeFromMushroomType(objectDefination[fieldName].type)},
87
- `;
87
+ code += ` ${fieldName}${optionalMark}: ${getCSharpTypeFromMushroomType(objectDefination[fieldName].type)},\n`;
88
88
  }
89
89
  }
90
- code += indent + "}";
91
- return code;
90
+ code += "}\n\n";
91
+
92
+ return code + childrenCode;
92
93
  }
93
94
 
94
95
  function generateTypescriptClassFromViewParameters(parameters) {
95
96
  if (Array.isArray(parameters)){
96
- return '{\n' + parameters.map(p => ` ${p.field}${p.validation?.required ? "" : "?"}: ${getCSharpTypeFromMushroomType(p.type || "string")}${p.isArray ? "[]" : ""}`).join(',\n') + '\n}';
97
+ return '{\n' + parameters.map(p => ` ${p.field}${p.validation?.required && p.defaultValue === undefined ? "" : "?"}: ${getCSharpTypeFromMushroomType(p.type || "string")}${p.isArray ? "[]" : ""}`).join(',\n') + '\n}';
97
98
  }
98
99
  return '{}';
99
100
  }
@@ -125,7 +126,7 @@ function genTypescriptDriverString(aliasProject, source) {
125
126
  }
126
127
  }
127
128
 
128
- let code = `import mushroom, { defineAsyncResource, defineAsyncView, fireEvent, createRestfulAsyncFunction, IdType, IMushroom, Mushroom, MushroomRequest, MushroomRequestSetting, MushroomRestfulRequest, MushroomResponse, MushroomResourceBase, MushroomListResource, MushroomFindByIdResource, MushroomCreateResource, MushroomBatchCreateResource, MushroomUpdateResource, MushroomBatchUpdateResource, MushroomPartialUpdateResource, MushroomDeleteResource, MushroomBatchDeleteResource, MushroomExtensibleResource, MushroomViewFunction, MushroomError } from "mushroomjs";
129
+ let code = `import mushroom, { defineAsyncResource, defineAsyncView, fireEvent, createRestfulAsyncFunction, IdType, IMushroom, Mushroom, MushroomRequest, MushroomRequestSetting, MushroomRestfulRequest, MushroomResponse, MushroomResponseDataType, MushroomResourceBase, MushroomListResource, MushroomFindByIdResource, MushroomCreateResource, MushroomBatchCreateResource, MushroomUpdateResource, MushroomBatchUpdateResource, MushroomPartialUpdateResource, MushroomDeleteResource, MushroomBatchDeleteResource, MushroomExtensibleResource, MushroomViewFunction, MushroomError, IMushroomOfflineResponse, MushroomOfflineResponse, MushroomOfflineResponseState } from "mushroomjs";
129
130
  ${extensionsImportLines.join("\n")}
130
131
  export * from "mushroomjs";
131
132
  export const extensions = {
@@ -143,9 +144,7 @@ export type IdValue = ${source.idType == "AutoIncInt" || source.idType == "AutoI
143
144
 
144
145
  const entity = entities[entityName];
145
146
  const typescriptClassName = toTypeScriptClassName(entityName);
146
- code += `export interface ${typescriptClassName} ${generateTypescriptClass(entity.fields)};
147
- `;
148
-
147
+ code += generateTypescriptClass(entity.fields, typescriptClassName);
149
148
 
150
149
  const actions = {};
151
150
  iApiClassBody += ` ${entityName}: MushroomResourceBase`;
@@ -156,7 +155,12 @@ export type IdValue = ${source.idType == "AutoIncInt" || source.idType == "AutoI
156
155
 
157
156
  actions[actionName] = {
158
157
  clientCache: (actionName === "findById" || actionName === "findMany") && isAllowCache(entity.actions[actionName], true) ? true : undefined,
159
- includeTotal: actionName === "findMany" && entity.actions[actionName].some(a => a.includeTotal) ? true: undefined
158
+ includeTotal: actionName === "findMany" && entity.actions[actionName].some(a => a.includeTotal) ? true: undefined,
159
+ offlineMode: entity.actions[actionName].some(a => a.offlineMode === "disallow")
160
+ ? "disallow"
161
+ : !entity.actions[actionName].length || entity.actions[actionName].some(a => a.offlineMode === "allow")
162
+ ? "allow"
163
+ : undefined
160
164
  };
161
165
 
162
166
  switch (actionName) {
@@ -233,8 +237,8 @@ ${customMethodCode}}
233
237
  code += `export interface ${inputType} ${generateTypescriptClassFromViewParameters(view.parameters)};
234
238
  `;
235
239
  if (view.codeHint?.output)
236
- code += `export interface ${outputType} ${generateTypescriptClass(view.codeHint.output.fields, undefined, {checkRequired: true})};
237
- `;
240
+ code += generateTypescriptClass(view.codeHint.output.fields, outputType, {checkRequired: true});
241
+
238
242
  return `MushroomViewFunction<${outputType}, ${inputType}>`;
239
243
  });
240
244
  viewCode += `
@@ -299,7 +303,7 @@ function genDtsString(aliasProject, source) {
299
303
  }
300
304
  }
301
305
 
302
- let code = `import { IdType, IMushroom, Mushroom, MushroomRequest, MushroomRequestSetting, MushroomRestfulRequest, MushroomResponse, MushroomResourceBase, MushroomListResource, MushroomFindByIdResource, MushroomCreateResource, MushroomBatchCreateResource, MushroomUpdateResource, MushroomBatchUpdateResource, MushroomPartialUpdateResource, MushroomDeleteResource, MushroomBatchDeleteResource, MushroomExtensibleResource, MushroomViewFunction, MushroomError } from "mushroomjs";
306
+ let code = `import { IdType, IMushroom, Mushroom, MushroomRequest, MushroomRequestSetting, MushroomRestfulRequest, MushroomResponse, MushroomResponseDataType, MushroomResourceBase, MushroomListResource, MushroomFindByIdResource, MushroomCreateResource, MushroomBatchCreateResource, MushroomUpdateResource, MushroomBatchUpdateResource, MushroomPartialUpdateResource, MushroomDeleteResource, MushroomBatchDeleteResource, MushroomExtensibleResource, MushroomViewFunction, MushroomError, IMushroomOfflineResponse, MushroomOfflineResponse, MushroomOfflineResponseState } from "mushroomjs";
303
307
  ${extensionsImportLines.join("\n")}
304
308
  export * from "mushroomjs";
305
309
  export declare const extensions = {
@@ -317,9 +321,7 @@ export declare type IdValue = ${source.idType == "AutoIncInt" || source.idType =
317
321
 
318
322
  const entity = entities[entityName];
319
323
  const typescriptClassName = toTypeScriptClassName(entityName);
320
- code += `export interface ${typescriptClassName} ${generateTypescriptClass(entity.fields)};
321
- `;
322
-
324
+ code += generateTypescriptClass(entity.fields, typescriptClassName);
323
325
 
324
326
  const actions = {};
325
327
  iApiClassBody += ` ${entityName}: MushroomResourceBase`;
@@ -329,7 +331,13 @@ export declare type IdValue = ${source.idType == "AutoIncInt" || source.idType =
329
331
  continue;
330
332
 
331
333
  actions[actionName] = {
332
- clientCache: (actionName === "findById" || actionName === "findMany") && isAllowCache(entity.actions[actionName], true) ? true : undefined
334
+ clientCache: (actionName === "findById" || actionName === "findMany") && isAllowCache(entity.actions[actionName], true) ? true : undefined,
335
+ includeTotal: actionName === "findMany" && entity.actions[actionName].some(a => a.includeTotal) ? true: undefined,
336
+ offlineMode: entity.actions[actionName].some(a => a.offlineMode === "disallow")
337
+ ? "disallow"
338
+ : !entity.actions[actionName].length || entity.actions[actionName].some(a => a.offlineMode === "allow")
339
+ ? "allow"
340
+ : undefined
333
341
  };
334
342
 
335
343
  switch (actionName) {
@@ -403,11 +411,10 @@ ${customMethodCode}}
403
411
  const viewParameterTypes = viewRoles.map(view => {
404
412
  const inputType = `${typescriptClassName}$ViewsResource_${viewName}View_${view.grant === "*" ? "" : toTypeScriptClassName(view.grant)}Parameters`;
405
413
  const outputType = view.codeHint?.output ? `${typescriptClassName}$ViewsResource_${viewName}View_${view.grant === "*" ? "" : toTypeScriptClassName(view.grant)}Result` : 'any';
406
- code += `export interface ${inputType} ${generateTypescriptClassFromViewParameters(view.parameters)};
407
- `;
414
+ code += `export interface ${inputType} ${generateTypescriptClassFromViewParameters(view.parameters)};\n`;
408
415
  if (view.codeHint?.output)
409
- code += `export interface ${outputType} ${generateTypescriptClass(view.codeHint.output.fields, undefined, {checkRequired: true})};
410
- `;
416
+ code += generateTypescriptClass(view.codeHint.output.fields, outputType, {checkRequired: true});
417
+
411
418
  return `MushroomViewFunction<${outputType}, ${inputType}>`;
412
419
  });
413
420
  viewCode += `
package/gen.js CHANGED
@@ -6,6 +6,7 @@ const help = require("./help.js");
6
6
  const masterFile = require("./master-file/master-file");
7
7
 
8
8
  const genApiDocs = require("./gen/gen-api-docs");
9
+ const genApiDocsV1 = require("./gen/gen-api-docs-v1");
9
10
  const genDatabaseDocs = require("./gen/gen-db-docs");
10
11
  const javascriptDriverGenerator = require("./gen/gen-js-driver");
11
12
  const genJavascriptDriverTarball = require("./gen/gen-js-driver-tarball");
@@ -85,7 +86,10 @@ async function gen(args) {
85
86
  const type = args[2];
86
87
  switch (type) {
87
88
  case "docs":
88
- await genApiDocs(args, projectName, project, alias, source);
89
+ if (args.includes("v1"))
90
+ await genApiDocsV1(args.filter(p => p != "v1"), projectName, project, alias, source);
91
+ else
92
+ await genApiDocs(args, projectName, project, alias, source);
89
93
  break;
90
94
  case "db-docs":
91
95
  genDatabaseDocs(args, projectName, project, alias, source);
package/merge.js ADDED
@@ -0,0 +1,84 @@
1
+ const fs = require("fs");
2
+ const path = require('path');
3
+ const JSON5 = require("json5");
4
+ const common = require("./common.js");
5
+ const help = require("./help.js");
6
+
7
+ const masterFile = require("./master-file/master-file");
8
+
9
+ async function merge(args) {
10
+ let projectName = args[1];
11
+ const output = args[2];
12
+
13
+ if (!projectName || !output) {
14
+ help(args);
15
+ return;
16
+ }
17
+
18
+ let alias = "default";
19
+ const atIndex = projectName.indexOf("@");
20
+ if (atIndex !== -1) {
21
+ alias = projectName.substring(atIndex + 1);
22
+ projectName = projectName.substring(0, atIndex);
23
+ if (!projectName) {
24
+ help(args);
25
+ return;
26
+ }
27
+
28
+ if (!alias)
29
+ alias = "default";
30
+ }
31
+
32
+ let mushroomConfig;
33
+ try {
34
+ const mushroomConfigContent = fs.readFileSync("./mushroom-config.json").toString();
35
+ mushroomConfig = JSON.parse(mushroomConfigContent);
36
+ }
37
+ catch (e) {
38
+ console.error("mushroom-config.json not found or unable to parse");
39
+ throw e;
40
+ }
41
+
42
+ if (!mushroomConfig || typeof mushroomConfig !== "object")
43
+ throw new Error(`Invalid configuration. Please re-check file mushroom-config.json`);
44
+
45
+ if (!mushroomConfig || !mushroomConfig.projects || !mushroomConfig.projects[projectName])
46
+ throw new Error(`Project not found (${projectName})`);
47
+
48
+ const project = mushroomConfig.projects[projectName];
49
+ if (!project.src)
50
+ throw new Error("Project source (src) not found");
51
+
52
+ var source;
53
+ try {
54
+ const sourceContent = fs.readFileSync(project.src).toString();
55
+ source = JSON5.parse(sourceContent);
56
+ }
57
+ catch(e) {
58
+ console.error(project.src + " not found or unable to parse");
59
+ throw e;
60
+ }
61
+
62
+ if (!source || typeof source !== "object")
63
+ throw new Error(`Invalid api project configuration. Please re-check file ${project.src}`);
64
+
65
+ if (Array.isArray(source.fragments)) {
66
+ console.log("Process master file: " + project.src);
67
+ source = masterFile.processMasterFile(path.dirname(project.src), source);
68
+ }
69
+
70
+ if (!source || typeof source !== "object") {
71
+ throw new Error(`${!source}\t${typeof source}\tInvalid api project configuration. Please re-check file ${project.src}`);
72
+ }
73
+
74
+ if (source.name !== projectName) {
75
+ throw new Error(`Project name is mismatched (${projectName} & ${source.name})`);
76
+ }
77
+
78
+ common.ensureFolder(output);
79
+
80
+ fs.writeFileSync(output, JSON.stringify(source));
81
+ console.log(`Merged ${projectName}@${alias} then saved to ${output}`);
82
+ }
83
+
84
+ module.exports = merge;
package/mr.js CHANGED
@@ -1,78 +1,82 @@
1
- #!/usr/bin/env node
2
-
3
- const init = require("./init.js");
4
- const config = require("./config.js");
5
- const gen = require("./gen.js");
6
- const update = require("./update.js");
7
- const help = require("./help.js");
8
-
9
- const pkg = require("./package.json");
10
- const checkUpdate = require("./check-update");
11
-
12
- const versionGroup = pkg.version.match(/([^-]+)(-(\w+).*)?/);
13
- const currentDistTag = versionGroup[3] ? versionGroup[3] : "latest";
14
- console.log("Mushroom CLI version " + pkg.version);
15
-
16
- async function run(args) {
17
- let debugMode = false;
18
- if (args.indexOf("--debug") !== -1) {
19
- debugMode = true;
20
- args = args.filter(arg => arg !== "--debug");
21
- }
22
-
23
- let checkUpdateTask;
24
- if (args.indexOf("--no-check-update") !== -1){
25
- args = args.filter(arg => arg !== "--no-check-update");
26
- checkUpdateTask = null;
27
- }
28
- else{
29
- checkUpdateTask = checkUpdate();
30
- checkUpdateTask.catch(() => {});
31
- }
32
-
33
- try {
34
- switch(args[0]) {
35
- case "init":
36
- await init(args);
37
- break;
38
- case "config":
39
- await config(args);
40
- break;
41
- case "gen":
42
- await gen(args);
43
- break;
44
- case "update":
45
- await update(args);
46
- break;
47
- case "":
48
- case undefined:
49
- console.log(`Mushroom CLI`);
50
- break;
51
- case "help":
52
- case "man":
53
- help(args.slice(1).join());
54
- break;
55
- default:
56
- help(args, true);
57
- break;
58
- }
59
- }
60
- catch (e) {
61
- console.error(debugMode ? e : e.message);
62
- }
63
-
64
-
65
- if (checkUpdateTask !== null)
66
- try {
67
- var data = await checkUpdateTask;
68
- if (data["dist-tags"] && data["dist-tags"][currentDistTag] && data["dist-tags"][currentDistTag] !== pkg.version) {
69
- console.warn(`YOUR mushroom-cli VERSION IS NOT LATEST (dist-tags: ${currentDistTag}). PLEASE RUN: npm i mushroom-cli@${data["dist-tags"][currentDistTag]} OR yarn add mushroom-cli@^${data["dist-tags"][currentDistTag]}`);
70
- }
71
- }
72
- catch(e) {
73
- if (debugMode) console.log(e);
74
- }
75
- }
76
-
77
- const [,, ...args] = process.argv;
1
+ #!/usr/bin/env node
2
+
3
+ const init = require("./init");
4
+ const config = require("./config");
5
+ const gen = require("./gen");
6
+ const merge = require("./merge");
7
+ const update = require("./update");
8
+ const help = require("./help");
9
+
10
+ const pkg = require("./package.json");
11
+ const checkUpdate = require("./check-update");
12
+
13
+ const versionGroup = pkg.version.match(/([^-]+)(-(\w+).*)?/);
14
+ const currentDistTag = versionGroup[3] ? versionGroup[3] : "latest";
15
+ console.log("Mushroom CLI version " + pkg.version);
16
+
17
+ async function run(args) {
18
+ let debugMode = false;
19
+ if (args.indexOf("--debug") !== -1) {
20
+ debugMode = true;
21
+ args = args.filter(arg => arg !== "--debug");
22
+ }
23
+
24
+ let checkUpdateTask;
25
+ if (args.indexOf("--no-check-update") !== -1){
26
+ args = args.filter(arg => arg !== "--no-check-update");
27
+ checkUpdateTask = null;
28
+ }
29
+ else{
30
+ checkUpdateTask = checkUpdate();
31
+ checkUpdateTask.catch(() => {});
32
+ }
33
+
34
+ const startTime = new Date().getTime();
35
+ try {
36
+ switch(args[0]) {
37
+ case "init":
38
+ await init(args);
39
+ break;
40
+ case "config":
41
+ await config(args);
42
+ break;
43
+ case "gen":
44
+ await gen(args);
45
+ break;
46
+ case "merge":
47
+ merge(args);
48
+ break;
49
+ case "update":
50
+ await update(args);
51
+ break;
52
+ case "help":
53
+ case "man":
54
+ help(args.slice(1).join());
55
+ break;
56
+ default:
57
+ help(args, true);
58
+ break;
59
+ }
60
+ }
61
+ catch (e) {
62
+ console.error(debugMode ? e : "ERROR: " + e.message);
63
+ }
64
+ finally {
65
+ console.log("Processed in", new Date().getTime() - startTime, "ms.");
66
+ }
67
+
68
+
69
+ if (checkUpdateTask !== null)
70
+ try {
71
+ var data = await checkUpdateTask;
72
+ if (data["dist-tags"] && data["dist-tags"][currentDistTag] && data["dist-tags"][currentDistTag] !== pkg.version) {
73
+ console.warn(`YOUR mushroom-cli VERSION IS NOT LATEST (dist-tags: ${currentDistTag}). PLEASE RUN: npm i mushroom-cli@${data["dist-tags"][currentDistTag]} OR yarn add mushroom-cli@^${data["dist-tags"][currentDistTag]}`);
74
+ }
75
+ }
76
+ catch(e) {
77
+ if (debugMode) console.log(e);
78
+ }
79
+ }
80
+
81
+ const [,, ...args] = process.argv;
78
82
  run(args);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mushroom-cli",
3
- "version": "1.2.0-alpha.1",
3
+ "version": "1.2.0-alpha.11",
4
4
  "description": "Mushroom CLI",
5
5
  "main": "mr.js",
6
6
  "scripts": {
@@ -16,11 +16,13 @@
16
16
  },
17
17
  "author": "suco2007",
18
18
  "license": "ISC",
19
+ "dependencies-comments": "node-fetch: 2.x; open: 8.x; minify: 7.x",
19
20
  "dependencies": {
20
- "jsdom": "^21.0.0",
21
+ "jsdom": "^24.1.1",
21
22
  "json5": "^2.2.3",
22
- "node-fetch": "^2.6.7",
23
- "open": "^8.4.0",
23
+ "minify": "7.2.2",
24
+ "node-fetch": "^2.7.0",
25
+ "open": "^8.4.2",
24
26
  "readline-sync": "^1.4.10",
25
27
  "targz": "^1.0.1"
26
28
  }