mushroom-cli 1.1.0-alpha.2 → 1.1.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.
package/README.md CHANGED
@@ -1,75 +1,83 @@
1
- # Init
1
+ # Mushroom CLI
2
+
3
+ ## Init
2
4
 
3
5
  Init a mushroom json file
4
6
 
5
- npx mr init json ./api/SampleProject.json [silent]
7
+ npx mushroom-cli init json ./api/SampleProject.json [silent]
6
8
 
7
- Init a dotnet core project which pre-install Mushroom Server
9
+ Init a DotNet Core project which pre-install Mushroom Server
8
10
 
9
- npx mr init netcore projectName [features file,auth,mail]
11
+ npx mushroom-cli init netcore [projectName]
10
12
 
11
- # Configure
13
+ ## Configure
12
14
 
13
15
  Add the mushroom json file into mushroom-config.json
14
16
 
15
- npx mr config add ./api/yourProjectName.json
17
+ npx mushroom-cli config add ./api/yourProjectName.json
16
18
 
17
19
  Config gen docs api
18
20
 
19
- npx mr config gen projectName docs ./wwwroot/docs.html [alias aliasName] [features file,auth,mail,driver] [roles Admin,User-Contributor]
21
+ npx mushroom-cli config gen projectName docs ./wwwroot/docs.html [alias aliasName] [features file,auth,mail,driver] [roles Admin,User-Contributor]
20
22
 
21
23
  Config gen database docs
22
24
 
23
- npx mr config gen projectName db-docs ./wwwroot/db-docs.html [alias aliasName] [features file,auth,mail]
25
+ npx mushroom-cli config gen projectName db-docs ./wwwroot/db-docs.html [alias aliasName] [features file,auth,mail]
24
26
 
25
27
  Config gen javascript driver
26
28
 
27
- npx mr config gen projectName js-driver ./src/api/mushroom.api.js [alias aliasName] [roles Admin,User-Contributor] [tarball tarballName]
29
+ npx mushroom-cli config gen projectName js-driver ./src/api/mushroom.api.js [alias aliasName] [roles Admin,User-Contributor] [tarball tarballName]
28
30
 
29
31
  Config gen typescript driver
30
32
 
31
- npx mr config gen projectName ts-driver ./src/api/mushroom.api.ts [alias aliasName] [roles Admin,User-Contributor]
33
+ npx mushroom-cli config gen projectName ts-driver ./src/api/mushroom.api.ts [alias aliasName] [roles Admin,User-Contributor]
32
34
 
33
35
  Config gen entities for Mushroom project in CSharp and using MongoDb
34
36
 
35
- npx mr config gen projectName c#-mongo ./API/Generated.cs [alias aliasName]
37
+ npx mushroom-cli config gen projectName c#-mongo ./API/Generated.cs [alias aliasName]
36
38
 
37
39
  Config gen entities for CSharp project which calls Mushroom API
38
40
 
39
- npx mr config gen projectName c#-driver ./API/Generated.cs [roles Admin,User-Contributor] [alias aliasName] [access-modifier public] [features file,auth,mail]
41
+ npx mushroom-cli config gen projectName c#-driver ./API/Generated.cs [roles Admin,User-Contributor] [alias aliasName] [access-modifier public] [features file,auth,mail]
40
42
 
41
- # Genarate
43
+ ## Genarate
42
44
 
43
45
  Generate docs api (html format)
44
46
 
45
- npx mr gen projectName[@aliasName] docs [tmp]
47
+ npx mushroom-cli gen projectName[@aliasName] docs [tmp]
46
48
 
47
49
  Generate database docs (html format)
48
50
 
49
- npx mr gen projectName[@aliasName] db-docs [tmp]
51
+ npx mushroom-cli gen projectName[@aliasName] db-docs [tmp]
50
52
 
51
53
  Generate npm package (tarball file)
52
54
 
53
- npx mr gen projectName[@aliasName] js-driver-tarball
55
+ npx mushroom-cli gen projectName[@aliasName] js-driver-tarball
54
56
 
55
57
  Generate javascript driver
56
58
 
57
- npx mr gen projectName[@aliasName] js-driver
59
+ npx mushroom-cli gen projectName[@aliasName] js-driver
58
60
 
59
61
  Generate typescript driver
60
62
 
61
- npx mr gen projectName[@aliasName] ts-driver
63
+ npx mushroom-cli gen projectName[@aliasName] ts-driver
62
64
 
63
65
  Generate entities for Mushroom project in CSharp and using MongoDb
64
66
 
65
- npx mr gen projectName[@aliasName] c#-mongo
67
+ npx mushroom-cli gen projectName[@aliasName] c#-mongo
66
68
 
