mushroom-cli 1.2.0-alpha.9 → 1.2.1
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 +208 -161
- package/dist/check-update.js +6 -0
- package/dist/common.js +284 -0
- package/dist/config.js +394 -0
- package/dist/docs/bootstrap-theme.min.css +6 -0
- package/dist/docs/bootstrap.min.css +6 -0
- package/dist/docs/bootstrap.min.js +7 -0
- package/dist/docs/doc-mt.js +633 -0
- package/dist/docs/docs.html +5380 -0
- package/{docs → dist/docs}/docs.v2.html +5206 -5065
- package/dist/docs/docs.v2.js +2037 -0
- package/dist/docs/jquery.min.js +4 -0
- package/dist/docs/mt-dom-1.0.js +1398 -0
- package/dist/docs/mt.extension.js +199 -0
- package/dist/gen/gen-api-docs-v1.js +110 -0
- package/dist/gen/gen-api-docs.js +327 -0
- package/{gen → dist/gen}/gen-csharp.js +1256 -1399
- package/{gen → dist/gen}/gen-db-docs.js +204 -222
- package/{gen → dist/gen}/gen-js-driver-tarball.js +81 -101
- package/{gen → dist/gen}/gen-js-driver.js +116 -133
- package/dist/gen/gen-ts-driver.js +600 -0
- package/{gen.js → dist/gen.js} +109 -124
- package/{help.js → dist/help.js} +53 -55
- package/dist/init/init-json.js +92 -0
- package/dist/init/init-netcore.js +30 -0
- package/dist/init.js +23 -0
- package/dist/master-file/master-file.js +185 -0
- package/dist/merge.js +70 -0
- package/dist/mr.js +77 -0
- package/dist/types.js +1 -0
- package/dist/update/tsCode.js +82 -0
- package/dist/update.js +20 -0
- package/package.json +64 -29
- package/check-update.js +0 -9
- package/common.js +0 -273
- package/config.js +0 -445
- package/docs/doc-mt.js +0 -523
- package/docs/docs.html +0 -5190
- package/docs/docs.v2.js +0 -1558
- package/docs/mt-dom-1.0.js +0 -1268
- package/docs/mt.extension.js +0 -174
- package/gen/gen-api-docs-v1.js +0 -125
- package/gen/gen-api-docs.js +0 -298
- package/gen/gen-ts-driver.js +0 -471
- package/init/init-json.js +0 -106
- package/init/init-netcore.js +0 -32
- package/init.js +0 -28
- package/master-file/master-file.js +0 -234
- package/merge.js +0 -84
- package/mr.js +0 -82
- package/update/tsCode.js +0 -20
- package/update.js +0 -24
package/README.md
CHANGED
|
@@ -1,161 +1,208 @@
|
|
|
1
|
-
# Mushroom CLI
|
|
2
|
-
|
|
3
|
-
## Init
|
|
4
|
-
|
|
5
|
-
Init a mushroom json file
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
Config gen
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
Generate
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
1
|
+
# Mushroom CLI
|
|
2
|
+
|
|
3
|
+
## Init
|
|
4
|
+
|
|
5
|
+
Init a mushroom json file
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx mushroom-cli init json ./api/SampleProject.json [silent]
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Init a DotNet Core project which pre-install Mushroom Server
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx mushroom-cli init netcore [projectName]
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Configure
|
|
18
|
+
|
|
19
|
+
Add the mushroom json file into mushroom-config.json
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npx mushroom-cli config add ./api/yourProjectName.json
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Config gen docs api
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx mushroom-cli config gen projectName docs ./wwwroot/docs.html [alias aliasName] [features file,auth,mail,driver] [roles Admin,User-Contributor]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Config gen database docs
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npx mushroom-cli config gen projectName "db-docs" ./wwwroot/db-docs.html [alias aliasName] [features file,auth,mail]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Config gen javascript driver
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npx mushroom-cli config gen projectName "js-driver" ./src/api/mushroom.api.js [alias aliasName] [roles Admin,User-Contributor] [tarball tarballName]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Config gen typescript driver
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npx mushroom-cli config gen projectName "ts-driver" ./src/api/mushroom.api.ts [alias aliasName] [roles Admin,User-Contributor]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Config gen entities for Mushroom project in CSharp and using MongoDb
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npx mushroom-cli config gen projectName "c#-mongo" ./API/Generated.cs [alias aliasName]
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Config gen entities for CSharp project which calls Mushroom API
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npx mushroom-cli config gen projectName "c#-driver" ./API/Generated.cs [roles Admin,User-Contributor] [alias aliasName] [access-modifier public] [features file,auth,mail]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Genarate
|
|
62
|
+
|
|
63
|
+
Generate docs api (html format)
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npx mushroom-cli gen projectName[@aliasName] docs [v1] [tmp]
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Generate database docs (html format)
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npx mushroom-cli gen projectName[@aliasName] "db-docs" [tmp]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Generate npm package (tarball file)
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npx mushroom-cli gen projectName[@aliasName] "js-driver-tarball"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Generate javascript driver
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npx mushroom-cli gen projectName[@aliasName] "js-driver"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Generate typescript driver
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
npx mushroom-cli gen projectName[@aliasName] "ts-driver"
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Generate entities for Mushroom project in CSharp and using MongoDb
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
npx mushroom-cli gen projectName[@aliasName] "c#-mongo"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Generate entities for CSharp project which calls Mushroom API
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
npx mushroom-cli gen projectName[@aliasName] "c#-driver"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Merge
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
npx mushroom-cli merge output-path.json
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Update
|
|
112
|
+
|
|
113
|
+
Update ts code (configuration was stored in mushroom-config.json file)
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
npx mushroom-cli update "ts-code"
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Update ts code (simple call)
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
npx mushroom-cli update "ts-code" file-path.ts remote-ts-code-url
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Other
|
|
126
|
+
|
|
127
|
+
Disable checking for update:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
npx mushroom-cli parameters --no-check-update
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Version
|
|
134
|
+
|
|
135
|
+
### 1.2.1
|
|
136
|
+
|
|
137
|
+
Improvement/Changes:
|
|
138
|
+
|
|
139
|
+
- gen-docs: fix bugs
|
|
140
|
+
- gen-csharp: fix signarute for RegisterCustomViewMethod method
|
|
141
|
+
- init netcore:
|
|
142
|
+
- change: show warning message when replacing content in unexisting file (instead of throwing error)
|
|
143
|
+
|
|
144
|
+
### 1.2.0
|
|
145
|
+
|
|
146
|
+
New features:
|
|
147
|
+
|
|
148
|
+
- Support merge command
|
|
149
|
+
- Support type for View result
|
|
150
|
+
- gen-ts-driver: Support offline mode
|
|
151
|
+
- init netcore: Support some new set-functions: `ToLowerCase`, `ToUpperCase`, `Trim`, `Replace`, `New-RandomNumber`, `New-RandomString`
|
|
152
|
+
- gen-docs: New gen docs engine v1.2 - deal with very big api docs
|
|
153
|
+
- support minify
|
|
154
|
+
- allow user to add and change API root url
|
|
155
|
+
- add `JavascriptParamModel`, `JavascriptRequestBodyModel`, `JavascriptResultModel` tab in code snippets of custom methods
|
|
156
|
+
- add docs for `inline` mode of download file and zip file(s), `allowSmallerSize` for thumb
|
|
157
|
+
- support links for Roles and Code Snippets
|
|
158
|
+
- add docs for response body of custom methods
|
|
159
|
+
- allow collapse object fields
|
|
160
|
+
- add validation of fields in custom methods and parameters in views
|
|
161
|
+
|
|
162
|
+
Improvement/Changes:
|
|
163
|
+
|
|
164
|
+
- init netcore
|
|
165
|
+
- Operating fail if unknown command or set-function found
|
|
166
|
+
- gen c#-mongo
|
|
167
|
+
- support System.Text.Json on resources
|
|
168
|
+
- generate resource classes with `partial`
|
|
169
|
+
- gen c#-driver
|
|
170
|
+
- Generate resource classes with `partial`
|
|
171
|
+
- gen ts-driver
|
|
172
|
+
- export type for object fields
|
|
173
|
+
- generate type for custom methods of action
|
|
174
|
+
- generate description for fields, entities
|
|
175
|
+
- generate type for getCountAsync declaration
|
|
176
|
+
- gen docs
|
|
177
|
+
- add default-value information of fields
|
|
178
|
+
- Improve documentation generation: view, custom action.
|
|
179
|
+
- filter role list
|
|
180
|
+
- format cURL to easy to copy and paste to Postman
|
|
181
|
+
- lighten font on MacOS
|
|
182
|
+
- donot use css and js files of bootstrap and jquery in CDN, copy file locally to enable to view offline
|
|
183
|
+
- curl of custom method is missing header X-HTTP-Method-Override
|
|
184
|
+
- Correct generate query string and request body at HTTP and cURL tabs in code snippets of custom methods
|
|
185
|
+
- Correct generate param and body at Javascript tab in code snippets of custom methods
|
|
186
|
+
- Correct generate JavascriptModel in code snippets
|
|
187
|
+
- improve copy function
|
|
188
|
+
- display the description of entities
|
|
189
|
+
|
|
190
|
+
Fixed bugs:
|
|
191
|
+
|
|
192
|
+
- gen docs
|
|
193
|
+
- Generate wrong required field for view parameter
|
|
194
|
+
- doesn't exclude fields of object in an array
|
|
195
|
+
- gen-ts
|
|
196
|
+
- add null value for nullable field type
|
|
197
|
+
|
|
198
|
+
### 1.1.1
|
|
199
|
+
|
|
200
|
+
Improvement:
|
|
201
|
+
|
|
202
|
+
- `mushroom-cli update tsCode` can read the configuration in mushroom-config.json file
|
|
203
|
+
|
|
204
|
+
### 1.1.0
|
|
205
|
+
|
|
206
|
+
Support MushroomServer 0.2.25.4 or later
|
|
207
|
+
|
|
208
|
+
Support mushroomjs 1.1.0 or later
|
package/dist/common.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import readlineSync from 'readline-sync';
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
export function getPackageVersion() {
|
|
8
|
+
try {
|
|
9
|
+
const content = fs.readFileSync(path.join(__dirname, '../package.json')).toString();
|
|
10
|
+
const pkg = JSON.parse(content);
|
|
11
|
+
return pkg.version;
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return '<!unable to get package version>';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function ensureFolder(path, isFolder) {
|
|
18
|
+
const index = path.lastIndexOf('/');
|
|
19
|
+
if (!isFolder && index <= 0) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!isFolder)
|
|
23
|
+
path = path.substring(0, index);
|
|
24
|
+
if (path === '.' || path === '..')
|
|
25
|
+
return;
|
|
26
|
+
if (!fs.existsSync(path)) {
|
|
27
|
+
fs.mkdirSync(path, { recursive: true, mode: 0o755 });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function getRootUrl(projectApiConfiguration) {
|
|
31
|
+
return projectApiConfiguration.codeHint && projectApiConfiguration.codeHint.rootUrl
|
|
32
|
+
? projectApiConfiguration.codeHint.rootUrl
|
|
33
|
+
: projectApiConfiguration.rootUrl;
|
|
34
|
+
}
|
|
35
|
+
export function getEntities(projectApiConfiguration, roles, getAll) {
|
|
36
|
+
if (!projectApiConfiguration || !projectApiConfiguration.entities || !Array.isArray(projectApiConfiguration.entities))
|
|
37
|
+
return {};
|
|
38
|
+
const entities = {};
|
|
39
|
+
projectApiConfiguration.entities.forEach((entity) => {
|
|
40
|
+
if (entity.disabled)
|
|
41
|
+
return;
|
|
42
|
+
if (getAll) {
|
|
43
|
+
entities[entity.name] = entity;
|
|
44
|
+
entity.storageName = entity.storageName ? entity.storageName : entity.name;
|
|
45
|
+
if (!roles || !roles.length)
|
|
46
|
+
roles = ['*'];
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
for (const actionName in entity.actions) {
|
|
50
|
+
entity.actions[actionName].forEach((action) => {
|
|
51
|
+
var _a;
|
|
52
|
+
if (!action.grant || !action.enabled || (action.codeHint && action.codeHint.disabled))
|
|
53
|
+
return;
|
|
54
|
+
const allow = roles?.includes('*') || action.grant === '*';
|
|
55
|
+
if (!allow) {
|
|
56
|
+
action.grant = action.grant
|
|
57
|
+
.split('|')
|
|
58
|
+
.map((roleGroup) => {
|
|
59
|
+
if (!roleGroup)
|
|
60
|
+
return null;
|
|
61
|
+
const ok = roleGroup.split('+').every((grantedRole) => roles?.includes(grantedRole));
|
|
62
|
+
return ok ? roleGroup : null;
|
|
63
|
+
})
|
|
64
|
+
.filter((roleGroup) => roleGroup)
|
|
65
|
+
.join('|');
|
|
66
|
+
if (!action.grant)
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (!(entity.name in entities)) {
|
|
70
|
+
entities[entity.name] = {
|
|
71
|
+
name: entity.name,
|
|
72
|
+
description: entity.description,
|
|
73
|
+
storageName: entity.storageName ? entity.storageName : entity.name,
|
|
74
|
+
fields: entity.fields,
|
|
75
|
+
views: {},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const actions = ((_a = entities[entity.name]).actions ?? (_a.actions = {}));
|
|
79
|
+
actions[actionName] ?? (actions[actionName] = []);
|
|
80
|
+
actions[actionName].push(action);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
for (const viewName in entity.views) {
|
|
84
|
+
entity.views[viewName].forEach((view) => {
|
|
85
|
+
var _a;
|
|
86
|
+
if (!view.grant || !view.enabled)
|
|
87
|
+
return;
|
|
88
|
+
const allow = roles?.indexOf('*') !== -1 || view.grant === '*';
|
|
89
|
+
if (!allow) {
|
|
90
|
+
view.grant = view.grant
|
|
91
|
+
.split('|')
|
|
92
|
+
.map((roleGroup) => {
|
|
93
|
+
if (!roleGroup)
|
|
94
|
+
return null;
|
|
95
|
+
const ok = roleGroup.split('+').every((grantedRole) => roles?.includes(grantedRole));
|
|
96
|
+
return ok ? roleGroup : null;
|
|
97
|
+
})
|
|
98
|
+
.filter((roleGroup) => roleGroup)
|
|
99
|
+
.join('|');
|
|
100
|
+
if (!view.grant)
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (!(entity.name in entities)) {
|
|
104
|
+
entities[entity.name] = {
|
|
105
|
+
name: entity.name,
|
|
106
|
+
description: entity.description,
|
|
107
|
+
storageName: entity.storageName ? entity.storageName : entity.name,
|
|
108
|
+
fields: entity.fields,
|
|
109
|
+
actions: {},
|
|
110
|
+
views: {},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const views = ((_a = entities[entity.name]).views ?? (_a.views = {}));
|
|
114
|
+
views[viewName] ?? (views[viewName] = []);
|
|
115
|
+
views[viewName].push(view);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (entity.name in entities) {
|
|
119
|
+
entities[entity.name].features = entity.features;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return entities;
|
|
123
|
+
}
|
|
124
|
+
export function filterRoles(roleList, allowRoleList) {
|
|
125
|
+
const roles = !allowRoleList || allowRoleList.includes('*')
|
|
126
|
+
? [...roleList]
|
|
127
|
+
: roleList.filter((role) => allowRoleList.includes(role.name));
|
|
128
|
+
roles.push({
|
|
129
|
+
name: '[Mọi người dùng]',
|
|
130
|
+
description: 'Tất cả người dùng (kể cả chưa đăng nhập)',
|
|
131
|
+
});
|
|
132
|
+
return roles;
|
|
133
|
+
}
|
|
134
|
+
export function processTemplate(basePath, templatePath) {
|
|
135
|
+
const templateJsonContent = fs.readFileSync(path.join(basePath, templatePath)).toString();
|
|
136
|
+
const template = JSON.parse(templateJsonContent);
|
|
137
|
+
if (template && typeof template !== 'object') {
|
|
138
|
+
throw new TypeError(`Invalid template file ${templatePath}`);
|
|
139
|
+
}
|
|
140
|
+
const commands = template.steps;
|
|
141
|
+
if (!Array.isArray(commands)) {
|
|
142
|
+
throw new TypeError(`Invalid template file ${templatePath}`);
|
|
143
|
+
}
|
|
144
|
+
const variables = {};
|
|
145
|
+
commands.forEach((command) => processCommand(command, basePath, variables));
|
|
146
|
+
return variables;
|
|
147
|
+
}
|
|
148
|
+
function processCommand(command, basePath, variables) {
|
|
149
|
+
const commandName = command.command;
|
|
150
|
+
switch (commandName) {
|
|
151
|
+
case 'input':
|
|
152
|
+
processInputCommand(command, variables);
|
|
153
|
+
return;
|
|
154
|
+
case 'set':
|
|
155
|
+
processSetCommand(command, variables);
|
|
156
|
+
return;
|
|
157
|
+
case 'rename':
|
|
158
|
+
processRenameCommand(command, basePath, variables);
|
|
159
|
+
return;
|
|
160
|
+
case 'delete':
|
|
161
|
+
processDeleteCommand(command, basePath, variables);
|
|
162
|
+
return;
|
|
163
|
+
case 'replace':
|
|
164
|
+
processReplaceCommand(command, basePath, variables);
|
|
165
|
+
return;
|
|
166
|
+
default:
|
|
167
|
+
throw new Error(`Unknown command: ${commandName}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function processInputCommand(command, variables) {
|
|
171
|
+
const value = readlineSync.question(command.message ?? '');
|
|
172
|
+
if (command.validation) {
|
|
173
|
+
if (command.validation.required) {
|
|
174
|
+
if (value === '')
|
|
175
|
+
throw new TypeError(`${command.variable} is required`);
|
|
176
|
+
}
|
|
177
|
+
if (command.validation.pattern) {
|
|
178
|
+
if (!new RegExp(command.validation.pattern).test(value))
|
|
179
|
+
throw new Error(`${value} is not valid`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
variables[command.variable] = value;
|
|
183
|
+
}
|
|
184
|
+
function processSetCommand(command, variables) {
|
|
185
|
+
switch (command['function']) {
|
|
186
|
+
case 'New-GUID':
|
|
187
|
+
processSetNewGuidCommand(command.variable, variables);
|
|
188
|
+
break;
|
|
189
|
+
case 'New-RandomNumber':
|
|
190
|
+
processSetNewRandomNumberCommand(command.variable, variables, command.min, command.max);
|
|
191
|
+
break;
|
|
192
|
+
case 'New-RandomString':
|
|
193
|
+
processSetNewRandomStringCommand(command.variable, variables, command.length, command.characterPool);
|
|
194
|
+
break;
|
|
195
|
+
case 'ToLowerCase':
|
|
196
|
+
toLowerCommand(command.variable, variables, command.input);
|
|
197
|
+
break;
|
|
198
|
+
case 'ToUpperCase':
|
|
199
|
+
toUpperCaseCommand(command.variable, variables, command.input);
|
|
200
|
+
break;
|
|
201
|
+
case 'Trim':
|
|
202
|
+
trimCommand(command.variable, variables, command.input);
|
|
203
|
+
break;
|
|
204
|
+
case 'Replace':
|
|
205
|
+
replaceCommand(command.variable, variables, command.input, command.replacement, command.pattern, command.mode);
|
|
206
|
+
break;
|
|
207
|
+
default:
|
|
208
|
+
throw new Error(`Unknown set-command: ${command['function']}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function processSetNewGuidCommand(variable, variables) {
|
|
212
|
+
variables[variable] = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
|
|
213
|
+
.replace(/[xy]/g, (c) => {
|
|
214
|
+
const r = (Math.random() * 16) | 0;
|
|
215
|
+
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
216
|
+
return v.toString(16);
|
|
217
|
+
})
|
|
218
|
+
.toUpperCase();
|
|
219
|
+
}
|
|
220
|
+
function processSetNewRandomNumberCommand(variable, variables, min = 0, max = (1 << 30) * 4) {
|
|
221
|
+
variables[variable] = Math.floor(min + Math.random() * (max - min)).toString();
|
|
222
|
+
}
|
|
223
|
+
function processSetNewRandomStringCommand(variable, variables, length, characterPool = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM') {
|
|
224
|
+
let result = '';
|
|
225
|
+
for (let i = 0; i < length; i++) {
|
|
226
|
+
result += characterPool.charAt(Math.floor(Math.random() * characterPool.length));
|
|
227
|
+
}
|
|
228
|
+
variables[variable] = result;
|
|
229
|
+
}
|
|
230
|
+
function toLowerCommand(variable, variables, input) {
|
|
231
|
+
variables[variable] = variables[input].toLowerCase();
|
|
232
|
+
}
|
|
233
|
+
function toUpperCaseCommand(variable, variables, input) {
|
|
234
|
+
variables[variable] = variables[input].toUpperCase();
|
|
235
|
+
}
|
|
236
|
+
function trimCommand(variable, variables, input) {
|
|
237
|
+
variables[variable] = variables[input].trim();
|
|
238
|
+
}
|
|
239
|
+
function replaceCommand(variable, variables, input, replacement, pattern, mode) {
|
|
240
|
+
const regex = new RegExp(pattern, mode);
|
|
241
|
+
variables[variable] = variables[input].replace(regex, replacement);
|
|
242
|
+
}
|
|
243
|
+
function processRenameCommand(command, basePath, variables) {
|
|
244
|
+
const input = replaceVariables(command.input, variables);
|
|
245
|
+
const output = replaceVariables(command.output, variables);
|
|
246
|
+
fs.renameSync(path.join(basePath, input), path.join(basePath, output));
|
|
247
|
+
}
|
|
248
|
+
function processDeleteCommand(command, basePath, variables) {
|
|
249
|
+
const input = replaceVariables(command.input, variables);
|
|
250
|
+
const deletedPath = path.join(basePath, input);
|
|
251
|
+
if (fs.lstatSync(deletedPath).isDirectory()) {
|
|
252
|
+
fs.rmSync(deletedPath, { recursive: true, force: true });
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
fs.rmSync(deletedPath, { force: true });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function processReplaceCommand(command, basePath, variables) {
|
|
259
|
+
const files = command.input;
|
|
260
|
+
const pattern = new RegExp(command.pattern ?? '', 'g');
|
|
261
|
+
const replacement = replaceVariables(command.replacement ?? '', variables);
|
|
262
|
+
files.forEach((file) => {
|
|
263
|
+
const filePath = path.join(basePath, replaceVariables(file, variables));
|
|
264
|
+
if (!fs.existsSync(filePath)) {
|
|
265
|
+
console.warn(`File ${file} does not exist, skipping...`);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
let content = fs.readFileSync(filePath).toString();
|
|
269
|
+
content = content.replace(pattern, () => replacement);
|
|
270
|
+
fs.writeFileSync(filePath, content);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
function replaceVariables(str, variables) {
|
|
274
|
+
return str.replace(/\$\{(\w+)\}/g, (_, varName) => variables[varName] ?? '');
|
|
275
|
+
}
|
|
276
|
+
export function logInfo(message, ...parameters) {
|
|
277
|
+
console.log(message, ...parameters);
|
|
278
|
+
}
|
|
279
|
+
export function logWarning(message, ...parameters) {
|
|
280
|
+
console.warn(message, ...parameters);
|
|
281
|
+
}
|
|
282
|
+
export function logError(message, ...parameters) {
|
|
283
|
+
console.error(message, ...parameters);
|
|
284
|
+
}
|