mushroom-cli 1.1.1 → 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/common.js CHANGED
@@ -1,236 +1,237 @@
1
- const fs = require("fs");
2
- const path = require('path');
3
- const readlineSync = require("readline-sync");
4
-
5
- function ensureFolder(path, isFolder) {
6
- const index = path.lastIndexOf("/");
7
- if (!isFolder && index <= 0){
8
- return;
9
- }
10
- if (!isFolder) path = path.substring(0, index);
11
- if (path === "." || path === "..")
12
- return;
13
-
14
- if (!fs.existsSync(path)) {
15
- fs.mkdirSync(path, { recursive: true, mode: 493 }); // 493 = 755 in octa
16
- }
17
- }
18
-
19
- function getRootUrl(projectApiConfiguration) {
20
- return projectApiConfiguration.codeHint && projectApiConfiguration.codeHint.rootUrl
21
- ? projectApiConfiguration.codeHint.rootUrl
22
- : projectApiConfiguration.rootUrl;
23
- }
24
-
25
- function getEntities(projectApiConfiguration, roles, getAll) {
26
- if (!projectApiConfiguration || !projectApiConfiguration.entities || !Array.isArray(projectApiConfiguration.entities))
27
- return {};
28
-
29
- const entities = {};
30
- projectApiConfiguration.entities.forEach(entity => {
31
-
32
- if (entity.disabled)
33
- return;
34
-
35
- if (getAll) {
36
- entities[entity.name] = entity;
37
- entity.storageName = entity.storageName ? entity.storageName : entity.name;
38
- if (!roles || !roles.length)
39
- roles = ["*"];
40
-
41
- return;
42
- }
43
-
44
- for (const actionName in entity.actions) {
45
- if (!entity.actions.hasOwnProperty(actionName))
46
- continue;
47
-
48
- entity.actions[actionName].forEach(action => {
49
- if (!action.grant || !action.enabled || action.codeHint && action.codeHint.disabled)
50
- return;
51
-
52
- let allow = roles.indexOf("*") !== -1 || action.grant === "*";
53
-
54
- if (!allow)
55
- action.grant.split("|").forEach(function (roleGroup) {
56
- if (!roleGroup)
57
- return;
58
- roleGroup.split("+").forEach(function (grantedRole) {
59
- if (roles.indexOf(grantedRole) !== -1)
60
- allow = true;
61
- });
62
- });
63
-
64
- if (!allow)
65
- return;
66
-
67
- if (!(entity.name in entities))
68
- entities[entity.name] = {
69
- description: entity.description,
70
- storageName: entity.storageName ? entity.storageName : entity.name,
71
- fields: entity.fields,
72
- actions: {},
73
- views: {}
74
- };
75
-
76
- if (!entities[entity.name].actions[actionName])
77
- entities[entity.name].actions[actionName] = [];
78
-
79
- entities[entity.name].actions[actionName].push(action);
80
- });
81
-
82
- }
83
-
84
- for (const viewName in entity.views) {
85
- if (!entity.views.hasOwnProperty(viewName))
86
- continue;
87
-
88
- entity.views[viewName].forEach(function(view) {
89
- if (!view.grant || !view.enabled)
90
- return;
91
-
92
- let allow = roles.indexOf("*") !== -1 || view.grant === "*";
93
-
94
- view.grant.split("|").forEach(function (roleGroup) {
95
- if (!roleGroup)
96
- return;
97
- roleGroup.split("+").forEach(function (grantedRole) {
98
- if (roles.indexOf(grantedRole) !== -1)
99
- allow = true;
100
- });
101
- });
102
-
103
- if (!allow)
104
- return;
105
-
106
- if (!(entity.name in entities))
107
- entities[entity.name] = {
108
- description: entity.description,
109
- storageName: entity.storageName ? entity.storageName : entity.name,
110
- fields: entity.fields,
111
- actions: {},
112
- views: {}
113
- };
114
-
115
- if (!entities[entity.name].views[viewName])
116
- entities[entity.name].views[viewName] = [];
117
-
118
- entities[entity.name].views[viewName].push(view);
119
- });
120
- }
121
-
122
- if (entity.name in entities) {
123
- entities[entity.name].features = entity.features;
124
- }
125
- });
126
-
127
- return entities;
128
- }
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
- const r = Math.random() * 16 | 0;
190
- const v = c == 'x' ? r : (r & 0x3 | 0x8);
191
- return v.toString(16);
192
- }).toUpperCase();
193
- }
194
-
195
- function processRenameCommand(command, basePath, variables) {
196
- const input = replaceVariables(command.input, variables);
197
- const output = replaceVariables(command.output, variables);
198
-
199
- fs.renameSync(path.join(basePath, input), path.join(basePath, output));
200
- }
201
-
202
- function processDeleteCommand(command, basePath, variables) {
203
- const input = replaceVariables(command.input, variables);
204
-
205
- const deletedPath = path.join(basePath, input);
206
- if (fs.lstatSync(deletedPath).isDirectory()) {
207
- fs.rmSync(deletedPath, {recursive: true, force: true});
208
- }
209
- else {
210
- fs.rmSync(deletedPath, {force: true});
211
- }
212
- }
213
-
214
- function processReplaceCommand(command, basePath, variables) {
215
- const files = command.input;
216
- const pattern = new RegExp(command.pattern, "g");
217
- const replacement = replaceVariables(command.replacement, variables);
218
-
219
- files.forEach(file => {
220
- const filePath = replaceVariables(file, variables);
221
- let content = fs.readFileSync(path.join(basePath, filePath)).toString();
222
- content = content.replace(pattern, () => replacement);
223
- fs.writeFileSync(path.join(basePath, filePath), content);
224
- });
225
- }
226
-
227
- function replaceVariables(str, variables) {
228
- return str.replace(/\$\{(\w+)\}/g, (_, varName) => variables[varName] ?? "");
229
- }
230
-
231
- module.exports = {
232
- ensureFolder: ensureFolder,
233
- getRootUrl: getRootUrl,
234
- getEntities: getEntities,
235
- processTemplate: processTemplate
236
- };
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const readlineSync = require("readline-sync");
4
+
5
+ function ensureFolder(path, isFolder) {
6
+ const index = path.lastIndexOf("/");
7
+ if (!isFolder && index <= 0) {
8
+ return;
9
+ }
10
+ if (!isFolder) path = path.substring(0, index);
11
+ if (path === "." || path === "..") return;
12
+
13
+ if (!fs.existsSync(path)) {
14
+ fs.mkdirSync(path, { recursive: true, mode: 493 }); // 493 = 755 in octa
15
+ }
16
+ }
17
+
18
+ function getRootUrl(projectApiConfiguration) {
19
+ return projectApiConfiguration.codeHint &&
20
+ projectApiConfiguration.codeHint.rootUrl
21
+ ? projectApiConfiguration.codeHint.rootUrl
22
+ : projectApiConfiguration.rootUrl;
23
+ }
24
+
25
+ function getEntities(projectApiConfiguration, roles, getAll) {
26
+ if (
27
+ !projectApiConfiguration ||
28
+ !projectApiConfiguration.entities ||
29
+ !Array.isArray(projectApiConfiguration.entities)
30
+ )
31
+ return {};
32
+
33
+ const entities = {};
34
+ projectApiConfiguration.entities.forEach((entity) => {
35
+ if (entity.disabled) return;
36
+
37
+ if (getAll) {
38
+ entities[entity.name] = entity;
39
+ entity.storageName = entity.storageName
40
+ ? entity.storageName
41
+ : entity.name;
42
+ if (!roles || !roles.length) roles = ["*"];
43
+
44
+ return;
45
+ }
46
+
47
+ for (const actionName in entity.actions) {
48
+ if (!entity.actions.hasOwnProperty(actionName)) continue;
49
+
50
+ entity.actions[actionName].forEach((action) => {
51
+ if (
52
+ !action.grant ||
53
+ !action.enabled ||
54
+ (action.codeHint && action.codeHint.disabled)
55
+ )
56
+ return;
57
+
58
+ let allow = roles.indexOf("*") !== -1 || action.grant === "*";
59
+
60
+ if (!allow)
61
+ action.grant.split("|").forEach(function (roleGroup) {
62
+ if (!roleGroup) return;
63
+ roleGroup.split("+").forEach(function (grantedRole) {
64
+ if (roles.indexOf(grantedRole) !== -1) allow = true;
65
+ });
66
+ });
67
+
68
+ if (!allow) return;
69
+
70
+ if (!(entity.name in entities))
71
+ entities[entity.name] = {
72
+ description: entity.description,
73
+ storageName: entity.storageName ? entity.storageName : entity.name,
74
+ fields: entity.fields,
75
+ actions: {},
76
+ views: {},
77
+ };
78
+
79
+ if (!entities[entity.name].actions[actionName])
80
+ entities[entity.name].actions[actionName] = [];
81
+
82
+ entities[entity.name].actions[actionName].push(action);
83
+ });
84
+ }
85
+
86
+ for (const viewName in entity.views) {
87
+ if (!entity.views.hasOwnProperty(viewName)) continue;
88
+
89
+ entity.views[viewName].forEach(function (view) {
90
+ if (!view.grant || !view.enabled) return;
91
+
92
+ let allow = roles.indexOf("*") !== -1 || view.grant === "*";
93
+
94
+ view.grant.split("|").forEach(function (roleGroup) {
95
+ if (!roleGroup) return;
96
+ roleGroup.split("+").forEach(function (grantedRole) {
97
+ if (roles.indexOf(grantedRole) !== -1) allow = true;
98
+ });
99
+ });
100
+
101
+ if (!allow) return;
102
+
103
+ if (!(entity.name in entities))
104
+ entities[entity.name] = {
105
+ description: entity.description,
106
+ storageName: entity.storageName ? entity.storageName : entity.name,
107
+ fields: entity.fields,
108
+ actions: {},
109
+ views: {},
110
+ };
111
+
112
+ if (!entities[entity.name].views[viewName])
113
+ entities[entity.name].views[viewName] = [];
114
+
115
+ entities[entity.name].views[viewName].push(view);
116
+ });
117
+ }
118
+
119
+ if (entity.name in entities) {
120
+ entities[entity.name].features = entity.features;
121
+ }
122
+ });
123
+
124
+ return entities;
125
+ }
126
+
127
+ function processTemplate(basePath, templatePath) {
128
+ const templateJsonContent = fs
129
+ .readFileSync(path.join(basePath, templatePath))
130
+ .toString();
131
+ const template = JSON.parse(templateJsonContent);
132
+ if (template && typeof template !== "object") {
133
+ throw new TypeError(`Invalid template file ${templatePath}`);
134
+ }
135
+ const commands = template.steps;
136
+ if (!Array.isArray(commands)) {
137
+ throw new TypeError(`Invalid template file ${templatePath}`);
138
+ }
139
+
140
+ const variables = {};
141
+ commands.forEach((command) => processCommand(command, basePath, variables));
142
+ return variables;
143
+ }
144
+
145
+ function processCommand(command, basePath, variables) {
146
+ switch (command.command) {
147
+ case "input":
148
+ processInputCommand(command, variables);
149
+ return;
150
+ case "set":
151
+ processSetCommand(command, variables);
152
+ return;
153
+ case "rename":
154
+ processRenameCommand(command, basePath, variables);
155
+ return;
156
+ case "delete":
157
+ processDeleteCommand(command, basePath, variables);
158
+ return;
159
+ case "replace":
160
+ processReplaceCommand(command, basePath, variables);
161
+ return;
162
+ }
163
+ }
164
+
165
+ function processInputCommand(command, variables) {
166
+ const value = readlineSync.question(command.message ?? "");
167
+ if (command.validation) {
168
+ if (command.validation.required) {
169
+ if (value === "") throw new TypeError(`${command.variable} is required`);
170
+ }
171
+ if (command.validation.pattern) {
172
+ if (!new RegExp(command.validation.pattern).test(value))
173
+ 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"
189
+ .replace(/[xy]/g, (c) => {
190
+ const r = (Math.random() * 16) | 0;
191
+ const v = c == "x" ? r : (r & 0x3) | 0x8;
192
+ return v.toString(16);
193
+ })
194
+ .toUpperCase();
195
+ }
196
+
197
+ function processRenameCommand(command, basePath, variables) {
198
+ const input = replaceVariables(command.input, variables);
199
+ const output = replaceVariables(command.output, variables);
200
+
201
+ fs.renameSync(path.join(basePath, input), path.join(basePath, output));
202
+ }
203
+
204
+ function processDeleteCommand(command, basePath, variables) {
205
+ const input = replaceVariables(command.input, variables);
206
+
207
+ const deletedPath = path.join(basePath, input);
208
+ if (fs.lstatSync(deletedPath).isDirectory()) {
209
+ fs.rmSync(deletedPath, { recursive: true, force: true });
210
+ } else {
211
+ fs.rmSync(deletedPath, { force: true });
212
+ }
213
+ }
214
+
215
+ function processReplaceCommand(command, basePath, variables) {
216
+ const files = command.input;
217
+ const pattern = new RegExp(command.pattern, "g");
218
+ const replacement = replaceVariables(command.replacement, variables);
219
+
220
+ files.forEach((file) => {
221
+ const filePath = replaceVariables(file, variables);
222
+ let content = fs.readFileSync(path.join(basePath, filePath)).toString();
223
+ content = content.replace(pattern, () => replacement);
224
+ fs.writeFileSync(path.join(basePath, filePath), content);
225
+ });
226
+ }
227
+
228
+ function replaceVariables(str, variables) {
229
+ return str.replace(/\$\{(\w+)\}/g, (_, varName) => variables[varName] ?? "");
230
+ }
231
+
232
+ module.exports = {
233
+ ensureFolder: ensureFolder,
234
+ getRootUrl: getRootUrl,
235
+ getEntities: getEntities,
236
+ processTemplate: processTemplate,
237
+ };