mushroom-cli 1.2.0-alpha.10 → 1.2.0-alpha.12

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.
Files changed (43) hide show
  1. package/README.md +233 -211
  2. package/dist/check-update.js +5 -0
  3. package/dist/common.js +260 -0
  4. package/dist/config.js +394 -0
  5. package/{docs → dist/docs}/doc-mt.js +471 -523
  6. package/dist/docs/docs.v2.js +1758 -0
  7. package/{docs → dist/docs}/mt-dom-1.0.js +1025 -1268
  8. package/{docs → dist/docs}/mt.extension.js +142 -174
  9. package/dist/gen/gen-api-docs-v1.js +106 -0
  10. package/dist/gen/gen-api-docs.js +292 -0
  11. package/{gen → dist/gen}/gen-csharp.js +1265 -1399
  12. package/{gen → dist/gen}/gen-db-docs.js +204 -222
  13. package/{gen → dist/gen}/gen-js-driver-tarball.js +81 -101
  14. package/{gen → dist/gen}/gen-js-driver.js +116 -133
  15. package/{gen → dist/gen}/gen-ts-driver.js +427 -471
  16. package/{gen.js → dist/gen.js} +109 -124
  17. package/{help.js → dist/help.js} +53 -55
  18. package/dist/init/init-json.js +91 -0
  19. package/dist/init/init-netcore.js +30 -0
  20. package/dist/init.js +23 -0
  21. package/dist/master-file/master-file.js +185 -0
  22. package/dist/merge.js +70 -0
  23. package/dist/mr.js +75 -0
  24. package/dist/types.js +1 -0
  25. package/dist/update/tsCode.js +82 -0
  26. package/dist/update.js +20 -0
  27. package/package.json +54 -29
  28. package/check-update.js +0 -9
  29. package/common.js +0 -273
  30. package/config.js +0 -445
  31. package/docs/docs.html +0 -5190
  32. package/docs/docs.v2.html +0 -5066
  33. package/docs/docs.v2.js +0 -1563
  34. package/gen/gen-api-docs-v1.js +0 -125
  35. package/gen/gen-api-docs.js +0 -298
  36. package/init/init-json.js +0 -106
  37. package/init/init-netcore.js +0 -32
  38. package/init.js +0 -28
  39. package/master-file/master-file.js +0 -234
  40. package/merge.js +0 -84
  41. package/mr.js +0 -82
  42. package/update/tsCode.js +0 -20
  43. package/update.js +0 -24