67
69
  Generate entities for CSharp project which calls Mushroom API
68
70
 
69
- npx mr gen projectName[@aliasName] c#-driver
71
+ npx mushroom-cli gen projectName[@aliasName] c#-driver
72
+
73
+ ## Update
74
+
75
+ Update ts code
76
+
77
+ npx mushroom-cli update ts-code [file-path.ts] [remote-ts-code-url]
70
78
 
71
- # Other
79
+ ## Other
72
80
 
73
81
  Disable checking for update:
74
82
 
75
- npx mr parameters --no-check-update
83
+ npx mushroom-cli parameters --no-check-update
package/check-update.js CHANGED
@@ -2,7 +2,7 @@ const fetch = require('node-fetch');
2
2
 
3
3
 
4
4
  function checkUpdate() {
5
- return fetch('https://registry.npmjs.org/mushroom-cli/latest')
5
+ return fetch('https://registry.npmjs.org/mushroom-cli/')
6
6
  .then(res => res.json());
7
7
  }
8
8
 
package/common.js CHANGED
@@ -1,11 +1,13 @@
1
1
  const fs = require("fs");
2
+ const path = require('path');
3
+ const readlineSync = require("readline-sync");
2
4
 
3
- function ensureFolder(path) {
5
+ function ensureFolder(path, isFolder) {
4
6
  const index = path.lastIndexOf("/");
5
- if (index <= 0){
7
+ if (!isFolder && index <= 0){
6
8
  return;
7
9
  }
8
- path = path.substr(0, index);
10
+ if (!isFolder) path = path.substring(0, index);
9
11
  if (path === "." || path === "..")
10
12
  return;
11
13
 
@@ -125,8 +127,109 @@ function getEntities(projectApiConfiguration, roles, getAll) {
125
127
  return entities;
126
128
  }
127
129
 
130
+ function processTemplate(basePath, templatePath) {
131
+ const templateJsonContent = fs.readFileSync(path.join(basePath, templatePath)).toString();
132
+ const template = JSON.parse(templateJsonContent);
133
+ if (template && typeof template !== "object") {
134
+ throw new TypeError(`Invalid template file ${templatePath}`);
135
+ }
136
+ const commands = template.steps;
137
+ if (!Array.isArray(commands)) {
138
+ throw new TypeError(`Invalid template file ${templatePath}`);
139
+ }
140
+
141
+ const variables = {};
142
+ commands.forEach(command => processCommand(command, basePath, variables));
143
+ return variables;
144
+ }
145
+
146
+ function processCommand(command, basePath, variables) {
147
+ switch (command.command) {
148
+ case "input":
149
+ processInputCommand(command, variables);
150
+ return;
151
+ case "set":
152
+ processSetCommand(command, variables);
153
+ return;
154
+ case "rename":
155
+ processRenameCommand(command, basePath, variables);
156
+ return;
157
+ case "delete":
158
+ processDeleteCommand(command, basePath, variables);
159
+ return;
160
+ case "replace":
161
+ processReplaceCommand(command, basePath, variables);
162
+ return;
163
+ }
164
+ }
165
+
166
+ function processInputCommand(command, variables) {
167
+ const value = readlineSync.question(command.message ?? "");
168
+ if (command.validation) {
169
+ if (command.validation.required) {
170
+ if (value === "") throw new TypeError(`${command.variable} is required`);
171
+ }
172
+ if (command.validation.pattern) {
173
+ if (!new RegExp(command.validation.pattern).test(value)) throw new Error(`${value} is not valid`);
174
+ }
175
+ }
176
+ variables[command.variable] = value;
177
+ }
178
+
179
+ function processSetCommand(command, variables) {
180
+ switch (command["function"]) {
181
+ case "New-GUID":
182
+ processSetGuidCommand(command.variable, variables);
183
+ break;
184
+ }
185
+ }
186
+
187
+ function processSetGuidCommand(variable, variables) {
188
+ variables[variable] = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
189
+ var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
190
+ return v.toString(16);
191
+ }).toUpperCase();
192
+ }
193
+
194
+ function processRenameCommand(command, basePath, variables) {
195
+ const input = replaceVariables(command.input, variables);
196
+ const output = replaceVariables(command.output, variables);
197
+
198
+ fs.renameSync(path.join(basePath, input), path.join(basePath, output));
199
+ }
200
+
201
+ function processDeleteCommand(command, basePath, variables) {
202
+ const input = replaceVariables(command.input, variables);
203
+
204
+ const deletedPath = path.join(basePath, input);
205
+ if (fs.lstatSync(deletedPath).isDirectory()) {
206
+ fs.rmdirSync(deletedPath, {recursive: true, force: true});
207
+ }
208
+ else {
209
+ fs.rmSync(deletedPath, {force: true});
210
+ }
211
+ }
212
+
213
+ function processReplaceCommand(command, basePath, variables) {
214
+ const files = command.input;
215
+ const pattern = new RegExp(command.pattern, "g");
216
+ const replacement = replaceVariables(command.replacement, variables);
217
+
218
+ files.forEach(file => {
219
+ const filePath = replaceVariables(file, variables);
220
+ let content = fs.readFileSync(path.join(basePath, filePath)).toString();
221
+ content = content.replace(pattern, () => replacement);
222
+ fs.writeFileSync(path.join(basePath, filePath), content);
223
+ });
224
+ }
225
+
226
+ function replaceVariables(str, variables) {
227
+ return str.replace(/\$\{(\w+)\}/g, (_, varName) => variables[varName] ?? "");
228
+ }
229
+
128
230
  module.exports = {
129
231
  ensureFolder: ensureFolder,
130
- getRootUrl : getRootUrl,
131
- getEntities : getEntities
232
+ getRootUrl: getRootUrl,
233
+ getEntities: getEntities,
234
+ processTemplate: processTemplate
132
235
  };
package/config.js CHANGED
@@ -75,8 +75,8 @@ function configGenDocs(args, mushroomConfig, projectName){
75
75
  const strRoles = args[index];
76
76
  if (strRoles) {
77
77
  const minusIndex = strRoles.indexOf("-");
78
- let grantedRoles = minusIndex == -1 ? strRoles : strRoles.substr(0, minusIndex);
79
- const deniedRoles = minusIndex == -1 ? "" : strRoles.substr(minusIndex + 1);
78
+ let grantedRoles = minusIndex == -1 ? strRoles : strRoles.substring(0, minusIndex);
79
+ const deniedRoles = minusIndex == -1 ? "" : strRoles.substring(minusIndex + 1);
80
80
  if (!grantedRoles)
81
81
  grantedRoles = "*";
82
82
  parameters.allowRoles = grantedRoles.split(",");
@@ -173,8 +173,8 @@ function configGenJavascriptDriver(args, mushroomConfig, projectName){
173
173
  const strRoles = args[index];
174
174
  if (strRoles) {
175
175
  const minusIndex = strRoles.indexOf("-");
176
- let grantedRoles = minusIndex == -1 ? strRoles : strRoles.substr(0, minusIndex);
177
- const deniedRoles = minusIndex == -1 ? "" : strRoles.substr(minusIndex + 1);
176
+ let grantedRoles = minusIndex == -1 ? strRoles : strRoles.substring(0, minusIndex);
177
+ const deniedRoles = minusIndex == -1 ? "" : strRoles.substring(minusIndex + 1);
178
178
  if (!grantedRoles)
179
179
  grantedRoles = "*";
180
180
  parameters.allowRoles = grantedRoles.split(",");
@@ -233,8 +233,8 @@ function configGenTypescriptDriver(args, mushroomConfig, projectName){
233
233
  const strRoles = args[index];
234
234
  if (strRoles) {
235
235
  const minusIndex = strRoles.indexOf("-");
236
- let grantedRoles = minusIndex == -1 ? strRoles : strRoles.substr(0, minusIndex);
237
- const deniedRoles = minusIndex == -1 ? "" : strRoles.substr(minusIndex + 1);
236
+ let grantedRoles = minusIndex == -1 ? strRoles : strRoles.substring(0, minusIndex);
237
+ const deniedRoles = minusIndex == -1 ? "" : strRoles.substring(minusIndex + 1);
238
238
  if (!grantedRoles)
239
239
  grantedRoles = "*";
240
240
  parameters.allowRoles = grantedRoles.split(",");
@@ -325,8 +325,8 @@ function configGenCSharpDriver(args, mushroomConfig, projectName){
325
325
  const strRoles = args[index];
326
326
  if (strRoles) {
327
327
  const minusIndex = strRoles.indexOf("-");
328
- let grantedRoles = minusIndex == -1 ? strRoles : strRoles.substr(0, minusIndex);
329
- const deniedRoles = minusIndex == -1 ? "" : strRoles.substr(minusIndex + 1);
328
+ let grantedRoles = minusIndex == -1 ? strRoles : strRoles.substring(0, minusIndex);
329
+ const deniedRoles = minusIndex == -1 ? "" : strRoles.substring(minusIndex + 1);
330
330
  if (!grantedRoles)
331
331
  grantedRoles = "*";
332
332
  parameters.allowRoles = grantedRoles.split(",");