mushroom-cli 1.1.0 → 1.1.2

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.
package/gen.js CHANGED
@@ -1,120 +1,146 @@
1
- const fs = require("fs");
2
- const path = require('path');
3
- const JSON5 = require("json5");
4
- const help = require("./help.js");
5
-
6
- const masterFile = require("./master-file/master-file");
7
-
8
- const genApiDocs = require("./gen/gen-api-docs");
9
- const genDatabaseDocs = require("./gen/gen-db-docs");
10
- const javascriptDriverGenerator = require("./gen/gen-js-driver");
11
- const genJavascriptDriverTarball = require("./gen/gen-js-driver-tarball");
12
- const typescriptDriverGenerator = require("./gen/gen-ts-driver");
13
- const csharp = require("./gen/gen-csharp");
14
-
15
-
16
- async function gen(args) {
17
- let projectName = args[1];
18
- let alias = "default";
19
-
20
- if (!projectName) {
21
- help(args);
22
- return;
23
- }
24
-
25
- const atIndex = projectName.indexOf("@");
26
- if (atIndex !== -1) {
27
- alias = projectName.substring(atIndex + 1);
28
- projectName = projectName.substring(0, atIndex);
29
- if (!projectName) {
30
- help(args);
31
- return;
32
- }
33
-
34
- if (!alias)
35
- alias = "default";
36
- }
37
-
38
- let mushroomConfig;
39
- try {
40
- const mushroomConfigContent = fs.readFileSync("./mushroom-config.json").toString();
41
- mushroomConfig = JSON.parse(mushroomConfigContent);
42
- }
43
- catch (e) {
44
- console.error("mushroom-config.json not found or unable to parse");
45
- throw e;
46
- }
47
-
48
- if (!mushroomConfig || typeof mushroomConfig !== "object")
49
- throw new Error(`Invalid configuration. Please re-check file mushroom-config.json`);
50
-
51
- if (!mushroomConfig || !mushroomConfig.projects || !mushroomConfig.projects[projectName])
52
- throw new Error(`Project not found (${projectName})`);
53
-
54
- const project = mushroomConfig.projects[projectName];
55
- if (!project.src)
56
- throw new Error("Project source (src) not found");
57
-
58
- var source;
59
- try {
60
- const sourceContent = fs.readFileSync(project.src).toString();
61
- source = JSON5.parse(sourceContent);
62
- }
63
- catch(e) {
64
- console.error(project.src + " not found or unable to parse");
65
- throw e;
66
- }
67
-
68
- if (!source || typeof source !== "object")
69
- throw new Error(`Invalid api project configuration. Please re-check file ${project.src}`);
70
-
71
- if (Array.isArray(source.fragments)) {
72
- console.log("Process master file: " + project.src);
73
- source = masterFile.processMasterFile(path.dirname(project.src), source);
74
- //console.log(source);
75
- }
76
-
77
- if (!source || typeof source !== "object") {
78
- throw new Error(`${!source}\t${typeof source}\tInvalid api project configuration. Please re-check file ${project.src}`);
79
- }
80
-
81
- if (source.name !== projectName) {
82
- throw new Error(`Project name is mismatched (${projectName} & ${source.name})`);
83
- }
84
-
85
- const type = args[2];
86
- switch (type) {
87
- case "docs":
88
- await genApiDocs(args, projectName, project, alias, source);
89
- break;
90
- case "db-docs":
91
- genDatabaseDocs(args, projectName, project, alias, source);
92
- break;
93
- case "js-driver":
94
- javascriptDriverGenerator.genJavascriptDriver(args, projectName, project, alias, source);
95
- break;
96
- case "js-driver-tarball":
97
- await genJavascriptDriverTarball(args, projectName, project, alias, source);
98
- break;
99
- case "ts-driver":
100
- typescriptDriverGenerator.genTypescriptDriver(args, projectName, project, alias, source);
101
- break;
102
- case "c#-mongo":
103
- csharp.genCSharpMongoDbClasses(args, projectName, project, alias, source);
104
- break;
105
- case "c#-driver":
106
- csharp.genCSharpDriver(args, projectName, project, alias, source);
107
- break;
108
- case "":
109
- case undefined:
110
- case "help":
111
- case "man":
112
- help(args);
113
- break;
114
- default:
115
- help(args, true);
116
- break;
117
- }
118
- }
119
-
120
- module.exports = gen;
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const JSON5 = require("json5");
4
+ const help = require("./help.js");
5
+
6
+ const masterFile = require("./master-file/master-file");
7
+
8
+ const genApiDocs = require("./gen/gen-api-docs");
9
+ const genDatabaseDocs = require("./gen/gen-db-docs");
10
+ const javascriptDriverGenerator = require("./gen/gen-js-driver");
11
+ const genJavascriptDriverTarball = require("./gen/gen-js-driver-tarball");
12
+ const typescriptDriverGenerator = require("./gen/gen-ts-driver");
13
+ const csharp = require("./gen/gen-csharp");
14
+
15
+ async function gen(args) {
16
+ let projectName = args[1];
17
+ let alias = "default";
18
+
19
+ if (!projectName) {
20
+ help(args);
21
+ return;
22
+ }
23
+
24
+ const atIndex = projectName.indexOf("@");
25
+ if (atIndex !== -1) {
26
+ alias = projectName.substring(atIndex + 1);
27
+ projectName = projectName.substring(0, atIndex);
28
+ if (!projectName) {
29
+ help(args);
30
+ return;
31
+ }
32
+
33
+ if (!alias) alias = "default";
34
+ }
35
+
36
+ let mushroomConfig;
37
+ try {
38
+ const mushroomConfigContent = fs
39
+ .readFileSync("./mushroom-config.json")
40
+ .toString();
41
+ mushroomConfig = JSON.parse(mushroomConfigContent);
42
+ } catch (e) {
43
+ console.error("mushroom-config.json not found or unable to parse");
44
+ throw e;
45
+ }
46
+
47
+ if (!mushroomConfig || typeof mushroomConfig !== "object")
48
+ throw new Error(
49
+ `Invalid configuration. Please re-check file mushroom-config.json`,
50
+ );
51
+
52
+ if (
53
+ !mushroomConfig ||
54
+ !mushroomConfig.projects ||
55
+ !mushroomConfig.projects[projectName]
56
+ )
57
+ throw new Error(`Project not found (${projectName})`);
58
+
59
+ const project = mushroomConfig.projects[projectName];
60
+ if (!project.src) throw new Error("Project source (src) not found");
61
+
62
+ let source;
63
+ try {
64
+ const sourceContent = fs.readFileSync(project.src).toString();
65
+ source = JSON5.parse(sourceContent);
66
+ } catch (e) {
67
+ console.error(project.src + " not found or unable to parse");
68
+ throw e;
69
+ }
70
+
71
+ if (!source || typeof source !== "object")
72
+ throw new Error(
73
+ `Invalid api project configuration. Please re-check file ${project.src}`,
74
+ );
75
+
76
+ if (Array.isArray(source.fragments)) {
77
+ console.log("Process master file: " + project.src);
78
+ source = masterFile.processMasterFile(path.dirname(project.src), source);
79
+ }
80
+
81
+ if (!source || typeof source !== "object") {
82
+ throw new Error(
83
+ `${!source}\t${typeof source}\tInvalid api project configuration. Please re-check file ${project.src}`,
84
+ );
85
+ }
86
+
87
+ if (source.name !== projectName) {
88
+ throw new Error(
89
+ `Project name is mismatched (${projectName} & ${source.name})`,
90
+ );
91
+ }
92
+
93
+ const type = args[2];
94
+ switch (type) {
95
+ case "docs":
96
+ await genApiDocs(args, projectName, project, alias, source);
97
+ break;
98
+ case "db-docs":
99
+ genDatabaseDocs(args, projectName, project, alias, source);
100
+ break;
101
+ case "js-driver":
102
+ javascriptDriverGenerator.genJavascriptDriver(
103
+ args,
104
+ projectName,
105
+ project,
106
+ alias,
107
+ source,
108
+ );
109
+ break;
110
+ case "js-driver-tarball":
111
+ await genJavascriptDriverTarball(
112
+ args,
113
+ projectName,
114
+ project,
115
+ alias,
116
+ source,
117
+ );
118
+ break;
119
+ case "ts-driver":
120
+ typescriptDriverGenerator.genTypescriptDriver(
121
+ args,
122
+ projectName,
123
+ project,
124
+ alias,
125
+ source,
126
+ );
127
+ break;
128
+ case "c#-mongo":
129
+ csharp.genCSharpMongoDbClasses(args, projectName, project, alias, source);
130
+ break;
131
+ case "c#-driver":
132
+ csharp.genCSharpDriver(args, projectName, project, alias, source);
133
+ break;
134
+ case "":
135
+ case undefined:
136
+ case "help":
137
+ case "man":
138
+ help(args);
139
+ break;
140
+ default:
141
+ help(args, true);
142
+ break;
143
+ }
144
+ }
145
+
146
+ module.exports = gen;
package/help.js CHANGED
@@ -1,55 +1,51 @@
1
- function help(key, wrong) {
2
- if (Array.isArray(key))
3
- key = key.join(" ");
4
-
5
- if (key){
6
- if (wrong)
7
- console.log(`Invalid command '${key}'`);
8
- else
9
- console.log(`Document for '${key}' is comming soon`);
10
- }
11
- else
12
- console.log(`Init a mushroom json file
13
- npx mushroom-cli init json ./api/SampleProject.json [silent]
14
-
15
- Init a dotnet core project which pre-install Mushroom Server
16
- npx mushroom-cli init netcore
17
-
18
- Add the mushroom json file into mushroom-config.json
19
- npx mushroom-cli config add ./api/yourProjectName.json
20
-
21
- Config gen docs api
22
- npx mushroom-cli config gen projectName docs ./wwwroot/docs.html [alias aliasName] [features file,auth,mail,driver] [roles Admin,User-Contributor]
23
-
24
- Generate docs api (html format)
25
- npx mushroom-cli gen projectName[@aliasName] docs [tmp]
26
-
27
- Config gen database docs
28
- npx mushroom-cli config gen projectName db-docs ./wwwroot/db-docs.html [alias aliasName] [features file,auth,mail]
29
-
30
- Generate database docs (html format)
31
- npx mushroom-cli gen projectName[@aliasName] db-docs [tmp]
32
-
33
- Config gen javascript driver
34
- npx mushroom-cli config gen projectName js-driver ./src/api/mushroom.api.js [alias aliasName] [roles Admin,User-Contributor] [tarball tarballName]
35
-
36
- Generate javascript driver
37
- npx mushroom-cli gen projectName[@aliasName] js-driver
38
-
39
- Config gen entities for Mushroom project in CSharp and using MongoDb
40
- npx mushroom-cli config gen projectName c#-mongo ./API/Generated.cs [alias aliasName]
41
-
42
- Generate entities for Mushroom project in CSharp and using MongoDb
43
- npx mushroom-cli gen projectName[@aliasName] c#-mongo
44
-
45
- Config gen entities for CSharp project which calls Mushroom API
46
- npx mushroom-cli config gen projectName c#-driver ./API/Generated.cs [roles Admin,User-Contributor] [alias aliasName]
47
-
48
- Generate entities for CSharp project which calls Mushroom API
49
- npx mushroom-cli gen projectName[@aliasName] c#-driver
50
-
51
- Disable checking for update:
52
- npx mushroom-cli parameters --no-check-update`);
53
- }
54
-
55
- module.exports = help;
1
+ function help(key, wrong) {
2
+ if (Array.isArray(key)) key = key.join(" ");
3
+
4
+ if (key) {
5
+ if (wrong) console.log(`Invalid command '${key}'`);
6
+ else console.log(`Document for '${key}' is comming soon`);
7
+ } else
8
+ console.log(`Init a mushroom json file
9
+ npx mushroom-cli init json ./api/SampleProject.json [silent]
10
+
11
+ Init a dotnet core project which pre-install Mushroom Server
12
+ npx mushroom-cli init netcore
13
+
14
+ Add the mushroom json file into mushroom-config.json
15
+ npx mushroom-cli config add ./api/yourProjectName.json
16
+
17
+ Config gen docs api
18
+ npx mushroom-cli config gen projectName docs ./wwwroot/docs.html [alias aliasName] [features file,auth,mail,driver] [roles Admin,User-Contributor]
19
+
20
+ Generate docs api (html format)
21
+ npx mushroom-cli gen projectName[@aliasName] docs [tmp]
22
+
23
+ Config gen database docs
24
+ npx mushroom-cli config gen projectName db-docs ./wwwroot/db-docs.html [alias aliasName] [features file,auth,mail]
25
+
26
+ Generate database docs (html format)
27
+ npx mushroom-cli gen projectName[@aliasName] db-docs [tmp]
28
+
29
+ Config gen javascript driver
30
+ npx mushroom-cli config gen projectName js-driver ./src/api/mushroom.api.js [alias aliasName] [roles Admin,User-Contributor] [tarball tarballName]
31
+
32
+ Generate javascript driver
33
+ npx mushroom-cli gen projectName[@aliasName] js-driver
34
+
35
+ Config gen entities for Mushroom project in CSharp and using MongoDb
36
+ npx mushroom-cli config gen projectName c#-mongo ./API/Generated.cs [alias aliasName]
37
+
38
+ Generate entities for Mushroom project in CSharp and using MongoDb
39
+ npx mushroom-cli gen projectName[@aliasName] c#-mongo
40
+
41
+ Config gen entities for CSharp project which calls Mushroom API
42
+ npx mushroom-cli config gen projectName c#-driver ./API/Generated.cs [roles Admin,User-Contributor] [alias aliasName]
43
+
44
+ Generate entities for CSharp project which calls Mushroom API
45
+ npx mushroom-cli gen projectName[@aliasName] c#-driver
46
+
47
+ Disable checking for update:
48
+ npx mushroom-cli parameters --no-check-update`);
49
+ }
50
+
51
+ module.exports = help;
package/init/init-json.js CHANGED
@@ -1,106 +1,110 @@
1
- const fs = require("fs");
2
- const fetch = require('node-fetch');
3
- const readlineSync = require("readline-sync");
4
- const help = require("../help.js");
5
- const common = require("../common.js");
6
-
7
- function initJson(args) {
8
- const path = args[2];
9
- if (!path) {
10
- help(args);
11
- return;
12
- }
13
-
14
- if (fs.existsSync(path)){
15
- console.error(`File ${path} is already existed`);
16
- return;
17
- }
18
-
19
- if (args[3] === "silent") {
20
- console.log("Fetch http://api.siten.vn/gen/template/default.json");
21
- fetch("http://api.siten.vn/gen/template/default.json").then(res => res.text()).then(text => {
22
- common.ensureFolder(path);
23
-
24
- fs.writeFile(path, text, err => {
25
- if (err)
26
- throw err;
27
- console.log(`Initialized json at ${path}`);
28
- });
29
- });
30
- return;
31
- }
32
-
33
- console.log("Fetch http://api.siten.vn/gen/template/template-def.json");
34
- fetch("http://api.siten.vn/gen/template/template-def.json").then(res => res.json()).then(templateConfig => {
35
- const info = {};
36
- templateConfig.fields.forEach(field => {
37
- while (true)
38
- {
39
- let question = `${field.required ? "[Required] " : ""}${field.label || field.name}`;
40
- if (Array.isArray(field.list)) {
41
- question += "\n";
42
- let i = 1;
43
- field.list.forEach(item => { question += "\t" + i++ + ". " + item + "\n"; });
44
- question += "Please choose";
45
- }
46
- if (field.defaultValue !== undefined) {
47
- question += ` (default: '${field.defaultValue}')`;
48
- }
49
- question += ": ";
50
- let choice = readlineSync.question(question);
51
- if (choice === "") {
52
- if (field.defaultValue != undefined)
53
- choice = field.defaultValue;
54
- }
55
- else if (Array.isArray(field.list)) {
56
- if (choice !== null && choice !== "" && !isNaN(choice))
57
- choice = field.list[choice - 1];
58
- else
59
- choice = undefined;
60
- if (choice === undefined) {
61
- console.warn("Invalid input");
62
- continue;
63
- }
64
- }
65
-
66
- if (choice === "" || choice === undefined || choice == null) {
67
- choice = "";
68
-
69
- if (field.required) {
70
- console.warn("Required input value");
71
- continue;
72
- }
73
- }
74
-
75
- if (choice !== "" && field.pattern) {
76
- const re = new RegExp(field.pattern, field.patternFlags);
77
- if (!re.test(choice)) {
78
- console.warn("Input doesn't match with pattern " + field.pattern);
79
- continue;
80
- }
81
- }
82
-
83
- info[field.name] = choice;
84
- break;
85
- }
86
- });
87
- //console.log(info);
88
-
89
- console.log(`Fetch ${templateConfig.templateUrl}`);
90
- fetch(templateConfig.templateUrl).then(res => res.text()).then(json => {
91
- json = json.replace(/\"\$\{([\w]+)\}\"/g, (s0, s1) => JSON.stringify(info[s1]));
92
- json = json.replace(/\$\{([\w]+)\}/g, (s0, s1) => JSON.stringify(info[s1]).replace(/^\"|\"$/g, ""));
93
-
94
- common.ensureFolder(path);
95
- //console.log(json);
96
-
97
- fs.writeFile(path, json, err => {
98
- if (err)
99
- throw err;
100
- console.log(`Initialized json at ${path}`);
101
- });
102
- });
103
- })
104
- }
105
-
106
- module.exports = initJson;
1
+ const fs = require("fs");
2
+ const fetch = require("node-fetch");
3
+ const readlineSync = require("readline-sync");
4
+ const help = require("../help.js");
5
+ const common = require("../common.js");
6
+
7
+ function initJson(args) {
8
+ const path = args[2];
9
+ if (!path) {
10
+ help(args);
11
+ return;
12
+ }
13
+
14
+ if (fs.existsSync(path)) {
15
+ console.error(`File ${path} is already existed`);
16
+ return;
17
+ }
18
+
19
+ if (args[3] === "silent") {
20
+ console.log("Fetch https://api.siten.vn/gen/template/default.json");
21
+ fetch("https://api.siten.vn/gen/template/default.json")
22
+ .then((res) => res.text())
23
+ .then((text) => {
24
+ common.ensureFolder(path);
25
+
26
+ fs.writeFile(path, text, (err) => {
27
+ if (err) throw err;
28
+ console.log(`Initialized json at ${path}`);
29
+ });
30
+ });
31
+ return;
32
+ }
33
+
34
+ console.log("Fetch https://api.siten.vn/gen/template/template-def.json");
35
+ fetch("https://api.siten.vn/gen/template/template-def.json")
36
+ .then((res) => res.json())
37
+ .then((templateConfig) => {
38
+ const info = {};
39
+ templateConfig.fields.forEach((field) => {
40
+ while (true) {
41
+ let question = `${field.required ? "[Required] " : ""}${field.label || field.name}`;
42
+ if (Array.isArray(field.list)) {
43
+ question += "\n";
44
+ let i = 1;
45
+ field.list.forEach((item) => {
46
+ question += "\t" + i++ + ". " + item + "\n";
47
+ });
48
+ question += "Please choose";
49
+ }
50
+ if (field.defaultValue !== undefined) {
51
+ question += ` (default: '${field.defaultValue}')`;
52
+ }
53
+ question += ": ";
54
+ let choice = readlineSync.question(question);
55
+ if (choice === "") {
56
+ if (field.defaultValue != undefined) choice = field.defaultValue;
57
+ } else if (Array.isArray(field.list)) {
58
+ if (choice !== null && choice !== "" && !isNaN(choice))
59
+ choice = field.list[choice - 1];
60
+ else choice = undefined;
61
+ if (choice === undefined) {
62
+ console.warn("Invalid input");
63
+ continue;
64
+ }
65
+ }
66
+
67
+ if (choice === "" || choice === undefined || choice == null) {
68
+ choice = "";
69
+
70
+ if (field.required) {
71
+ console.warn("Required input value");
72
+ continue;
73
+ }
74
+ }
75
+
76
+ if (choice !== "" && field.pattern) {
77
+ const re = new RegExp(field.pattern, field.patternFlags);
78
+ if (!re.test(choice)) {
79
+ console.warn("Input doesn't match with pattern " + field.pattern);
80
+ continue;
81
+ }
82
+ }
83
+
84
+ info[field.name] = choice;
85
+ break;
86
+ }
87
+ });
88
+
89
+ console.log(`Fetch ${templateConfig.templateUrl}`);
90
+ fetch(templateConfig.templateUrl)
91
+ .then((res) => res.text())
92
+ .then((json) => {
93
+ json = json.replace(/\"\$\{(\w+)\}\"/g, (s0, s1) =>
94
+ JSON.stringify(info[s1]),
95
+ );
96
+ json = json.replace(/\$\{(\w+)\}/g, (s0, s1) =>
97
+ JSON.stringify(info[s1]).replace(/(^")|("$)/g, ""),
98
+ );
99
+
100
+ common.ensureFolder(path);
101
+
102
+ fs.writeFile(path, json, (err) => {
103
+ if (err) throw err;
104
+ console.log(`Initialized json at ${path}`);
105
+ });
106
+ });
107
+ });
108
+ }
109
+
110
+ module.exports = initJson;