@@ -1,222 +1,204 @@
1
- const fs = require("fs");
2
- const open = require("open");
3
- const common = require("../common.js");
4
-
5
- function encodeHtml(str) {
6
- if (str === null || str === undefined)
7
- return "";
8
- if (typeof str !== "string")
9
- str = str.toString();
10
- var html = str.replace(/&/g, "&");
11
- html = html.replace(/</g, "&lt;");
12
- html = html.replace(/>/g, "&gt;");
13
- html = html.replace(/"/g, "&quot;");
14
- html = html.replace(/'/g, "&apos;");
15
- return html;
16
- }
17
-
18
- function getConstrains(valField) {
19
- const constraints = [];
20
-
21
- if (valField.reference)
22
- constraints.push(`Ref to <a href="#${valField.reference}">${encodeHtml(valField.reference)}</a>`);
23
-
24
- if (valField.validation) {
25
- if (valField.validation.required)
26
- constraints.push("Not null");
27
- if (valField.validation.validValues && valField.validation.validValues.length)
28
- constraints.push(`Enum: ${valField.validation.validValues.map(encodeHtml).join(", ")}`);
29
- if (valField.validation.invalidValues && valField.validation.invalidValues.length)
30
- constraints.push(`Invalid values: ${valField.validation.invalidValues.map(encodeHtml).join(", ")}`);
31
- if (valField.validation.length !== undefined)
32
- constraints.push(`Length: ${valField.validation.length}`);
33
- if (valField.validation.minLength !== undefined)
34
- constraints.push(`Min length: ${valField.validation.minLength}`);
35
- if (valField.validation.maxLength !== undefined)
36
- constraints.push(`Max length: ${valField.validation.maxLength}`);
37
- if (valField.validation.gt !== undefined)
38
- constraints.push(`GT: ${encodeHtml(valField.validation.gt)}`);
39
- if (valField.validation.lt !== undefined)
40
- constraints.push(`LT: ${encodeHtml(valField.validation.lt)}`);
41
- if (valField.validation.min !== undefined)
42
- constraints.push(`Min: ${encodeHtml(valField.validation.min)}`);
43
- if (valField.validation.max !== undefined)
44
- constraints.push(`Max: ${encodeHtml(valField.validation.max)}`);
45
- if (valField.validation.pattern)
46
- constraints.push(`Pattern: ${encodeHtml(valField.validation.pattern)}`);
47
- }
48
-
49
- if (valField.defaultValue)
50
- constraints.push(`Default (${encodeHtml(valField.defaultValue)})`);
51
-
52
- return constraints.join("<br/>");
53
- }
54
-
55
- function getMongoTableDocs(valTable) {
56
- let str = `
57
- <table border="1" cellspacing="0" cellpadding="8" width="100%" bordercolor="#999">
58
- <tr>
59
- <th style="width: 1.75in">Column</th>
60
- <th style="width: 1in">DataType</th>
61
- <th style="width: 2in">Constraints</th>
62
- <th>Description</th>
63
- </tr>`;
64
- for (let keyField in valTable) {
65
- const valField = valTable[keyField];
66
- const isObject = valField.type === "object" || valField.type === "array" && valField.items.type === "object";
67
- const isArray = valField.type === "array";
68
-
69
- if (isObject) {
70
- str += `
71
- <tr>
72
- <td valign="top" rowspan="2">${keyField}</td>
73
- <td valign="top">${isArray ? "array of " : ""}object</td>
74
- <td valign="top">${getConstrains(isArray ? valField.items : valField)}</td>
75
- <td valign="top" style="word-break: break-word">${encodeHtml(valField.description)}</td>
76
- </tr>
77
- <tr>
78
- <td colspan="3">${getMongoTableDocs(isArray ? valField.items.fields : valField.fields)}</td>
79
- </tr>`;
80
- }
81
- else {
82
- str += `
83
- <tr>
84
- <td valign="top">${keyField}</td>
85
- <td valign="top">${isArray ? "array of " + valField.items.type : valField.type}</td>
86
- <td valign="top">${getConstrains(valField)}${isArray ? "<hr/>Array items:<br/>" + getConstrains(valField.items) : ""}</td>
87
- <td valign="top" style="word-break: break-word">${encodeHtml(valField.description)}</td>
88
- </tr>`;
89
- }
90
- }
91
- str += `</table>`;
92
- return str;
93
- }
94
-
95
- function getMongoDbDocs(entities, source) {
96
- let htmlStr = "";
97
- let toc = "";
98
- for (let keyTable in entities) {
99
- const valTable = entities[keyTable];
100
- const des = encodeHtml(valTable.description);
101
- htmlStr += `
102
- <h2 id="${keyTable}">${keyTable}</h2>
103
- <div>${des}</div>
104
- ${getMongoTableDocs(valTable.fields)}
105
- <br>`;
106
- toc += `<li><a href="#${keyTable}">${keyTable}</a> <span class="des">- ${des}</span></li>`;
107
- }
108
-
109
- return `
110
- <!DOCTYPE html>
111
- <html>
112
- <head>
113
- <meta charset=utf-8>
114
- <style>
115
- * {box-sizing: border-box}
116
- html, body {margin: 0 0; padding: 0 0; font-family: Calibri, 'helvetica neue', Arial, serifs}
117
- h2{background-color: #ccc;margin: 30px -20px 0;padding: 5px 10px;}
118
- hr{border-width: 0; border-top: 1px solid #ccc;}
119
- table {border-collapse: collapse;margin-top: 5px;}
120
- th{background-color: #d7d7d7}
121
- a{color: #000; text-decoration: none}
122
- a:hover{color: blue; text-decoration: underline}
123
-
124
- .nav{position: fixed; top: 0; left: 0; bottom: 0; width: 200px; z-index: 100; overflow: auto;border-right: 1px solid #bbb;background-color: #eee}
125
- ul#nav-toc{padding: 0;margin: 0}
126
- ul#nav-toc li{list-style: none;padding: 0;margin: 0;}
127
- ul#nav-toc a{display: block;padding: 5px 10px;}
128
- ul#nav-toc a:hover{background-color: #aaa; color: #fff; text-decoration: none}
129
- ul#nav-toc .des{display: none}
130
-
131
- :target {
132
- -webkit-animation: target-fade 2s 1;
133
- -moz-animation: target-fade 2s 1;
134
- }
135
-
136
- @-webkit-keyframes target-fade {
137
- 0% { background-color: #ff0; }
138
- 100% { background-color: #ccc; }
139
- }
140
- @-moz-keyframes target-fade {
141
- 0% { background-color: #ff0; }
142
- 100% { background-color: #ccc; }
143
- }
144
-
145
- body{padding-left: 200px}
146
- .paper{padding: 0 20px}
147
-
148
- ul#main-toc a{font-weight: bold}
149
-
150
- @media print {
151
- body{padding-left: 0}
152
- .nav{display: none}
153
- }
154
- @media only screen and (max-width: 768px) {
155
- body{padding-left: 0}
156
- .nav{display: none}
157
- }
158
- </style>
159
- <head>
160
- <body>
161
- <div class="nav">
162
- <ul id="nav-toc">${toc}</ul>
163
- </div>
164
- <div class="paper">
165
- <h1 style="text-align: center">Đặc tả Cơ sở dữ liệu</h1>
166
- Hệ quản trị cơ sở dữ liệu: <b>${source.db}</b>
167
- <ul id="main-toc">${toc}</ul>
168
- ${htmlStr}
169
- </div>
170
- </body>
171
- </html>`;
172
- }
173
-
174
- function genDatabaseDocs(args, projectName, project, alias, source) {
175
- const aliasProject = ((project.dbDocs || {}).alias || {})[alias];
176
-
177
- if (!aliasProject) {
178
- throw new Error(`Not found gen db-docs configuration. Please run 'npx mushroom-cli config ${projectName} gen db-docs OUTPUT_FILE.html${alias === "default" ? "" : " alias " + alias}' first`);
179
- }
180
-
181
- const entities = common.getEntities(source, aliasProject.allowRoles, true);
182
-
183
- let code;
184
- switch (source.db) {
185
- case "MongoDB":
186
- code = getMongoDbDocs(entities, source);
187
- break;
188
- //case "SqlServer":
189
- // break;
190
- case undefined:
191
- case null:
192
- case "":
193
- throw new Error(`Missing db configuration in ${aliasProject.output}`);
194
- default:
195
- throw new Error(`Unsupported gen db-docs for '${source.db}'`);
196
- }
197
-
198
- if (args[3] === "tmp") {
199
- const htmlFilePath = aliasProject.output + "_tmp.html";
200
- fs.writeFile(htmlFilePath, code, err => {
201
- if (err)
202
- throw err;
203
- open(htmlFilePath).then(() => {
204
- setTimeout(() => {
205
- fs.unlink(htmlFilePath, ex => {
206
- if (ex)
207
- console.error(ex);
208
- });
209
- }, 2000);
210
- }).catch(err => {console.error(err);});
211
- });
212
-
213
- return;
214
- }
215
-
216
- common.ensureFolder(aliasProject.output);
217
-
218
- fs.writeFileSync(aliasProject.output, code);
219
- console.log(`Generated Database docs for ${projectName}@${alias} to ${aliasProject.output}`);
220
- }
221
-
222
- module.exports = genDatabaseDocs;
1
+ import fs from 'fs';
2
+ import open from 'open';
3
+ import * as common from '../common.js';
4
+ function encodeHtml(str) {
5
+ if (str === null || str === undefined)
6
+ return '';
7
+ const s = typeof str !== 'string' ? str.toString() : str;
8
+ let html = s.replace(/&/g, '&amp;');
9
+ html = html.replace(/</g, '&lt;');
10
+ html = html.replace(/>/g, '&gt;');
11
+ html = html.replace(/"/g, '&quot;');
12
+ html = html.replace(/'/g, '&apos;');
13
+ return html;
14
+ }
15
+ function getConstrains(valField) {
16
+ if (!valField)
17
+ return '';
18
+ const constraints = [];
19
+ if (valField.reference)
20
+ constraints.push(`Ref to <a href="#${valField.reference}">${encodeHtml(valField.reference)}</a>`);
21
+ if (valField.validation) {
22
+ if (valField.validation.required)
23
+ constraints.push('Not null');
24
+ if (valField.validation.validValues && valField.validation.validValues.length)
25
+ constraints.push(`Enum: ${valField.validation.validValues.map(encodeHtml).join(', ')}`);
26
+ if (valField.validation.invalidValues && valField.validation.invalidValues.length)
27
+ constraints.push(`Invalid values: ${valField.validation.invalidValues.map(encodeHtml).join(', ')}`);
28
+ if (valField.validation.length !== undefined)
29
+ constraints.push(`Length: ${valField.validation.length}`);
30
+ if (valField.validation.minLength !== undefined)
31
+ constraints.push(`Min length: ${valField.validation.minLength}`);
32
+ if (valField.validation.maxLength !== undefined)
33
+ constraints.push(`Max length: ${valField.validation.maxLength}`);
34
+ if (valField.validation.gt !== undefined)
35
+ constraints.push(`GT: ${encodeHtml(valField.validation.gt)}`);
36
+ if (valField.validation.lt !== undefined)
37
+ constraints.push(`LT: ${encodeHtml(valField.validation.lt)}`);
38
+ if (valField.validation.min !== undefined)
39
+ constraints.push(`Min: ${encodeHtml(valField.validation.min)}`);
40
+ if (valField.validation.max !== undefined)
41
+ constraints.push(`Max: ${encodeHtml(valField.validation.max)}`);
42
+ if (valField.validation.pattern)
43
+ constraints.push(`Pattern: ${encodeHtml(valField.validation.pattern)}`);
44
+ }
45
+ if (valField.defaultValue)
46
+ constraints.push(`Default (${encodeHtml(valField.defaultValue)})`);
47
+ return constraints.join('<br/>');
48
+ }
49
+ function getMongoTableDocs(valTable) {
50
+ let str = `
51
+ <table border="1" cellspacing="0" cellpadding="8" width="100%" bordercolor="#999">
52
+ <tr>
53
+ <th style="width: 1.75in">Column</th>
54
+ <th style="width: 1in">DataType</th>
55
+ <th style="width: 2in">Constraints</th>
56
+ <th>Description</th>
57
+ </tr>`;
58
+ for (const keyField in valTable) {
59
+ const valField = valTable[keyField];
60
+ const isObject = valField.type === 'object' || (valField.type === 'array' && valField.items?.type === 'object');
61
+ const isArray = valField.type === 'array';
62
+ if (isObject) {
63
+ str += `
64
+ <tr>
65
+ <td valign="top" rowspan="2">${keyField}</td>
66
+ <td valign="top">${isArray ? 'array of ' : ''}object</td>
67
+ <td valign="top">${getConstrains(isArray ? valField.items : valField)}</td>
68
+ <td valign="top" style="word-break: break-word">${encodeHtml(valField.description)}</td>
69
+ </tr>
70
+ <tr>
71
+ <td colspan="3">${getMongoTableDocs((isArray ? valField.items?.fields : valField.fields) ?? {})}</td>
72
+ </tr>`;
73
+ }
74
+ else {
75
+ str += `
76
+ <tr>
77
+ <td valign="top">${keyField}</td>
78
+ <td valign="top">${isArray ? 'array of ' + valField.items?.type : valField.type}</td>
79
+ <td valign="top">${getConstrains(valField)}${isArray ? '<hr/>Array items:<br/>' + getConstrains(valField.items) : ''}</td>
80
+ <td valign="top" style="word-break: break-word">${encodeHtml(valField.description)}</td>
81
+ </tr>`;
82
+ }
83
+ }
84
+ str += `</table>`;
85
+ return str;
86
+ }
87
+ function getMongoDbDocs(entities, source) {
88
+ let htmlStr = '';
89
+ let toc = '';
90
+ for (const keyTable in entities) {
91
+ const valTable = entities[keyTable];
92
+ const des = encodeHtml(valTable.description);
93
+ htmlStr += `
94
+ <h2 id="${keyTable}">${keyTable}</h2>
95
+ <div>${des}</div>
96
+ ${getMongoTableDocs(valTable.fields)}
97
+ <br>`;
98
+ toc += `<li><a href="#${keyTable}">${keyTable}</a> <span class="des">- ${des}</span></li>`;
99
+ }
100
+ return `
101
+ <!DOCTYPE html>
102
+ <html>
103
+ <head>
104
+ <meta charset=utf-8>
105
+ <style>
106
+ * {box-sizing: border-box}
107
+ html, body {margin: 0 0; padding: 0 0; font-family: Calibri, 'helvetica neue', Arial, serifs}
108
+ h2{background-color: #ccc;margin: 30px -20px 0;padding: 5px 10px;}
109
+ hr{border-width: 0; border-top: 1px solid #ccc;}
110
+ table {border-collapse: collapse;margin-top: 5px;}
111
+ th{background-color: #d7d7d7}
112
+ a{color: #000; text-decoration: none}
113
+ a:hover{color: blue; text-decoration: underline}
114
+
115
+ .nav{position: fixed; top: 0; left: 0; bottom: 0; width: 200px; z-index: 100; overflow: auto;border-right: 1px solid #bbb;background-color: #eee}
116
+ ul#nav-toc{padding: 0;margin: 0}
117
+ ul#nav-toc li{list-style: none;padding: 0;margin: 0;}
118
+ ul#nav-toc a{display: block;padding: 5px 10px;}
119
+ ul#nav-toc a:hover{background-color: #aaa; color: #fff; text-decoration: none}
120
+ ul#nav-toc .des{display: none}
121
+
122
+ :target {
123
+ -webkit-animation: target-fade 2s 1;
124
+ -moz-animation: target-fade 2s 1;
125
+ }
126
+
127
+ @-webkit-keyframes target-fade {
128
+ 0% { background-color: #ff0; }
129
+ 100% { background-color: #ccc; }
130
+ }
131
+ @-moz-keyframes target-fade {
132
+ 0% { background-color: #ff0; }
133
+ 100% { background-color: #ccc; }
134
+ }
135
+
136
+ body{padding-left: 200px}
137
+ .paper{padding: 0 20px}
138
+
139
+ ul#main-toc a{font-weight: bold}
140
+
141
+ @media print {
142
+ body{padding-left: 0}
143
+ .nav{display: none}
144
+ }
145
+ @media only screen and (max-width: 768px) {
146
+ body{padding-left: 0}
147
+ .nav{display: none}
148
+ }
149
+ </style>
150
+ <head>
151
+ <body>
152
+ <div class="nav">
153
+ <ul id="nav-toc">${toc}</ul>
154
+ </div>
155
+ <div class="paper">
156
+ <h1 style="text-align: center">Đặc tả Cơ sở dữ liệu</h1>
157
+ Hệ quản trị cơ sở dữ liệu: <b>${source.db}</b>
158
+ <ul id="main-toc">${toc}</ul>
159
+ ${htmlStr}
160
+ </div>
161
+ </body>
162
+ </html>`;
163
+ }
164
+ export default function genDatabaseDocs(args, projectName, project, alias, source) {
165
+ const aliasProject = ((project.dbDocs || {}).alias || {})[alias];
166
+ if (!aliasProject) {
167
+ throw new Error(`Not found gen db-docs configuration. Please run 'npx mushroom-cli config ${projectName} gen db-docs OUTPUT_FILE.html${alias === 'default' ? '' : ' alias ' + alias}' first`);
168
+ }
169
+ const entities = common.getEntities(source, aliasProject.allowRoles, true);
170
+ let code;
171
+ switch (source.db) {
172
+ case 'MongoDB':
173
+ code = getMongoDbDocs(entities, source);
174
+ break;
175
+ case undefined:
176
+ case null:
177
+ throw new Error(`Missing db configuration in ${aliasProject.output}`);
178
+ default:
179
+ throw new Error(`Unsupported gen db-docs for '${source.db}'`);
180
+ }
181
+ if (args[3] === 'tmp') {
182
+ const htmlFilePath = aliasProject.output + '_tmp.html';
183
+ fs.writeFile(htmlFilePath, code, (err) => {
184
+ if (err)
185
+ throw err;
186
+ open(htmlFilePath)
187
+ .then(() => {
188
+ setTimeout(() => {
189
+ fs.unlink(htmlFilePath, (ex) => {
190
+ if (ex)
191
+ common.logError(ex);
192
+ });
193
+ }, 2000);
194
+ })
195
+ .catch((err) => {
196
+ common.logError(err);
197
+ });
198
+ });
199
+ return;
200
+ }
201
+ common.ensureFolder(aliasProject.output);
202
+ fs.writeFileSync(aliasProject.output, code);
203
+ common.logInfo(`Generated Database docs for ${projectName}@${alias} to ${aliasProject.output}`);
204
+ }
@@ -1,101 +1,81 @@
1
- const fs = require("fs");
2
- const path = require('path');
3
- var targz = require('targz');
4
- const javascriptDriverGenerator = require("./gen-js-driver");
5
- const typescriptDriverGenerator = require("./gen-ts-driver");
6
- const package = require("./../package.json");
7
-
8
- function compressAsync(src, dest, folderName) {
9
- return new Promise((resolve, reject) => {
10
- const options = {
11
- src: src,
12
- dest: dest
13
- };
14
- if (folderName) {
15
- options.tar = {
16
- map: (header) => {
17
- if (header.name === ".") return header;
18
- header.name = folderName + '/' + header.name;
19
- return header;
20
- }
21
- };
22
- }
23
-
24
- targz.compress(options, function(err){
25
- if(err) {
26
- reject(err);
27
- return;
28
- }
29
- resolve();
30
- });
31
- });
32
- }
33
-
34
- async function genJavascriptDriverTarball(args, projectName, project, alias, source) {
35
- const aliasProject = ((project.jsDriver || {}).alias || {})[alias];
36
-
37
- let tarballName = aliasProject.tarballName ? aliasProject.tarballName : projectName;
38
- tarballName = tarballName.toLowerCase().replace(/[^a-z-]/g, "-");
39
-
40
- const dirPath = path.join(path.dirname(aliasProject.output), tarballName);
41
-
42
- const versionGroup = package.version.match(/([^-]+)(-(\w+).*)?/);
43
- const currentDistTag = versionGroup[3] ? versionGroup[3] : "latest";
44
-
45
- const dependencies = [`"mushroomjs": "${currentDistTag}"`];
46
- if (Array.isArray(aliasProject.features) && aliasProject.features.includes("auth")) {
47
- dependencies.push(`"mushroomjs-auth": "${currentDistTag}"`);
48
- }
49
- if (Array.isArray(aliasProject.features) && aliasProject.features.includes("file")) {
50
- dependencies.push(`"mushroomjs-file": "${currentDistTag}"`);
51
- }
52
-
53
- const packageContent = `{
54
- "version": "1.0.${new Date().getTime()}",
55
- "name": "${tarballName}",
56
- "private": true,
57
- "main": "${tarballName}.js",
58
- "types": "${tarballName}.d.ts",
59
- "dependencies": {
60
- ${dependencies.join(',\n ')}
61
- }
62
- }`;
63
-
64
- if (fs.existsSync(dirPath)) {
65
- console.log("Clean up tarball folder");
66
- // fs.rmdirSync(dirPath, { recursive: true });
67
- fs.rmSync(dirPath, { recursive: true });
68
- }
69
-
70
- const tarballFileName = path.join(path.dirname(aliasProject.output), tarballName + ".tar.gz");
71
-
72
- if (fs.existsSync(tarballFileName)) {
73
- console.log("Clean up old tarball file");
74
- fs.unlinkSync(tarballFileName);
75
- }
76
-
77
- console.log("Creating tarball folder");
78
-
79
- fs.mkdirSync(dirPath, { recursive: true, mode: 0755 });
80
-
81
- console.log("Generating main file: " + tarballName + ".js");
82
- let code = javascriptDriverGenerator.genJavascriptDriverString(aliasProject, source);
83
- fs.writeFileSync(path.join(dirPath, tarballName + ".js"), code);
84
-
85
- console.log("Generating types file: " + tarballName + ".d.ts");
86
- code = typescriptDriverGenerator.genDtsString(aliasProject, source);
87
- fs.writeFileSync(path.join(dirPath, tarballName + ".d.ts"), code);
88
-
89
- console.log("Generating package file: package.json");
90
- fs.writeFileSync(path.join(dirPath, "package.json"), packageContent);
91
-
92
- console.log("Generating tarball file: " + tarballFileName);
93
- await compressAsync(dirPath, tarballFileName, tarballName);
94
-
95
- console.log("Generated tarball file");
96
- // fs.rmdirSync(dirPath, { recursive: true });
97
- fs.rmSync(dirPath, { recursive: true });
98
- console.log("Cleaned up tarball folder");
99
- }
100
-
101
- module.exports = genJavascriptDriverTarball;
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import targz from 'targz';
4
+ import * as common from '../common.js';
5
+ import * as javascriptDriverGenerator from './gen-js-driver.js';
6
+ import * as typescriptDriverGenerator from './gen-ts-driver.js';
7
+ import packageJson from '../../package.json' assert { type: 'json' };
8
+ function compressAsync(src, dest, folderName) {
9
+ return new Promise((resolve, reject) => {
10
+ const options = {
11
+ src: src,
12
+ dest: dest,
13
+ };
14
+ if (folderName) {
15
+ options.tar = {
16
+ map: (header) => {
17
+ if (header.name === '.')
18
+ return header;
19
+ header.name = folderName + '/' + header.name;
20
+ return header;
21
+ },
22
+ };
23
+ }
24
+ targz.compress(options, function (err) {
25
+ if (err) {
26
+ reject(err);
27
+ return;
28
+ }
29
+ resolve();
30
+ });
31
+ });
32
+ }
33
+ export default async function genJavascriptDriverTarball(args, projectName, project, alias, source) {
34
+ const aliasProject = ((project.jsDriver || {}).alias || {})[alias];
35
+ let tarballName = aliasProject.tarballName ? aliasProject.tarballName : projectName;
36
+ tarballName = tarballName.toLowerCase().replace(/[^a-z-]/g, '-');
37
+ const dirPath = path.join(path.dirname(aliasProject.output), tarballName);
38
+ const versionGroup = packageJson.version.match(/([^-]+)(-(\w+).*)?/);
39
+ const currentDistTag = versionGroup?.[3] ? versionGroup[3] : 'latest';
40
+ const dependencies = [`"mushroomjs": "${currentDistTag}"`];
41
+ if (Array.isArray(aliasProject.features) && aliasProject.features.includes('auth')) {
42
+ dependencies.push(`"mushroomjs-auth": "${currentDistTag}"`);
43
+ }
44
+ if (Array.isArray(aliasProject.features) && aliasProject.features.includes('file')) {
45
+ dependencies.push(`"mushroomjs-file": "${currentDistTag}"`);
46
+ }
47
+ const packageContent = `{
48
+ "version": "1.0.${new Date().getTime()}",
49
+ "name": "${tarballName}",
50
+ "private": true,
51
+ "main": "${tarballName}.js",
52
+ "types": "${tarballName}.d.ts",
53
+ "dependencies": {
54
+ ${dependencies.join(',\n ')}
55
+ }
56
+ }`;
57
+ if (fs.existsSync(dirPath)) {
58
+ common.logInfo('Clean up tarball folder');
59
+ fs.rmSync(dirPath, { recursive: true });
60
+ }
61
+ const tarballFileName = path.join(path.dirname(aliasProject.output), tarballName + '.tar.gz');
62
+ if (fs.existsSync(tarballFileName)) {
63
+ common.logInfo('Clean up old tarball file');
64
+ fs.unlinkSync(tarballFileName);
65
+ }
66
+ common.logInfo('Creating tarball folder');
67
+ fs.mkdirSync(dirPath, { recursive: true, mode: 0o755 });
68
+ common.logInfo('Generating main file:', tarballName + '.js');
69
+ let code = javascriptDriverGenerator.genJavascriptDriverString(aliasProject, source);
70
+ fs.writeFileSync(path.join(dirPath, tarballName + '.js'), code);
71
+ common.logInfo('Generating types file:', tarballName + '.d.ts');
72
+ code = typescriptDriverGenerator.genDtsString(aliasProject, source);
73
+ fs.writeFileSync(path.join(dirPath, tarballName + '.d.ts'), code);
74
+ common.logInfo('Generating package file: package.json');
75
+ fs.writeFileSync(path.join(dirPath, 'package.json'), packageContent);
76
+ common.logInfo('Generating tarball file:', tarballFileName);
77
+ await compressAsync(dirPath, tarballFileName, tarballName);
78
+ common.logInfo('Generated tarball file');
79
+ fs.rmSync(dirPath, { recursive: true });
80
+ common.logInfo('Cleaned up tarball folder');
81
+ }