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.
@@ -1,32 +1,37 @@
1
- const fs = require("fs");
2
- const { spawn } = require("child_process");
3
- const common = require("../common.js");
4
-
5
-
6
- function fetchFromGit() {
7
- return new Promise((resolve, reject) => {
8
- const git = spawn("git", ["clone", "https://tfs.siten.vn/tfs/Siten/CodeBase/_git/Mushroom", "-b", "full_extensions", "--single-branch"]);
9
- git.stdout.on('data', (data) => {
10
- console.log(`GIT: ${data}`);
11
- });
12
-
13
- git.stderr.on('data', (data) => {
14
- console.error(`GIT: ${data}`);
15
- });
16
-
17
- git.on('close', (code) => {
18
- if (code) {
19
- reject(new Error(`GIT exited with code ${code}`));
20
- return;
21
- }
22
- resolve();
23
- });
24
- })
25
- }
26
-
27
- async function initNetCore(args) {
28
- await fetchFromGit();
29
- common.processTemplate("Mushroom", "mushroom-template.json");
30
- }
31
-
32
- module.exports = initNetCore;
1
+ const fs = require("fs");
2
+ const { spawn } = require("child_process");
3
+ const common = require("../common.js");
4
+
5
+ function fetchFromGit() {
6
+ return new Promise((resolve, reject) => {
7
+ const git = spawn("git", [
8
+ "clone",
9
+ "https://tfs.siten.vn/tfs/Siten/CodeBase/_git/Mushroom",
10
+ "-b",
11
+ "full_extensions",
12
+ "--single-branch",
13
+ ]);
14
+ git.stdout.on("data", (data) => {
15
+ console.log(`GIT: ${data}`);
16
+ });
17
+
18
+ git.stderr.on("data", (data) => {
19
+ console.error(`GIT: ${data}`);
20
+ });
21
+
22
+ git.on("close", (code) => {
23
+ if (code) {
24
+ reject(new Error(`GIT exited with code ${code}`));
25
+ return;
26
+ }
27
+ resolve();
28
+ });
29
+ });
30
+ }
31
+
32
+ async function initNetCore(args) {
33
+ await fetchFromGit();
34
+ common.processTemplate("Mushroom", "mushroom-template.json");
35
+ }
36
+
37
+ module.exports = initNetCore;
package/init.js CHANGED
@@ -1,28 +1,27 @@
1
- const help = require("./help.js");
2
-
3
- const initJson = require("./init/init-json");
4
- const initNetCore = require("./init/init-netcore");
5
-
6
- async function init(args) {
7
- const configCommand = args[1];
8
- switch(configCommand) {
9
- case "json":
10
- initJson(args);
11
- break;
12
- case "netcore":
13
- await initNetCore(args);
14
- break;
15
- case "":
16
- case undefined:
17
- case "help":
18
- case "man":
19
- help("init");
20
- break;
21
- default:
22
- help(args, true);
23
- break;
24
- }
25
-
26
- }
27
-
28
- module.exports = init;
1
+ const help = require("./help.js");
2
+
3
+ const initJson = require("./init/init-json");
4
+ const initNetCore = require("./init/init-netcore");
5
+
6
+ async function init(args) {
7
+ const configCommand = args[1];
8
+ switch (configCommand) {
9
+ case "json":
10
+ initJson(args);
11
+ break;
12
+ case "netcore":
13
+ await initNetCore(args);
14
+ break;
15
+ case "":
16
+ case undefined:
17
+ case "help":
18
+ case "man":
19
+ help("init");
20
+ break;
21
+ default:
22
+ help(args, true);
23
+ break;
24
+ }
25
+ }
26
+
27
+ module.exports = init;
@@ -1,234 +1,254 @@
1
- const fs = require("fs");
2
- const path = require('path');
3
- const JSON5 = require("json5");
4
-
5
- function transformValidationNode(validation) {
6
- if (!validation || typeof validation !== "object")
7
- return;
8
-
9
- if (!("min" in validation) && "gte" in validation) {
10
- validation.min = validation.gte;
11
- }
12
-
13
- if (!("max" in validation) && "lte" in validation) {
14
- validation.max = validation.lte;
15
- }
16
- }
17
-
18
-
19
- function transformEntityFieldNode(field) {
20
- if (!field || typeof field !== "object")
21
- return;
22
-
23
- transformValidationNode(field.validation);
24
- if (Array.isArray(field.items))
25
- transformValidationNode(field.items.validation);
26
-
27
- if (!field.fields || typeof field.fields !== "object")
28
- return;
29
-
30
- for (var fieldName in field.fields) {
31
- transformEntityFieldNode(field.fields[fieldName]);
32
- }
33
- }
34
-
35
- function transformViewNode(view) {
36
- if (!view || !Array.isArray(view.parameters))
37
- return;
38
-
39
- view.parameters.forEach(p => transformValidationNode(p.validation));
40
- }
41
-
42
- function mergeApiProject(totalProject, fragmentProject, options) {
43
- if (totalProject.name != null && fragmentProject.name != null && fragmentProject.name != totalProject.name)
44
- throw new Error(`Unable merge project '${fragmentProject.name}' into '${totalProject.name}'`);
45
- if (totalProject.version != null && fragmentProject.version != null && fragmentProject.version != totalProject.version)
46
- throw new Error(`Unable merge project '${fragmentProject.version}' into '${totalProject.version}'`);
47
-
48
- if (fragmentProject.environmentVariables != null) {
49
- totalProject.environmentVariables = totalProject.environmentVariables || {};
50
-
51
- for (var name in fragmentProject.environmentVariables) {
52
- if (name in totalProject.environmentVariables) {
53
- switch (options.environmentVariable.duplicateNameHandling) {
54
- case "override":
55
- totalProject.environmentVariables[name] = fragmentProject.environmentVariables[name];
56
- break;
57
- case "ignore":
58
- break;
59
- default:
60
- throw new Error(`Duplicate environment variables: ${name}. Can config dublicate name handling behavior at options.environmentVariable.duplicateNameHandling`);
61
- }
62
- }
63
- else
64
- totalProject.environmentVariables[name] = fragmentProject.environmentVariables[name];
65
- }
66
- }
67
-
68
- if (fragmentProject.entities != null) {
69
- totalProject.entities = totalProject.entities || [];
70
-
71
- fragmentProject.entities.forEach(entity => {
72
-
73
- var found = totalProject.entities.some((entityInTotal, i) => {
74
- if (entityInTotal.name !== entity.name){
75
- return false;
76
- }
77
- switch (options.entity.duplicateNameHandling) {
78
- case "override":
79
- totalProject.entities[i] = entity;
80
- return true;
81
- case "ignore":
82
- return true;
83
- default:
84
- throw new Error("Duplicate entity: " + entity.name + ". Can config dublicate name handling behavior at options.entity.duplicateNameHandling");
85
- }
86
- });
87
-
88
- if (found)
89
- return;
90
-
91
- totalProject.entities.push(entity);
92
- // console.log("add new entity: " + entity.name + "; totalProject.entities.length=" + totalProject.entities.length);
93
-
94
- if (!entity.fields || typeof entity.fields !== "object")
95
- return;
96
-
97
- for (var fieldName in entity.fields) {
98
- transformEntityFieldNode(entity.fields[fieldName]);
99
- }
100
- });
101
- }
102
-
103
- if (fragmentProject.views != null) {
104
- totalProject.views = totalProject.views || {};
105
-
106
- for (var viewName in fragmentProject.views)
107
- {
108
- if (viewName in totalProject.views)
109
- {
110
- switch (options.view.duplicateNameHandling)
111
- {
112
- case "override":
113
- totalProject.views[viewName] = fragmentProject.views[viewName];
114
- break;
115
- case "ignore":
116
- continue;
117
- default:
118
- throw new Error("Duplicate global view: " + viewName + ". Can config dublicate name handling behavior at options.views.duplicateNameHandling");
119
- }
120
- }
121
- else
122
- totalProject.views[viewName] = fragmentProject.views[viewName];
123
-
124
- if (!fragmentProject.views[viewName] || Array.isArray(fragmentProject.views[viewName])) {
125
- delete fragmentProject.views[viewName];
126
- continue;
127
- }
128
-
129
- fragmentProject.views[viewName].forEach(transformViewNode);
130
- }
131
- }
132
-
133
-
134
- for (var name in fragmentProject) {
135
- if (name === "environmentVariables" || name === "entities" || name === "views" || name === "$schema")
136
- continue;
137
-
138
- totalProject[name] = fragmentProject[name];
139
- }
140
-
141
- return totalProject;
142
- }
143
-
144
- function processMasterFile(basePath, master) {
145
- if (master.fragments == null || master.fragments.Length == 0)
146
- return null;
147
-
148
- const options = master.options || {
149
- formatIndent: false
150
- };
151
- options.environmentVariable = options.environmentVariable || { duplicateNameHandling: "raiseException" };
152
- options.entity = options.entity || { duplicateNameHandling: "raiseException" };
153
- options.view = options.view || { duplicateNameHandling: "raiseException" };
154
-
155
- let apiProject = {};
156
- master.fragments.forEach(fragment => {
157
- let apiProjectFragment;
158
- const fragmentPath = path.join(basePath, fragment.path)
159
- console.log("merging fragment file: " + fragmentPath);
160
- try {
161
- const content = fs.readFileSync(fragmentPath).toString();
162
- apiProjectFragment = JSON5.parse(content);
163
- }
164
- catch (e) {
165
- if (fragment.optional){
166
- console.warn(fragmentPath + " not found or unable to parse");
167
- return;
168
- }
169
- console.error(fragmentPath + " not found or unable to parse");
170
- throw e;
171
- }
172
-
173
- apiProject = mergeApiProject(apiProject, apiProjectFragment, master.options);
174
- })
175
-
176
- if (apiProject == null)
177
- return null;
178
-
179
- apiProject["$schema"] = "http://schema.siten.vn/mushroom/project.json";
180
-
181
- if (apiProject.entities == null)
182
- apiProject.entities = [];
183
-
184
- apiProject.entities.forEach(entity => {
185
- if (entity.actions == null)
186
- entity.actions = [];
187
-
188
- for (let actionName in entity.actions) {
189
- if (actionName != "findMany" && actionName != "findById" || entity.actions[actionName] == null)
190
- continue;
191
-
192
- entity.actions[actionName].forEach(action => {
193
- if (action.fields) {
194
- action.defaultFields = action.fields.split(',');
195
- delete action.fields;
196
- }
197
- if (action.allowFields) {
198
- action.returnableFields = action.allowFields.split(',');
199
- delete action.allowFields;
200
- }
201
-
202
- if (actionName != "findMany")
203
- return;
204
- if (action.sorting) {
205
- action.defaultSortings = action.sorting.split(',').map(f => {
206
- const c = f.charAt(0);
207
- return {
208
- field: c === "+" || c === "-" ? f.substring(1) : f,
209
- type: c === "-" ? "Desc" : "Asc"
210
- }
211
- });
212
- delete action.sorting;
213
- }
214
- if (action.allowSorting)
215
- {
216
- action.allowSortings = action.allowSorting.split(',').map(f => {
217
- const c = f.charAt(0);
218
- return {
219
- field: c === "+" || c === "-" ? f.substring(1) : f,
220
- type: c === "-" ? "Desc" : c === "+" ? "Asc" : "Both"
221
- }
222
- });
223
- delete action.allowSorting;
224
- }
225
- });
226
- }
227
- });
228
-
229
- return apiProject;
230
- }
231
-
232
- module.exports = {
233
- processMasterFile: processMasterFile
234
- };
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const JSON5 = require("json5");
4
+
5
+ function transformValidationNode(validation) {
6
+ if (!validation || typeof validation !== "object") return;
7
+
8
+ if (!("min" in validation) && "gte" in validation) {
9
+ validation.min = validation.gte;
10
+ }
11
+
12
+ if (!("max" in validation) && "lte" in validation) {
13
+ validation.max = validation.lte;
14
+ }
15
+ }
16
+
17
+ function transformEntityFieldNode(field) {
18
+ if (!field || typeof field !== "object") return;
19
+
20
+ transformValidationNode(field.validation);
21
+ if (Array.isArray(field.items))
22
+ transformValidationNode(field.items.validation);
23
+
24
+ if (!field.fields || typeof field.fields !== "object") return;
25
+
26
+ for (const fieldName in field.fields) {
27
+ transformEntityFieldNode(field.fields[fieldName]);
28
+ }
29
+ }
30
+
31
+ function transformViewNode(view) {
32
+ if (!view || !Array.isArray(view.parameters)) return;
33
+
34
+ view.parameters.forEach((p) => transformValidationNode(p.validation));
35
+ }
36
+
37
+ function mergeApiProject(totalProject, fragmentProject, options) {
38
+ if (
39
+ totalProject.name != null &&
40
+ fragmentProject.name != null &&
41
+ fragmentProject.name != totalProject.name
42
+ )
43
+ throw new Error(
44
+ `Unable merge project '${fragmentProject.name}' into '${totalProject.name}'`,
45
+ );
46
+ if (
47
+ totalProject.version != null &&
48
+ fragmentProject.version != null &&
49
+ fragmentProject.version != totalProject.version
50
+ )
51
+ throw new Error(
52
+ `Unable merge project '${fragmentProject.version}' into '${totalProject.version}'`,
53
+ );
54
+
55
+ if (fragmentProject.environmentVariables != null) {
56
+ totalProject.environmentVariables = totalProject.environmentVariables || {};
57
+
58
+ for (const name in fragmentProject.environmentVariables) {
59
+ if (name in totalProject.environmentVariables) {
60
+ switch (options.environmentVariable.duplicateNameHandling) {
61
+ case "override":
62
+ totalProject.environmentVariables[name] =
63
+ fragmentProject.environmentVariables[name];
64
+ break;
65
+ case "ignore":
66
+ break;
67
+ default:
68
+ throw new Error(
69
+ `Duplicate environment variables: ${name}. Can config dublicate name handling behavior at options.environmentVariable.duplicateNameHandling`,
70
+ );
71
+ }
72
+ } else
73
+ totalProject.environmentVariables[name] =
74
+ fragmentProject.environmentVariables[name];
75
+ }
76
+ }
77
+
78
+ if (fragmentProject.entities != null) {
79
+ totalProject.entities = totalProject.entities || [];
80
+
81
+ fragmentProject.entities.forEach((entity) => {
82
+ const found = totalProject.entities.some((entityInTotal, i) => {
83
+ if (entityInTotal.name !== entity.name) {
84
+ return false;
85
+ }
86
+ switch (options.entity.duplicateNameHandling) {
87
+ case "override":
88
+ totalProject.entities[i] = entity;
89
+ return true;
90
+ case "ignore":
91
+ return true;
92
+ default:
93
+ throw new Error(
94
+ "Duplicate entity: " +
95
+ entity.name +
96
+ ". Can config dublicate name handling behavior at options.entity.duplicateNameHandling",
97
+ );
98
+ }
99
+ });
100
+
101
+ if (found) return;
102
+
103
+ totalProject.entities.push(entity);
104
+
105
+ if (!entity.fields || typeof entity.fields !== "object") return;
106
+
107
+ for (const fieldName in entity.fields) {
108
+ transformEntityFieldNode(entity.fields[fieldName]);
109
+ }
110
+ });
111
+ }
112
+
113
+ if (fragmentProject.views != null) {
114
+ totalProject.views = totalProject.views || {};
115
+
116
+ for (const viewName in fragmentProject.views) {
117
+ if (viewName in totalProject.views) {
118
+ switch (options.view.duplicateNameHandling) {
119
+ case "override":
120
+ totalProject.views[viewName] = fragmentProject.views[viewName];
121
+ break;
122
+ case "ignore":
123
+ continue;
124
+ default:
125
+ throw new Error(
126
+ "Duplicate global view: " +
127
+ viewName +
128
+ ". Can config dublicate name handling behavior at options.views.duplicateNameHandling",
129
+ );
130
+ }
131
+ } else totalProject.views[viewName] = fragmentProject.views[viewName];
132
+
133
+ if (
134
+ !fragmentProject.views[viewName] ||
135
+ Array.isArray(fragmentProject.views[viewName])
136
+ ) {
137
+ delete fragmentProject.views[viewName];
138
+ continue;
139
+ }
140
+
141
+ fragmentProject.views[viewName].forEach(transformViewNode);
142
+ }
143
+ }
144
+
145
+ for (const name in fragmentProject) {
146
+ if (
147
+ name === "environmentVariables" ||
148
+ name === "entities" ||
149
+ name === "views" ||
150
+ name === "$schema"
151
+ )
152
+ continue;
153
+
154
+ totalProject[name] = fragmentProject[name];
155
+ }
156
+
157
+ return totalProject;
158
+ }
159
+
160
+ function processMasterFile(basePath, master) {
161
+ if (master.fragments == null || master.fragments.Length == 0) return null;
162
+
163
+ const options = master.options || {
164
+ formatIndent: false,
165
+ };
166
+ options.environmentVariable = options.environmentVariable || {
167
+ duplicateNameHandling: "raiseException",
168
+ };
169
+ options.entity = options.entity || {
170
+ duplicateNameHandling: "raiseException",
171
+ };
172
+ options.view = options.view || { duplicateNameHandling: "raiseException" };
173
+
174
+ let apiProject = {};
175
+ master.fragments.forEach((fragment) => {
176
+ let apiProjectFragment;
177
+ const fragmentPath = path.join(basePath, fragment.path);
178
+ console.log("merging fragment file: " + fragmentPath);
179
+ try {
180
+ const content = fs.readFileSync(fragmentPath).toString();
181
+ apiProjectFragment = JSON5.parse(content);
182
+ } catch (e) {
183
+ if (fragment.optional) {
184
+ console.warn(fragmentPath + " not found or unable to parse");
185
+ return;
186
+ }
187
+ console.error(fragmentPath + " not found or unable to parse");
188
+ throw e;
189
+ }
190
+
191
+ apiProject = mergeApiProject(
192
+ apiProject,
193
+ apiProjectFragment,
194
+ master.options,
195
+ );
196
+ });
197
+
198
+ if (apiProject == null) return null;
199
+
200
+ apiProject["$schema"] = "https://schema.siten.vn/mushroom/project.json";
201
+
202
+ if (apiProject.entities == null) apiProject.entities = [];
203
+
204
+ apiProject.entities.forEach((entity) => {
205
+ if (entity.actions == null) entity.actions = [];
206
+
207
+ for (let actionName in entity.actions) {
208
+ if (
209
+ (actionName != "findMany" && actionName != "findById") ||
210
+ entity.actions[actionName] == null
211
+ )
212
+ continue;
213
+
214
+ entity.actions[actionName].forEach((action) => {
215
+ if (action.fields) {
216
+ action.defaultFields = action.fields.split(",");
217
+ delete action.fields;
218
+ }
219
+ if (action.allowFields) {
220
+ action.returnableFields = action.allowFields.split(",");
221
+ delete action.allowFields;
222
+ }
223
+
224
+ if (actionName != "findMany") return;
225
+ if (action.sorting) {
226
+ action.defaultSortings = action.sorting.split(",").map((f) => {
227
+ const c = f.charAt(0);
228
+ return {
229
+ field: c === "+" || c === "-" ? f.substring(1) : f,
230
+ type: c === "-" ? "Desc" : "Asc",
231
+ };
232
+ });
233
+ delete action.sorting;
234
+ }
235
+ if (action.allowSorting) {
236
+ action.allowSortings = action.allowSorting.split(",").map((f) => {
237
+ const c = f.charAt(0);
238
+ return {
239
+ field: c === "+" || c === "-" ? f.substring(1) : f,
240
+ type: c === "-" ? "Desc" : c === "+" ? "Asc" : "Both",
241
+ };
242
+ });
243
+ delete action.allowSorting;
244
+ }
245
+ });
246
+ }
247
+ });
248
+
249
+ return apiProject;
250
+ }
251
+
252
+ module.exports = {
253
+ processMasterFile: processMasterFile,
254
+ };