underpost 2.7.1 → 2.7.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.
Files changed (213) hide show
  1. package/.dockerignore +13 -13
  2. package/.env.development +7 -7
  3. package/.env.production +7 -7
  4. package/.env.test +7 -7
  5. package/.github/workflows/publish.yml +26 -0
  6. package/.nycrc +9 -9
  7. package/.prettierignore +12 -12
  8. package/.prettierrc +9 -9
  9. package/.vscode/extensions.json +72 -72
  10. package/.vscode/settings.json +100 -99
  11. package/Dockerfile +89 -89
  12. package/LICENSE +21 -21
  13. package/README.md +96 -96
  14. package/bin/db.js +172 -119
  15. package/bin/deploy.js +582 -661
  16. package/bin/dns.js +1 -1
  17. package/bin/file.js +92 -92
  18. package/bin/index.js +53 -53
  19. package/bin/install.js +398 -357
  20. package/bin/shortcut.js +44 -44
  21. package/bin/ssl.js +65 -64
  22. package/bin/util.js +182 -182
  23. package/bin/vs.js +35 -35
  24. package/conf.js +251 -249
  25. package/docker-compose.yml +67 -67
  26. package/jsconfig.json +7 -7
  27. package/jsdoc.json +32 -32
  28. package/nodemon.json +6 -6
  29. package/package.json +137 -132
  30. package/prometheus.yml +36 -36
  31. package/setup.sh +24 -24
  32. package/src/api/core/core.controller.js +69 -69
  33. package/src/api/core/core.model.js +11 -11
  34. package/src/api/core/core.router.js +23 -23
  35. package/src/api/core/core.service.js +29 -29
  36. package/src/api/crypto/crypto.controller.js +51 -51
  37. package/src/api/crypto/crypto.model.js +23 -23
  38. package/src/api/crypto/crypto.router.js +20 -20
  39. package/src/api/crypto/crypto.service.js +64 -64
  40. package/src/api/default/default.controller.js +69 -69
  41. package/src/api/default/default.model.js +20 -20
  42. package/src/api/default/default.router.js +23 -23
  43. package/src/api/default/default.service.js +31 -31
  44. package/src/api/file/file.controller.js +53 -51
  45. package/src/api/file/file.model.js +19 -19
  46. package/src/api/file/file.router.js +21 -20
  47. package/src/api/file/file.service.js +76 -70
  48. package/src/api/instance/instance.controller.js +69 -69
  49. package/src/api/instance/instance.model.js +36 -36
  50. package/src/api/instance/instance.router.js +33 -33
  51. package/src/api/instance/instance.service.js +48 -48
  52. package/src/api/test/test.controller.js +59 -59
  53. package/src/api/test/test.model.js +14 -14
  54. package/src/api/test/test.router.js +21 -21
  55. package/src/api/test/test.service.js +35 -35
  56. package/src/api/user/user.build.js +16 -0
  57. package/src/api/user/user.controller.js +70 -70
  58. package/src/api/user/user.model.js +65 -65
  59. package/src/api/user/user.router.js +345 -345
  60. package/src/api/user/user.service.js +479 -479
  61. package/src/api.js +23 -23
  62. package/src/client/Default.index.js +40 -40
  63. package/src/client/components/core/Account.js +290 -290
  64. package/src/client/components/core/AgGrid.js +160 -160
  65. package/src/client/components/core/Auth.js +19 -19
  66. package/src/client/components/core/Badge.js +32 -32
  67. package/src/client/components/core/BlockChain.js +41 -41
  68. package/src/client/components/core/Blog.js +9 -9
  69. package/src/client/components/core/BtnIcon.js +101 -94
  70. package/src/client/components/core/CalendarCore.js +458 -319
  71. package/src/client/components/core/Chat.js +64 -64
  72. package/src/client/components/core/ColorPalette.js +5267 -5267
  73. package/src/client/components/core/CommonJs.js +735 -732
  74. package/src/client/components/core/Content.js +193 -49
  75. package/src/client/components/core/Css.js +1064 -1027
  76. package/src/client/components/core/CssCore.js +817 -796
  77. package/src/client/components/core/D3Chart.js +44 -44
  78. package/src/client/components/core/Docs.js +229 -229
  79. package/src/client/components/core/DropDown.js +164 -164
  80. package/src/client/components/core/EventsUI.js +46 -54
  81. package/src/client/components/core/FileExplorer.js +699 -624
  82. package/src/client/components/core/FullScreen.js +45 -45
  83. package/src/client/components/core/Input.js +346 -259
  84. package/src/client/components/core/JoyStick.js +77 -77
  85. package/src/client/components/core/Keyboard.js +73 -73
  86. package/src/client/components/core/LoadingAnimation.js +179 -157
  87. package/src/client/components/core/LogIn.js +187 -181
  88. package/src/client/components/core/LogOut.js +58 -52
  89. package/src/client/components/core/Logger.js +26 -26
  90. package/src/client/components/core/Modal.js +1612 -1596
  91. package/src/client/components/core/NotificationManager.js +84 -84
  92. package/src/client/components/core/Panel.js +613 -413
  93. package/src/client/components/core/PanelForm.js +468 -0
  94. package/src/client/components/core/Polyhedron.js +162 -162
  95. package/src/client/components/core/Recover.js +204 -204
  96. package/src/client/components/core/Responsive.js +53 -53
  97. package/src/client/components/core/RichText.js +51 -27
  98. package/src/client/components/core/Router.js +76 -77
  99. package/src/client/components/core/Scroll.js +34 -0
  100. package/src/client/components/core/SignUp.js +125 -125
  101. package/src/client/components/core/SocketIo.js +72 -72
  102. package/src/client/components/core/Stream.js +113 -113
  103. package/src/client/components/core/ToggleSwitch.js +87 -87
  104. package/src/client/components/core/ToolTip.js +26 -26
  105. package/src/client/components/core/Translate.js +437 -408
  106. package/src/client/components/core/Validator.js +100 -100
  107. package/src/client/components/core/VanillaJs.js +460 -457
  108. package/src/client/components/core/Wallet.js +106 -106
  109. package/src/client/components/core/Webhook.js +25 -25
  110. package/src/client/components/core/Worker.js +272 -272
  111. package/src/client/components/default/CommonDefault.js +29 -29
  112. package/src/client/components/default/CssDefault.js +13 -13
  113. package/src/client/components/default/ElementsDefault.js +38 -38
  114. package/src/client/components/default/LogInDefault.js +41 -41
  115. package/src/client/components/default/LogOutDefault.js +28 -28
  116. package/src/client/components/default/MenuDefault.js +389 -389
  117. package/src/client/components/default/RoutesDefault.js +48 -48
  118. package/src/client/components/default/SettingsDefault.js +16 -16
  119. package/src/client/components/default/SignUpDefault.js +9 -9
  120. package/src/client/components/default/SocketIoDefault.js +54 -54
  121. package/src/client/components/default/TranslateDefault.js +7 -7
  122. package/src/client/public/default/assets/mailer/api-user-check.png +0 -0
  123. package/src/client/public/default/assets/mailer/api-user-invalid-token.png +0 -0
  124. package/src/client/public/default/assets/mailer/api-user-recover.png +0 -0
  125. package/src/client/public/default/browserconfig.xml +11 -11
  126. package/src/client/public/default/manifest.webmanifest +68 -68
  127. package/src/client/public/default/plantuml/client-conf.svg +1 -0
  128. package/src/client/public/default/plantuml/client-schema.svg +1 -0
  129. package/src/client/public/default/plantuml/cron-conf.svg +1 -0
  130. package/src/client/public/default/plantuml/cron-schema.svg +1 -0
  131. package/src/client/public/default/plantuml/server-conf.svg +1 -0
  132. package/src/client/public/default/plantuml/server-schema.svg +1 -0
  133. package/src/client/public/default/plantuml/ssr-conf.svg +1 -0
  134. package/src/client/public/default/plantuml/ssr-schema.svg +1 -0
  135. package/src/client/public/default/sitemap +147 -147
  136. package/src/client/public/default/yandex-browser-manifest.json +8 -8
  137. package/src/client/public/doc/sitemap +147 -147
  138. package/src/client/public/test/sitemap +147 -147
  139. package/src/client/services/core/core.service.js +170 -152
  140. package/src/client/services/crypto/crypto.service.js +70 -70
  141. package/src/client/services/default/default.management.js +345 -345
  142. package/src/client/services/default/default.service.js +89 -89
  143. package/src/client/services/file/file.service.js +70 -70
  144. package/src/client/services/instance/instance.management.js +74 -74
  145. package/src/client/services/instance/instance.service.js +89 -89
  146. package/src/client/services/test/test.service.js +70 -70
  147. package/src/client/services/user/user.management.js +50 -50
  148. package/src/client/services/user/user.service.js +89 -89
  149. package/src/client/ssr/Render.js +16 -16
  150. package/src/client/ssr/body-components/CacheControl.js +114 -113
  151. package/src/client/ssr/body-components/DefaultSplashScreen.js +79 -79
  152. package/src/client/ssr/email-components/DefaultRecoverEmail.js +21 -21
  153. package/src/client/ssr/email-components/DefaultVerifyEmail.js +17 -17
  154. package/src/client/ssr/head-components/Css.js +241 -241
  155. package/src/client/ssr/head-components/DefaultScripts.js +3 -3
  156. package/src/client/ssr/head-components/Microdata.js +11 -11
  157. package/src/client/ssr/head-components/Production.js +1 -1
  158. package/src/client/ssr/head-components/PwaDefault.js +59 -59
  159. package/src/client/ssr/head-components/Seo.js +14 -14
  160. package/src/client/sw/default.sw.js +201 -201
  161. package/src/client/sw/template.sw.js +84 -84
  162. package/src/client.build.js +22 -22
  163. package/src/client.dev.js +21 -21
  164. package/src/cron.js +25 -25
  165. package/src/db/DataBaseProvider.js +34 -34
  166. package/src/db/mariadb/MariaDB.js +33 -33
  167. package/src/db/mongo/MongooseDB.js +46 -46
  168. package/src/dns.js +22 -22
  169. package/src/index.js +42 -29
  170. package/src/mailer/EmailRender.js +69 -69
  171. package/src/mailer/MailerProvider.js +96 -96
  172. package/src/proxy.js +22 -22
  173. package/src/runtime/lampp/Lampp.js +69 -44
  174. package/src/runtime/nginx/Nginx.js +3 -3
  175. package/src/runtime/xampp/Xampp.js +49 -49
  176. package/src/server/auth.js +235 -204
  177. package/src/server/backup.js +101 -94
  178. package/src/server/client-build-live.js +72 -72
  179. package/src/server/client-build.js +705 -699
  180. package/src/server/client-dev-server.js +60 -58
  181. package/src/server/client-formatted.js +48 -48
  182. package/src/server/client-icons.js +149 -150
  183. package/src/server/conf.js +860 -611
  184. package/src/server/dns.js +98 -98
  185. package/src/server/downloader.js +42 -42
  186. package/src/server/logger.js +180 -180
  187. package/src/server/network.js +122 -122
  188. package/src/server/peer.js +33 -33
  189. package/src/server/process.js +66 -66
  190. package/src/server/prompt-optimizer.js +28 -28
  191. package/src/server/proxy.js +118 -118
  192. package/src/server/runtime.js +444 -393
  193. package/src/server/ssl.js +109 -107
  194. package/src/server.js +25 -25
  195. package/src/ws/IoInterface.js +45 -45
  196. package/src/ws/IoServer.js +39 -39
  197. package/src/ws/core/channels/core.ws.chat.js +23 -23
  198. package/src/ws/core/channels/core.ws.mailer.js +35 -35
  199. package/src/ws/core/channels/core.ws.stream.js +31 -31
  200. package/src/ws/core/core.ws.connection.js +28 -28
  201. package/src/ws/core/core.ws.emit.js +14 -14
  202. package/src/ws/core/core.ws.server.js +24 -24
  203. package/src/ws/core/management/core.ws.chat.js +8 -8
  204. package/src/ws/core/management/core.ws.mailer.js +16 -16
  205. package/src/ws/core/management/core.ws.stream.js +8 -8
  206. package/src/ws/default/channels/default.ws.main.js +16 -16
  207. package/src/ws/default/default.ws.connection.js +22 -22
  208. package/src/ws/default/default.ws.emit.js +14 -14
  209. package/src/ws/default/default.ws.server.js +20 -20
  210. package/src/ws/default/management/default.ws.main.js +8 -8
  211. package/startup.js +11 -11
  212. package/supervisord-openssh-server.conf +4 -4
  213. package/test/api.test.js +60 -60
@@ -1,699 +1,705 @@
1
- 'use strict';
2
-
3
- import fs from 'fs-extra';
4
- import { srcFormatted, componentFormatted, viewFormatted } from './client-formatted.js';
5
- import { loggerFactory } from './logger.js';
6
- import { cap, newInstance, orderArrayFromAttrInt, titleFormatted } from '../client/components/core/CommonJs.js';
7
- import UglifyJS from 'uglify-js';
8
- import { minify } from 'html-minifier-terser';
9
- import dotenv from 'dotenv';
10
- import AdmZip from 'adm-zip';
11
- import * as dir from 'path';
12
- import { shellExec } from './process.js';
13
- import swaggerAutoGen from 'swagger-autogen';
14
- import { SitemapStream, streamToPromise } from 'sitemap';
15
- import { Readable } from 'stream';
16
- import { buildIcons, buildTextImg, getBufferPngText } from './client-icons.js';
17
-
18
- dotenv.config();
19
-
20
- // Static Site Generation (SSG)
21
-
22
- const buildAcmeChallengePath = (acmeChallengeFullPath = '') => {
23
- fs.mkdirSync(acmeChallengeFullPath, {
24
- recursive: true,
25
- });
26
- fs.writeFileSync(`${acmeChallengeFullPath}/.gitkeep`, '', 'utf8');
27
- };
28
-
29
- const fullBuild = async ({
30
- path,
31
- logger,
32
- client,
33
- db,
34
- dists,
35
- rootClientPath,
36
- acmeChallengeFullPath,
37
- publicClientId,
38
- iconsBuild,
39
- metadata,
40
- }) => {
41
- logger.warn('Full build', rootClientPath);
42
-
43
- fs.removeSync(rootClientPath);
44
-
45
- buildAcmeChallengePath(acmeChallengeFullPath);
46
-
47
- if (fs.existsSync(`./src/client/public/${publicClientId}`)) {
48
- if (iconsBuild) {
49
- const defaultBaseIconFolderPath = `src/client/public/${publicClientId}/assets/logo`;
50
- if (!fs.existsSync(defaultBaseIconFolderPath)) fs.mkdirSync(defaultBaseIconFolderPath, { recursive: true });
51
- const defaultBaseIconPath = `${defaultBaseIconFolderPath}/base-icon.png`;
52
- if (!fs.existsSync(defaultBaseIconPath))
53
- await buildTextImg(metadata.title, { debugFilename: defaultBaseIconPath });
54
-
55
- if (path === '/' && !fs.existsSync(`./src/client/public/${publicClientId}/site.webmanifest`))
56
- await buildIcons({ publicClientId, metadata });
57
- }
58
- fs.copySync(
59
- `./src/client/public/${publicClientId}`,
60
- rootClientPath /* {
61
- filter: function (name) {
62
- console.log(name);
63
- return true;
64
- },
65
- } */,
66
- );
67
- } else if (fs.existsSync(`./engine-private/src/client/public/${publicClientId}`)) {
68
- switch (publicClientId) {
69
- case 'mysql_test':
70
- if (db) {
71
- fs.copySync(`./engine-private/src/client/public/${publicClientId}`, rootClientPath);
72
- fs.writeFileSync(
73
- `${rootClientPath}/index.php`,
74
- fs
75
- .readFileSync(`${rootClientPath}/index.php`, 'utf8')
76
- .replace('test_servername', 'localhost')
77
- .replace('test_username', db.user)
78
- .replace('test_password', db.password)
79
- .replace('test_dbname', db.name),
80
- 'utf8',
81
- );
82
- }
83
- break;
84
-
85
- default:
86
- break;
87
- }
88
- }
89
- if (dists)
90
- for (const dist of dists) {
91
- if ('folder' in dist) {
92
- fs.mkdirSync(`${rootClientPath}${dist.public_folder}`, { recursive: true });
93
- fs.copySync(dist.folder, `${rootClientPath}${dist.public_folder}`);
94
- }
95
- if ('styles' in dist) {
96
- fs.mkdirSync(`${rootClientPath}${dist.public_styles_folder}`, { recursive: true });
97
- fs.copySync(dist.styles, `${rootClientPath}${dist.public_styles_folder}`);
98
- }
99
- }
100
- };
101
-
102
- const buildClient = async (options = { liveClientBuildPaths: [], instances: [] }) => {
103
- const logger = loggerFactory(import.meta);
104
- const confClient = JSON.parse(fs.readFileSync(`./conf/conf.client.json`, 'utf8'));
105
- const confServer = JSON.parse(fs.readFileSync(`./conf/conf.server.json`, 'utf8'));
106
- const confSSR = JSON.parse(fs.readFileSync(`./conf/conf.ssr.json`, 'utf8'));
107
- const packageData = JSON.parse(fs.readFileSync(`./package.json`, 'utf8'));
108
- const acmeChallengePath = `/.well-known/acme-challenge`;
109
- const publicPath = `./public`;
110
-
111
- // { srcBuildPath, publicBuildPath }
112
- const enableLiveRebuild =
113
- options && options.liveClientBuildPaths && options.liveClientBuildPaths.length > 0 ? true : false;
114
-
115
- let currentPort = parseInt(process.env.PORT) + 1;
116
- for (const host of Object.keys(confServer)) {
117
- const paths = orderArrayFromAttrInt(Object.keys(confServer[host]), 'length', 'asc');
118
- for (const path of paths) {
119
- if (
120
- options &&
121
- options.instances &&
122
- options.instances.length > 0 &&
123
- !options.instances.find((i) => i.path === path && i.host === host)
124
- )
125
- continue;
126
- const {
127
- runtime,
128
- client,
129
- directory,
130
- disabledRebuild,
131
- minifyBuild,
132
- db,
133
- redirect,
134
- apis,
135
- iconsBuild,
136
- docsBuild,
137
- apiBaseProxyPath,
138
- apiBaseHost,
139
- ttiLoadTimeLimit,
140
- singleReplica,
141
- } = confServer[host][path];
142
- if (singleReplica) continue;
143
- if (!confClient[client]) confClient[client] = {};
144
- const { components, dists, views, services, metadata, publicRef } = confClient[client];
145
- let backgroundImage;
146
- if (metadata) {
147
- backgroundImage = metadata.backgroundImage;
148
- if (metadata.thumbnail) metadata.thumbnail = `${path === '/' ? path : `${path}/`}${metadata.thumbnail}`;
149
- }
150
- const rootClientPath = directory ? directory : `${publicPath}/${host}${path}`;
151
- const port = newInstance(currentPort);
152
- const publicClientId = publicRef ? publicRef : client;
153
- const fullBuildEnabled = !process.argv.includes('l') && !confServer[host][path].liteBuild && !enableLiveRebuild;
154
- // const baseHost = process.env.NODE_ENV === 'production' ? `https://${host}` : `http://localhost:${port}`;
155
- const baseHost = process.env.NODE_ENV === 'production' ? `https://${host}` : ``;
156
- // ''; // process.env.NODE_ENV === 'production' ? `https://${host}` : ``;
157
- currentPort++;
158
-
159
- const acmeChallengeFullPath = directory
160
- ? `${directory}${acmeChallengePath}`
161
- : `${publicPath}/${host}${acmeChallengePath}`;
162
-
163
- if (!enableLiveRebuild) buildAcmeChallengePath(acmeChallengeFullPath);
164
-
165
- if (redirect || disabledRebuild) continue;
166
-
167
- if (!enableLiveRebuild && runtime === 'lampp' && client === 'wordpress') {
168
- shellExec(`node bin/install linux wordpress ${host}${path}`);
169
- shellExec(`node bin/db ${host}${path} create`);
170
- continue;
171
- }
172
-
173
- if (fullBuildEnabled)
174
- // !(confServer[host]['/'] && confServer[host]['/'].liteBuild)
175
- await fullBuild({
176
- path,
177
- logger,
178
- client,
179
- db,
180
- dists,
181
- rootClientPath,
182
- acmeChallengeFullPath,
183
- publicClientId,
184
- iconsBuild,
185
- metadata,
186
- });
187
-
188
- if (components)
189
- for (const module of Object.keys(components)) {
190
- if (!fs.existsSync(`${rootClientPath}/components/${module}`))
191
- fs.mkdirSync(`${rootClientPath}/components/${module}`, { recursive: true });
192
-
193
- for (const component of components[module]) {
194
- const jsSrcPath = `./src/client/components/${module}/${component}.js`;
195
- const jsPublicPath = `${rootClientPath}/components/${module}/${component}.js`;
196
-
197
- if (enableLiveRebuild && !options.liveClientBuildPaths.find((p) => p.srcBuildPath === jsSrcPath)) continue;
198
-
199
- const jsSrc = componentFormatted(
200
- await srcFormatted(fs.readFileSync(jsSrcPath, 'utf8')),
201
- module,
202
- dists,
203
- path,
204
- 'components',
205
- baseHost,
206
- );
207
- fs.writeFileSync(
208
- jsPublicPath,
209
- minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
210
- 'utf8',
211
- );
212
- }
213
- }
214
-
215
- if (services) {
216
- for (const module of services) {
217
- if (!fs.existsSync(`${rootClientPath}/services/${module}`))
218
- fs.mkdirSync(`${rootClientPath}/services/${module}`, { recursive: true });
219
-
220
- if (fs.existsSync(`./src/client/services/${module}/${module}.service.js`)) {
221
- const jsSrcPath = `./src/client/services/${module}/${module}.service.js`;
222
- const jsPublicPath = `${rootClientPath}/services/${module}/${module}.service.js`;
223
- if (enableLiveRebuild && !options.liveClientBuildPaths.find((p) => p.srcBuildPath === jsSrcPath)) continue;
224
-
225
- let jsSrc = componentFormatted(
226
- await srcFormatted(fs.readFileSync(jsSrcPath, 'utf8')),
227
- module,
228
- dists,
229
- path,
230
- 'services',
231
- baseHost,
232
- );
233
- if (module === 'core' && process.env.NODE_ENV === 'production') {
234
- if (apiBaseHost)
235
- jsSrc = jsSrc.replace(
236
- 'const getBaseHost = () => location.host;',
237
- `const getBaseHost = () => '${apiBaseHost}';`,
238
- );
239
- if (apiBaseProxyPath)
240
- jsSrc = jsSrc.replace('${getProxyPath()}api/', `${apiBaseProxyPath}${process.env.BASE_API}/`);
241
- }
242
- fs.writeFileSync(
243
- jsPublicPath,
244
- minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
245
- 'utf8',
246
- );
247
- }
248
- }
249
-
250
- for (const module of services) {
251
- if (fs.existsSync(`./src/client/services/${module}/${module}.management.js`)) {
252
- const jsSrcPath = `./src/client/services/${module}/${module}.management.js`;
253
- const jsPublicPath = `${rootClientPath}/services/${module}/${module}.management.js`;
254
- if (enableLiveRebuild && !options.liveClientBuildPaths.find((p) => p.srcBuildPath === jsSrcPath)) continue;
255
-
256
- const jsSrc = componentFormatted(
257
- await srcFormatted(fs.readFileSync(jsSrcPath, 'utf8')),
258
- module,
259
- dists,
260
- path,
261
- 'services',
262
- baseHost,
263
- );
264
- fs.writeFileSync(
265
- jsPublicPath,
266
- minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
267
- 'utf8',
268
- );
269
- }
270
- }
271
- }
272
-
273
- const buildId = `${client}.index`;
274
- const siteMapLinks = [];
275
-
276
- if (views) {
277
- // build service worker
278
- if (path === '/') {
279
- const jsSrcPath = fs.existsSync(`./src/client/sw/${publicClientId}.sw.js`)
280
- ? `./src/client/sw/${publicClientId}.sw.js`
281
- : `./src/client/sw/default.sw.js`;
282
-
283
- const jsPublicPath = `${rootClientPath}/sw.js`;
284
-
285
- if (!(enableLiveRebuild && !options.liveClientBuildPaths.find((p) => p.srcBuildPath === jsSrcPath))) {
286
- const jsSrc = viewFormatted(await srcFormatted(fs.readFileSync(jsSrcPath, 'utf8')), dists, path, baseHost);
287
-
288
- fs.writeFileSync(
289
- jsPublicPath,
290
- minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
291
- 'utf8',
292
- );
293
- }
294
- }
295
- if (
296
- !(
297
- enableLiveRebuild &&
298
- !options.liveClientBuildPaths.find(
299
- (p) => p.srcBuildPath.startsWith(`./src/client/ssr`) || p.srcBuildPath.slice(-9) === '.index.js',
300
- )
301
- )
302
- )
303
- for (const view of views) {
304
- const buildPath = `${
305
- rootClientPath[rootClientPath.length - 1] === '/' ? rootClientPath.slice(0, -1) : rootClientPath
306
- }${view.path === '/' ? view.path : `${view.path}/`}`;
307
-
308
- if (!fs.existsSync(buildPath)) fs.mkdirSync(buildPath, { recursive: true });
309
-
310
- logger.info('View build', buildPath);
311
-
312
- const jsSrc = viewFormatted(
313
- await srcFormatted(fs.readFileSync(`./src/client/${view.client}.index.js`, 'utf8')),
314
- dists,
315
- path,
316
- baseHost,
317
- );
318
-
319
- fs.writeFileSync(
320
- `${buildPath}${buildId}.js`,
321
- minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
322
- 'utf8',
323
- );
324
-
325
- // const title = `${metadata && metadata.title ? metadata.title : cap(client)}${
326
- // view.title ? ` | ${view.title}` : view.path !== '/' ? ` | ${titleFormatted(view.path)}` : ''
327
- // }`;
328
-
329
- const title = `${
330
- view.title ? `${view.title} | ` : view.path !== '/' ? `${titleFormatted(view.path)} | ` : ''
331
- }${metadata && metadata.title ? metadata.title : cap(client)}`;
332
-
333
- const canonicalURL = `https://${host}${path}${
334
- view.path === '/' ? (path === '/' ? '' : '/') : path === '/' ? `${view.path.slice(1)}/` : `${view.path}/`
335
- }`;
336
- const ssrPath = path === '/' ? path : `${path}/`;
337
-
338
- let ssrHeadComponents = ``;
339
- let ssrBodyComponents = ``;
340
- if ('ssr' in view) {
341
- // https://metatags.io/
342
- if (process.env.NODE_ENV === 'production' && !confSSR[view.ssr].head.includes('Production'))
343
- confSSR[view.ssr].head.unshift('Production');
344
-
345
- for (const ssrHeadComponent of confSSR[view.ssr].head) {
346
- let SrrComponent;
347
- eval(
348
- await srcFormatted(
349
- fs.readFileSync(`./src/client/ssr/head-components/${ssrHeadComponent}.js`, 'utf8'),
350
- ),
351
- );
352
-
353
- switch (ssrHeadComponent) {
354
- case 'Pwa':
355
- const validPwaBuild =
356
- metadata &&
357
- fs.existsSync(`./src/client/public/${publicClientId}/browserconfig.xml`) &&
358
- fs.existsSync(`./src/client/public/${publicClientId}/site.webmanifest`);
359
-
360
- if (view.path === '/' && validPwaBuild) {
361
- // build webmanifest
362
- const webmanifestJson = JSON.parse(
363
- fs.readFileSync(`./src/client/public/${publicClientId}/site.webmanifest`, 'utf8'),
364
- );
365
- if (metadata.title) {
366
- webmanifestJson.name = metadata.title;
367
- webmanifestJson.short_name = metadata.title;
368
- }
369
- if (metadata.description) {
370
- webmanifestJson.description = metadata.description;
371
- }
372
- if (metadata.themeColor) {
373
- webmanifestJson.theme_color = metadata.themeColor;
374
- webmanifestJson.background_color = metadata.themeColor;
375
- }
376
- fs.writeFileSync(
377
- `${buildPath}site.webmanifest`,
378
- JSON.stringify(webmanifestJson, null, 4).replaceAll(`: "/`, `: "${ssrPath}`),
379
- 'utf8',
380
- );
381
- // build browserconfig
382
- fs.writeFileSync(
383
- `${buildPath}browserconfig.xml`,
384
- fs
385
- .readFileSync(`./src/client/public/${publicClientId}/browserconfig.xml`, 'utf8')
386
- .replaceAll(
387
- `<TileColor></TileColor>`,
388
- metadata.themeColor
389
- ? `<TileColor>${metadata.themeColor}</TileColor>`
390
- : `<TileColor>#e0e0e0</TileColor>`,
391
- )
392
- .replaceAll(`src="/`, `src="${ssrPath}`),
393
- 'utf8',
394
- );
395
-
396
- // Android play store example:
397
- //
398
- // "related_applications": [
399
- // {
400
- // "platform": "play",
401
- // "url": "https://play.google.com/store/apps/details?id=cheeaun.hackerweb"
402
- // }
403
- // ],
404
- // "prefer_related_applications": true
405
- }
406
- if (validPwaBuild) ssrHeadComponents += SrrComponent({ title, ssrPath, canonicalURL, ...metadata });
407
- break;
408
- case 'Seo':
409
- if (metadata) {
410
- ssrHeadComponents += SrrComponent({ title, ssrPath, canonicalURL, ...metadata });
411
- }
412
- break;
413
- case 'Microdata':
414
- if (
415
- fs.existsSync(`./src/client/public/${publicClientId}/microdata.json`) // &&
416
- // path === '/' &&
417
- // view.path === '/'
418
- ) {
419
- const microdata = JSON.parse(
420
- fs.readFileSync(`./src/client/public/${publicClientId}/microdata.json`, 'utf8'),
421
- );
422
- ssrHeadComponents += SrrComponent({ microdata });
423
- }
424
- break;
425
- default:
426
- ssrHeadComponents += SrrComponent({ ssrPath, host, path });
427
- break;
428
- }
429
- }
430
-
431
- for (const ssrBodyComponent of confSSR[view.ssr].body) {
432
- let SrrComponent;
433
- eval(
434
- await srcFormatted(
435
- fs.readFileSync(`./src/client/ssr/body-components/${ssrBodyComponent}.js`, 'utf8'),
436
- ),
437
- );
438
- switch (ssrBodyComponent) {
439
- case 'UnderpostDefaultSplashScreen':
440
- case 'CyberiaDefaultSplashScreen':
441
- case 'NexodevSplashScreen':
442
- case 'DefaultSplashScreen':
443
- if (backgroundImage) {
444
- ssrHeadComponents += SrrComponent({
445
- base64BackgroundImage: `data:image/${backgroundImage.split('.').pop()};base64,${fs
446
- .readFileSync(backgroundImage)
447
- .toString('base64')}`,
448
- });
449
- } else {
450
- const bufferBackgroundImage = await getBufferPngText({
451
- text: ' ',
452
- textColor: metadata?.themeColor ? metadata.themeColor : '#ececec',
453
- size: '100x100',
454
- bgColor: metadata?.themeColor ? metadata.themeColor : '#ececec',
455
- });
456
- ssrHeadComponents += SrrComponent({
457
- base64BackgroundImage: `data:image/png;base64,${bufferBackgroundImage.toString('base64')}`,
458
- });
459
- }
460
- break;
461
-
462
- default:
463
- ssrBodyComponents += SrrComponent({ ssrPath, host, path, ttiLoadTimeLimit });
464
- break;
465
- }
466
- }
467
- }
468
-
469
- let Render = () => '';
470
- eval(await srcFormatted(fs.readFileSync(`./src/client/ssr/Render.js`, 'utf8')));
471
-
472
- const htmlSrc = Render({
473
- title,
474
- buildId,
475
- ssrPath,
476
- ssrHeadComponents,
477
- ssrBodyComponents,
478
- });
479
-
480
- /** @type {import('sitemap').SitemapItem} */
481
- const siteMapLink = {
482
- url: `${path === '/' ? '' : path}${view.path}`,
483
- changefreq: 'daily',
484
- priority: 0.8,
485
- };
486
- siteMapLinks.push(siteMapLink);
487
-
488
- fs.writeFileSync(
489
- `${buildPath}index.html`,
490
- minifyBuild || process.env.NODE_ENV === 'production'
491
- ? await minify(htmlSrc, {
492
- minifyCSS: true,
493
- minifyJS: true,
494
- collapseBooleanAttributes: true,
495
- collapseInlineTagWhitespace: true,
496
- collapseWhitespace: true,
497
- })
498
- : htmlSrc,
499
- 'utf8',
500
- );
501
- }
502
- }
503
- if (!enableLiveRebuild && siteMapLinks.length > 0) {
504
- const xslUrl = fs.existsSync(`${rootClientPath}/sitemap`)
505
- ? `${path === '/' ? '' : path}/sitemap.xsl`
506
- : undefined;
507
- // Create a stream to write to
508
- /** @type {import('sitemap').SitemapStreamOptions} */
509
- const sitemapOptions = { hostname: `https://${host}`, xslUrl };
510
-
511
- const siteMapStream = new SitemapStream(sitemapOptions);
512
- let siteMapSrc = await new Promise((resolve) =>
513
- streamToPromise(Readable.from(siteMapLinks).pipe(siteMapStream)).then((data) => resolve(data.toString())),
514
- );
515
- switch (publicClientId) {
516
- case 'underpost':
517
- siteMapSrc = siteMapSrc.replaceAll(
518
- `</urlset>`,
519
- `${fs.readFileSync(`./src/client/public/underpost/sitemap-template.txt`, 'utf8')} </urlset>`,
520
- );
521
- break;
522
-
523
- default:
524
- break;
525
- }
526
- // Return a promise that resolves with your XML string
527
- fs.writeFileSync(`${rootClientPath}/sitemap.xml`, siteMapSrc, 'utf8');
528
- if (xslUrl)
529
- fs.writeFileSync(
530
- `${rootClientPath}/sitemap.xsl`,
531
- fs.readFileSync(`${rootClientPath}/sitemap`, 'utf8').replaceAll('{{web-url}}', `https://${host}${path}`),
532
- 'utf8',
533
- );
534
-
535
- fs.writeFileSync(
536
- `${rootClientPath}/robots.txt`,
537
- `User-agent: *
538
- Sitemap: https://${host}${path === '/' ? '' : path}/sitemap.xml`,
539
- 'utf8',
540
- );
541
- }
542
-
543
- if (!enableLiveRebuild && !process.argv.includes('l') && !process.argv.includes('deploy') && docsBuild) {
544
- // fullBuildEnabled || process.argv.includes('docs')
545
-
546
- // https://www.pullrequest.com/blog/leveraging-jsdoc-for-better-code-documentation-in-javascript/
547
- // https://jsdoc.app/about-configuring-jsdoc
548
- // https://jsdoc.app/ Block tags
549
-
550
- // "theme_opts": {
551
- // "default_theme": "dark" // "light", "fallback-dark", "fallback-light"
552
- // }
553
-
554
- const jsDocsConfig = JSON.parse(fs.readFileSync(`./jsdoc.json`, 'utf8'));
555
- jsDocsConfig.opts.destination = `./public/${host}${path === '/' ? path : `${path}/`}docs/`;
556
- jsDocsConfig.opts.theme_opts.title = metadata && metadata.title ? metadata.title : undefined;
557
- jsDocsConfig.opts.theme_opts.favicon = `./public/${host}${path === '/' ? path : `${path}/favicon.ico`}`;
558
- fs.writeFileSync(`./jsdoc.json`, JSON.stringify(jsDocsConfig, null, 4), 'utf8');
559
- logger.warn('build jsdoc view', jsDocsConfig.opts.destination);
560
- shellExec(`npm run docs`, { silent: true });
561
-
562
- // coverage
563
- if (!fs.existsSync(`./coverage`)) {
564
- shellExec(`npm test`);
565
- }
566
- const coverageBuildPath = `${jsDocsConfig.opts.destination}/coverage`;
567
- fs.mkdirSync(coverageBuildPath, { recursive: true });
568
- fs.copySync(`./coverage`, coverageBuildPath);
569
-
570
- // uml
571
- shellExec(`node bin/deploy uml ${host} ${path}`);
572
-
573
- // https://swagger-autogen.github.io/docs/
574
-
575
- const basePath = path === '/' ? `${process.env.BASE_API}` : `/${process.env.BASE_API}`;
576
-
577
- const doc = {
578
- info: {
579
- version: packageData.version, // by default: '1.0.0'
580
- title: metadata?.title ? `${metadata.title}` : 'REST API', // by default: 'REST API'
581
- description: metadata?.description ? metadata.description : '', // by default: ''
582
- },
583
- servers: [
584
- {
585
- url:
586
- process.env.NODE_ENV === 'development'
587
- ? `http://localhost:${port}${path}${basePath}`
588
- : `https://${host}${path}${basePath}`, // by default: 'http://localhost:3000'
589
- description: `${process.env.NODE_ENV} server`, // by default: ''
590
- },
591
- ],
592
- tags: [
593
- // by default: empty Array
594
- {
595
- name: 'user', // Tag name
596
- description: 'User API operations', // Tag description
597
- },
598
- ],
599
- components: {
600
- schemas: {
601
- userRequest: {
602
- username: 'user123',
603
- password: 'Password123',
604
- email: 'user@example.com',
605
- },
606
- userResponse: {
607
- status: 'success',
608
- data: {
609
- token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjY2YzM3N2Y1N2Y5OWU1OTY5YjgxZG...',
610
- user: {
611
- _id: '66c377f57f99e5969b81de89',
612
- email: 'user@example.com',
613
- emailConfirmed: false,
614
- username: 'user123',
615
- role: 'user',
616
- profileImageId: '66c377f57f99e5969b81de87',
617
- },
618
- },
619
- },
620
- userUpdateResponse: {
621
- status: 'success',
622
- data: {
623
- _id: '66c377f57f99e5969b81de89',
624
- email: 'user@example.com',
625
- emailConfirmed: false,
626
- username: 'user123222',
627
- role: 'user',
628
- profileImageId: '66c377f57f99e5969b81de87',
629
- },
630
- },
631
- userGetResponse: {
632
- status: 'success',
633
- data: {
634
- _id: '66c377f57f99e5969b81de89',
635
- email: 'user@example.com',
636
- emailConfirmed: false,
637
- username: 'user123222',
638
- role: 'user',
639
- profileImageId: '66c377f57f99e5969b81de87',
640
- },
641
- },
642
- userLogInRequest: {
643
- email: 'user@example.com',
644
- password: 'Password123',
645
- },
646
- userBadRequestResponse: {
647
- status: 'error',
648
- message: 'Bad request. Please check your inputs, and try again',
649
- },
650
- },
651
- securitySchemes: {
652
- bearerAuth: {
653
- type: 'http',
654
- scheme: 'bearer',
655
- },
656
- },
657
- },
658
- };
659
-
660
- logger.warn('build swagger api docs', doc.info);
661
-
662
- const outputFile = `./public/${host}${path === '/' ? path : `${path}/`}swagger-output.json`;
663
- const routes = [];
664
- for (const api of apis) {
665
- if (['user'].includes(api)) routes.push(`./src/api/${api}/${api}.router.js`);
666
- }
667
-
668
- /* NOTE: If you are using the express Router, you must pass in the 'routes' only the
669
- root file where the route starts, such as index.js, app.js, routes.js, etc ... */
670
-
671
- await swaggerAutoGen({ openapi: '3.0.0' })(outputFile, routes, doc);
672
- }
673
- if (!enableLiveRebuild && process.argv[2] === 'build-full-client-zip') {
674
- logger.warn('build zip', rootClientPath);
675
-
676
- if (!fs.existsSync('./build')) fs.mkdirSync('./build');
677
-
678
- const zip = new AdmZip();
679
- const files = await fs.readdir(rootClientPath, { recursive: true });
680
-
681
- for (const relativePath of files) {
682
- const filePath = dir.resolve(`${rootClientPath}/${relativePath}`);
683
- if (!fs.lstatSync(filePath).isDirectory()) {
684
- const folder = dir.relative(`public/${host}${path}`, dir.dirname(filePath));
685
- zip.addLocalFile(filePath, folder);
686
- }
687
- }
688
-
689
- const buildId = `${host}-${path.replaceAll('/', '')}`;
690
-
691
- logger.warn('write zip', `./build/${buildId}.zip`);
692
-
693
- zip.writeZip(`./build/${buildId}.zip`);
694
- }
695
- }
696
- }
697
- };
698
-
699
- export { buildClient };
1
+ 'use strict';
2
+
3
+ import fs from 'fs-extra';
4
+ import { srcFormatted, componentFormatted, viewFormatted } from './client-formatted.js';
5
+ import { loggerFactory } from './logger.js';
6
+ import { cap, newInstance, orderArrayFromAttrInt, titleFormatted } from '../client/components/core/CommonJs.js';
7
+ import UglifyJS from 'uglify-js';
8
+ import { minify } from 'html-minifier-terser';
9
+ import dotenv from 'dotenv';
10
+ import AdmZip from 'adm-zip';
11
+ import * as dir from 'path';
12
+ import { shellExec } from './process.js';
13
+ import swaggerAutoGen from 'swagger-autogen';
14
+ import { SitemapStream, streamToPromise } from 'sitemap';
15
+ import { Readable } from 'stream';
16
+ import { buildIcons, buildTextImg, getBufferPngText } from './client-icons.js';
17
+
18
+ dotenv.config();
19
+
20
+ // Static Site Generation (SSG)
21
+
22
+ const buildAcmeChallengePath = (acmeChallengeFullPath = '') => {
23
+ fs.mkdirSync(acmeChallengeFullPath, {
24
+ recursive: true,
25
+ });
26
+ fs.writeFileSync(`${acmeChallengeFullPath}/.gitkeep`, '', 'utf8');
27
+ };
28
+
29
+ const fullBuild = async ({
30
+ path,
31
+ logger,
32
+ client,
33
+ db,
34
+ dists,
35
+ rootClientPath,
36
+ acmeChallengeFullPath,
37
+ publicClientId,
38
+ iconsBuild,
39
+ metadata,
40
+ }) => {
41
+ logger.warn('Full build', rootClientPath);
42
+
43
+ fs.removeSync(rootClientPath);
44
+
45
+ buildAcmeChallengePath(acmeChallengeFullPath);
46
+
47
+ if (fs.existsSync(`./src/client/public/${publicClientId}`)) {
48
+ if (iconsBuild) {
49
+ const defaultBaseIconFolderPath = `src/client/public/${publicClientId}/assets/logo`;
50
+ if (!fs.existsSync(defaultBaseIconFolderPath)) fs.mkdirSync(defaultBaseIconFolderPath, { recursive: true });
51
+ const defaultBaseIconPath = `${defaultBaseIconFolderPath}/base-icon.png`;
52
+ if (!fs.existsSync(defaultBaseIconPath))
53
+ await buildTextImg(metadata.title, { debugFilename: defaultBaseIconPath });
54
+
55
+ if (path === '/' && !fs.existsSync(`./src/client/public/${publicClientId}/site.webmanifest`))
56
+ await buildIcons({ publicClientId, metadata });
57
+ }
58
+ fs.copySync(
59
+ `./src/client/public/${publicClientId}`,
60
+ rootClientPath /* {
61
+ filter: function (name) {
62
+ console.log(name);
63
+ return true;
64
+ },
65
+ } */,
66
+ );
67
+ } else if (fs.existsSync(`./engine-private/src/client/public/${publicClientId}`)) {
68
+ switch (publicClientId) {
69
+ case 'mysql_test':
70
+ if (db) {
71
+ fs.copySync(`./engine-private/src/client/public/${publicClientId}`, rootClientPath);
72
+ fs.writeFileSync(
73
+ `${rootClientPath}/index.php`,
74
+ fs
75
+ .readFileSync(`${rootClientPath}/index.php`, 'utf8')
76
+ .replace('test_servername', 'localhost')
77
+ .replace('test_username', db.user)
78
+ .replace('test_password', db.password)
79
+ .replace('test_dbname', db.name),
80
+ 'utf8',
81
+ );
82
+ } else logger.error('not provided db config');
83
+ break;
84
+
85
+ default:
86
+ break;
87
+ }
88
+ }
89
+ if (dists)
90
+ for (const dist of dists) {
91
+ if ('folder' in dist) {
92
+ fs.mkdirSync(`${rootClientPath}${dist.public_folder}`, { recursive: true });
93
+ fs.copySync(dist.folder, `${rootClientPath}${dist.public_folder}`);
94
+ }
95
+ if ('styles' in dist) {
96
+ fs.mkdirSync(`${rootClientPath}${dist.public_styles_folder}`, { recursive: true });
97
+ fs.copySync(dist.styles, `${rootClientPath}${dist.public_styles_folder}`);
98
+ }
99
+ }
100
+ };
101
+
102
+ const buildClient = async (options = { liveClientBuildPaths: [], instances: [] }) => {
103
+ const logger = loggerFactory(import.meta);
104
+ const confClient = JSON.parse(fs.readFileSync(`./conf/conf.client.json`, 'utf8'));
105
+ const confServer = JSON.parse(fs.readFileSync(`./conf/conf.server.json`, 'utf8'));
106
+ const confSSR = JSON.parse(fs.readFileSync(`./conf/conf.ssr.json`, 'utf8'));
107
+ const packageData = JSON.parse(fs.readFileSync(`./package.json`, 'utf8'));
108
+ const acmeChallengePath = `/.well-known/acme-challenge`;
109
+ const publicPath = `./public`;
110
+
111
+ // { srcBuildPath, publicBuildPath }
112
+ const enableLiveRebuild =
113
+ options && options.liveClientBuildPaths && options.liveClientBuildPaths.length > 0 ? true : false;
114
+
115
+ let currentPort = parseInt(process.env.PORT) + 1;
116
+ for (const host of Object.keys(confServer)) {
117
+ const paths = orderArrayFromAttrInt(Object.keys(confServer[host]), 'length', 'asc');
118
+ for (const path of paths) {
119
+ if (
120
+ options &&
121
+ options.instances &&
122
+ options.instances.length > 0 &&
123
+ !options.instances.find((i) => i.path === path && i.host === host)
124
+ )
125
+ continue;
126
+ const {
127
+ runtime,
128
+ client,
129
+ directory,
130
+ disabledRebuild,
131
+ minifyBuild,
132
+ db,
133
+ redirect,
134
+ apis,
135
+ iconsBuild,
136
+ docsBuild,
137
+ apiBaseProxyPath,
138
+ apiBaseHost,
139
+ ttiLoadTimeLimit,
140
+ singleReplica,
141
+ } = confServer[host][path];
142
+ if (singleReplica) continue;
143
+ if (!confClient[client]) confClient[client] = {};
144
+ const { components, dists, views, services, metadata, publicRef } = confClient[client];
145
+ let backgroundImage;
146
+ if (metadata) {
147
+ backgroundImage = metadata.backgroundImage;
148
+ if (metadata.thumbnail) metadata.thumbnail = `${path === '/' ? path : `${path}/`}${metadata.thumbnail}`;
149
+ }
150
+ const rootClientPath = directory ? directory : `${publicPath}/${host}${path}`;
151
+ const port = newInstance(currentPort);
152
+ const publicClientId = publicRef ? publicRef : client;
153
+ const fullBuildEnabled = !process.argv.includes('l') && !confServer[host][path].liteBuild && !enableLiveRebuild;
154
+ // const baseHost = process.env.NODE_ENV === 'production' ? `https://${host}` : `http://localhost:${port}`;
155
+ const baseHost = process.env.NODE_ENV === 'production' ? `https://${host}` : ``;
156
+ // ''; // process.env.NODE_ENV === 'production' ? `https://${host}` : ``;
157
+ currentPort++;
158
+
159
+ const acmeChallengeFullPath = directory
160
+ ? `${directory}${acmeChallengePath}`
161
+ : `${publicPath}/${host}${acmeChallengePath}`;
162
+
163
+ if (!enableLiveRebuild) buildAcmeChallengePath(acmeChallengeFullPath);
164
+
165
+ if (redirect || disabledRebuild) continue;
166
+
167
+ if (!enableLiveRebuild && runtime === 'lampp' && client === 'wordpress') {
168
+ shellExec(`node bin/install linux wordpress ${host}${path}`);
169
+ shellExec(`node bin/db ${host}${path} create`);
170
+ continue;
171
+ }
172
+
173
+ if (fullBuildEnabled) {
174
+ // !(confServer[host]['/'] && confServer[host]['/'].liteBuild)
175
+ await fullBuild({
176
+ path,
177
+ logger,
178
+ client,
179
+ db,
180
+ dists,
181
+ rootClientPath,
182
+ acmeChallengeFullPath,
183
+ publicClientId,
184
+ iconsBuild,
185
+ metadata,
186
+ });
187
+ if (apis)
188
+ for (const apiBuildScript of apis) {
189
+ const scriptPath = `src/api/${apiBuildScript}/${apiBuildScript}.build.js`;
190
+ if (fs.existsSync(`./${scriptPath}`)) {
191
+ shellExec(`node ${scriptPath}`);
192
+ }
193
+ }
194
+ }
195
+
196
+ if (components)
197
+ for (const module of Object.keys(components)) {
198
+ if (!fs.existsSync(`${rootClientPath}/components/${module}`))
199
+ fs.mkdirSync(`${rootClientPath}/components/${module}`, { recursive: true });
200
+
201
+ for (const component of components[module]) {
202
+ const jsSrcPath = `./src/client/components/${module}/${component}.js`;
203
+ const jsPublicPath = `${rootClientPath}/components/${module}/${component}.js`;
204
+
205
+ if (enableLiveRebuild && !options.liveClientBuildPaths.find((p) => p.srcBuildPath === jsSrcPath)) continue;
206
+
207
+ const jsSrc = componentFormatted(
208
+ await srcFormatted(fs.readFileSync(jsSrcPath, 'utf8')),
209
+ module,
210
+ dists,
211
+ path,
212
+ 'components',
213
+ baseHost,
214
+ );
215
+ fs.writeFileSync(
216
+ jsPublicPath,
217
+ minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
218
+ 'utf8',
219
+ );
220
+ }
221
+ }
222
+
223
+ if (services) {
224
+ for (const module of services) {
225
+ if (!fs.existsSync(`${rootClientPath}/services/${module}`))
226
+ fs.mkdirSync(`${rootClientPath}/services/${module}`, { recursive: true });
227
+
228
+ if (fs.existsSync(`./src/client/services/${module}/${module}.service.js`)) {
229
+ const jsSrcPath = `./src/client/services/${module}/${module}.service.js`;
230
+ const jsPublicPath = `${rootClientPath}/services/${module}/${module}.service.js`;
231
+ if (enableLiveRebuild && !options.liveClientBuildPaths.find((p) => p.srcBuildPath === jsSrcPath)) continue;
232
+
233
+ let jsSrc = componentFormatted(
234
+ await srcFormatted(fs.readFileSync(jsSrcPath, 'utf8')),
235
+ module,
236
+ dists,
237
+ path,
238
+ 'services',
239
+ baseHost,
240
+ );
241
+ if (module === 'core' && (process.env.NODE_ENV === 'production' || process.argv.includes('static'))) {
242
+ if (apiBaseHost)
243
+ jsSrc = jsSrc.replace(
244
+ 'const getBaseHost = () => location.host;',
245
+ `const getBaseHost = () => '${apiBaseHost}';`,
246
+ );
247
+ if (apiBaseProxyPath)
248
+ jsSrc = jsSrc.replace('${getProxyPath()}api/', `${apiBaseProxyPath}${process.env.BASE_API}/`);
249
+ }
250
+ fs.writeFileSync(
251
+ jsPublicPath,
252
+ minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
253
+ 'utf8',
254
+ );
255
+ }
256
+ }
257
+
258
+ for (const module of services) {
259
+ if (fs.existsSync(`./src/client/services/${module}/${module}.management.js`)) {
260
+ const jsSrcPath = `./src/client/services/${module}/${module}.management.js`;
261
+ const jsPublicPath = `${rootClientPath}/services/${module}/${module}.management.js`;
262
+ if (enableLiveRebuild && !options.liveClientBuildPaths.find((p) => p.srcBuildPath === jsSrcPath)) continue;
263
+
264
+ const jsSrc = componentFormatted(
265
+ await srcFormatted(fs.readFileSync(jsSrcPath, 'utf8')),
266
+ module,
267
+ dists,
268
+ path,
269
+ 'services',
270
+ baseHost,
271
+ );
272
+ fs.writeFileSync(
273
+ jsPublicPath,
274
+ minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
275
+ 'utf8',
276
+ );
277
+ }
278
+ }
279
+ }
280
+
281
+ const buildId = `${client}.index`;
282
+ const siteMapLinks = [];
283
+
284
+ if (views) {
285
+ // build service worker
286
+ if (path === '/') {
287
+ const jsSrcPath = fs.existsSync(`./src/client/sw/${publicClientId}.sw.js`)
288
+ ? `./src/client/sw/${publicClientId}.sw.js`
289
+ : `./src/client/sw/default.sw.js`;
290
+
291
+ const jsPublicPath = `${rootClientPath}/sw.js`;
292
+
293
+ if (!(enableLiveRebuild && !options.liveClientBuildPaths.find((p) => p.srcBuildPath === jsSrcPath))) {
294
+ const jsSrc = viewFormatted(await srcFormatted(fs.readFileSync(jsSrcPath, 'utf8')), dists, path, baseHost);
295
+
296
+ fs.writeFileSync(
297
+ jsPublicPath,
298
+ minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
299
+ 'utf8',
300
+ );
301
+ }
302
+ }
303
+ if (
304
+ !(
305
+ enableLiveRebuild &&
306
+ !options.liveClientBuildPaths.find(
307
+ (p) => p.srcBuildPath.startsWith(`./src/client/ssr`) || p.srcBuildPath.slice(-9) === '.index.js',
308
+ )
309
+ )
310
+ )
311
+ for (const view of views) {
312
+ const buildPath = `${
313
+ rootClientPath[rootClientPath.length - 1] === '/' ? rootClientPath.slice(0, -1) : rootClientPath
314
+ }${view.path === '/' ? view.path : `${view.path}/`}`;
315
+
316
+ if (!fs.existsSync(buildPath)) fs.mkdirSync(buildPath, { recursive: true });
317
+
318
+ logger.info('View build', buildPath);
319
+
320
+ const jsSrc = viewFormatted(
321
+ await srcFormatted(fs.readFileSync(`./src/client/${view.client}.index.js`, 'utf8')),
322
+ dists,
323
+ path,
324
+ baseHost,
325
+ );
326
+
327
+ fs.writeFileSync(
328
+ `${buildPath}${buildId}.js`,
329
+ minifyBuild || process.env.NODE_ENV === 'production' ? UglifyJS.minify(jsSrc).code : jsSrc,
330
+ 'utf8',
331
+ );
332
+
333
+ // const title = `${metadata && metadata.title ? metadata.title : cap(client)}${
334
+ // view.title ? ` | ${view.title}` : view.path !== '/' ? ` | ${titleFormatted(view.path)}` : ''
335
+ // }`;
336
+
337
+ const title = `${
338
+ view.title ? `${view.title} | ` : view.path !== '/' ? `${titleFormatted(view.path)} | ` : ''
339
+ }${metadata && metadata.title ? metadata.title : cap(client)}`;
340
+
341
+ const canonicalURL = `https://${host}${path}${
342
+ view.path === '/' ? (path === '/' ? '' : '/') : path === '/' ? `${view.path.slice(1)}/` : `${view.path}/`
343
+ }`;
344
+ const ssrPath = path === '/' ? path : `${path}/`;
345
+
346
+ let ssrHeadComponents = ``;
347
+ let ssrBodyComponents = ``;
348
+ if ('ssr' in view) {
349
+ // https://metatags.io/
350
+ if (process.env.NODE_ENV === 'production' && !confSSR[view.ssr].head.includes('Production'))
351
+ confSSR[view.ssr].head.unshift('Production');
352
+
353
+ for (const ssrHeadComponent of confSSR[view.ssr].head) {
354
+ let SrrComponent;
355
+ eval(
356
+ await srcFormatted(
357
+ fs.readFileSync(`./src/client/ssr/head-components/${ssrHeadComponent}.js`, 'utf8'),
358
+ ),
359
+ );
360
+
361
+ switch (ssrHeadComponent) {
362
+ case 'Pwa':
363
+ const validPwaBuild =
364
+ metadata &&
365
+ fs.existsSync(`./src/client/public/${publicClientId}/browserconfig.xml`) &&
366
+ fs.existsSync(`./src/client/public/${publicClientId}/site.webmanifest`);
367
+
368
+ if (view.path === '/' && validPwaBuild) {
369
+ // build webmanifest
370
+ const webmanifestJson = JSON.parse(
371
+ fs.readFileSync(`./src/client/public/${publicClientId}/site.webmanifest`, 'utf8'),
372
+ );
373
+ if (metadata.title) {
374
+ webmanifestJson.name = metadata.title;
375
+ webmanifestJson.short_name = metadata.title;
376
+ }
377
+ if (metadata.description) {
378
+ webmanifestJson.description = metadata.description;
379
+ }
380
+ if (metadata.themeColor) {
381
+ webmanifestJson.theme_color = metadata.themeColor;
382
+ webmanifestJson.background_color = metadata.themeColor;
383
+ }
384
+ fs.writeFileSync(
385
+ `${buildPath}site.webmanifest`,
386
+ JSON.stringify(webmanifestJson, null, 4).replaceAll(`: "/`, `: "${ssrPath}`),
387
+ 'utf8',
388
+ );
389
+ // build browserconfig
390
+ fs.writeFileSync(
391
+ `${buildPath}browserconfig.xml`,
392
+ fs
393
+ .readFileSync(`./src/client/public/${publicClientId}/browserconfig.xml`, 'utf8')
394
+ .replaceAll(
395
+ `<TileColor></TileColor>`,
396
+ metadata.themeColor
397
+ ? `<TileColor>${metadata.themeColor}</TileColor>`
398
+ : `<TileColor>#e0e0e0</TileColor>`,
399
+ )
400
+ .replaceAll(`src="/`, `src="${ssrPath}`),
401
+ 'utf8',
402
+ );
403
+
404
+ // Android play store example:
405
+ //
406
+ // "related_applications": [
407
+ // {
408
+ // "platform": "play",
409
+ // "url": "https://play.google.com/store/apps/details?id=cheeaun.hackerweb"
410
+ // }
411
+ // ],
412
+ // "prefer_related_applications": true
413
+ }
414
+ if (validPwaBuild) ssrHeadComponents += SrrComponent({ title, ssrPath, canonicalURL, ...metadata });
415
+ break;
416
+ case 'Seo':
417
+ if (metadata) {
418
+ ssrHeadComponents += SrrComponent({ title, ssrPath, canonicalURL, ...metadata });
419
+ }
420
+ break;
421
+ case 'Microdata':
422
+ if (
423
+ fs.existsSync(`./src/client/public/${publicClientId}/microdata.json`) // &&
424
+ // path === '/' &&
425
+ // view.path === '/'
426
+ ) {
427
+ const microdata = JSON.parse(
428
+ fs.readFileSync(`./src/client/public/${publicClientId}/microdata.json`, 'utf8'),
429
+ );
430
+ ssrHeadComponents += SrrComponent({ microdata });
431
+ }
432
+ break;
433
+ default:
434
+ ssrHeadComponents += SrrComponent({ ssrPath, host, path });
435
+ break;
436
+ }
437
+ }
438
+
439
+ for (const ssrBodyComponent of confSSR[view.ssr].body) {
440
+ let SrrComponent;
441
+ eval(
442
+ await srcFormatted(
443
+ fs.readFileSync(`./src/client/ssr/body-components/${ssrBodyComponent}.js`, 'utf8'),
444
+ ),
445
+ );
446
+ switch (ssrBodyComponent) {
447
+ case 'UnderpostDefaultSplashScreen':
448
+ case 'CyberiaDefaultSplashScreen':
449
+ case 'NexodevSplashScreen':
450
+ case 'DefaultSplashScreen':
451
+ if (backgroundImage) {
452
+ ssrHeadComponents += SrrComponent({
453
+ base64BackgroundImage: `data:image/${backgroundImage.split('.').pop()};base64,${fs
454
+ .readFileSync(backgroundImage)
455
+ .toString('base64')}`,
456
+ });
457
+ } else {
458
+ const bufferBackgroundImage = await getBufferPngText({
459
+ text: ' ',
460
+ textColor: metadata?.themeColor ? metadata.themeColor : '#ececec',
461
+ size: '100x100',
462
+ bgColor: metadata?.themeColor ? metadata.themeColor : '#ececec',
463
+ });
464
+ ssrHeadComponents += SrrComponent({
465
+ base64BackgroundImage: `data:image/png;base64,${bufferBackgroundImage.toString('base64')}`,
466
+ });
467
+ }
468
+ break;
469
+
470
+ default:
471
+ ssrBodyComponents += SrrComponent({ ssrPath, host, path, ttiLoadTimeLimit });
472
+ break;
473
+ }
474
+ }
475
+ }
476
+
477
+ let Render = () => '';
478
+ eval(await srcFormatted(fs.readFileSync(`./src/client/ssr/Render.js`, 'utf8')));
479
+
480
+ const htmlSrc = Render({
481
+ title,
482
+ buildId,
483
+ ssrPath,
484
+ ssrHeadComponents,
485
+ ssrBodyComponents,
486
+ });
487
+
488
+ /** @type {import('sitemap').SitemapItem} */
489
+ const siteMapLink = {
490
+ url: `${path === '/' ? '' : path}${view.path}`,
491
+ changefreq: 'daily',
492
+ priority: 0.8,
493
+ };
494
+ siteMapLinks.push(siteMapLink);
495
+
496
+ fs.writeFileSync(
497
+ `${buildPath}index.html`,
498
+ minifyBuild || process.env.NODE_ENV === 'production'
499
+ ? await minify(htmlSrc, {
500
+ minifyCSS: true,
501
+ minifyJS: true,
502
+ collapseBooleanAttributes: true,
503
+ collapseInlineTagWhitespace: true,
504
+ collapseWhitespace: true,
505
+ })
506
+ : htmlSrc,
507
+ 'utf8',
508
+ );
509
+ }
510
+ }
511
+ if (!enableLiveRebuild && siteMapLinks.length > 0) {
512
+ const xslUrl = fs.existsSync(`${rootClientPath}/sitemap`)
513
+ ? `${path === '/' ? '' : path}/sitemap.xsl`
514
+ : undefined;
515
+ // Create a stream to write to
516
+ /** @type {import('sitemap').SitemapStreamOptions} */
517
+ const sitemapOptions = { hostname: `https://${host}`, xslUrl };
518
+
519
+ const siteMapStream = new SitemapStream(sitemapOptions);
520
+ let siteMapSrc = await new Promise((resolve) =>
521
+ streamToPromise(Readable.from(siteMapLinks).pipe(siteMapStream)).then((data) => resolve(data.toString())),
522
+ );
523
+ switch (publicClientId) {
524
+ case 'underpost':
525
+ siteMapSrc = siteMapSrc.replaceAll(
526
+ `</urlset>`,
527
+ `${fs.readFileSync(`./src/client/public/underpost/sitemap-template.txt`, 'utf8')} </urlset>`,
528
+ );
529
+ break;
530
+
531
+ default:
532
+ break;
533
+ }
534
+ // Return a promise that resolves with your XML string
535
+ fs.writeFileSync(`${rootClientPath}/sitemap.xml`, siteMapSrc, 'utf8');
536
+ if (xslUrl)
537
+ fs.writeFileSync(
538
+ `${rootClientPath}/sitemap.xsl`,
539
+ fs.readFileSync(`${rootClientPath}/sitemap`, 'utf8').replaceAll('{{web-url}}', `https://${host}${path}`),
540
+ 'utf8',
541
+ );
542
+
543
+ fs.writeFileSync(
544
+ `${rootClientPath}/robots.txt`,
545
+ `User-agent: *
546
+ Sitemap: https://${host}${path === '/' ? '' : path}/sitemap.xml`,
547
+ 'utf8',
548
+ );
549
+ }
550
+
551
+ if (!enableLiveRebuild && !process.argv.includes('l') && !process.argv.includes('deploy') && docsBuild) {
552
+ // https://www.pullrequest.com/blog/leveraging-jsdoc-for-better-code-documentation-in-javascript/
553
+ // https://jsdoc.app/about-configuring-jsdoc
554
+ // https://jsdoc.app/ Block tags
555
+
556
+ const jsDocsConfig = JSON.parse(fs.readFileSync(`./jsdoc.json`, 'utf8'));
557
+ jsDocsConfig.opts.destination = `./public/${host}${path === '/' ? path : `${path}/`}docs/`;
558
+ jsDocsConfig.opts.theme_opts.title = metadata && metadata.title ? metadata.title : undefined;
559
+ jsDocsConfig.opts.theme_opts.favicon = `./public/${host}${path === '/' ? path : `${path}/favicon.ico`}`;
560
+ fs.writeFileSync(`./jsdoc.json`, JSON.stringify(jsDocsConfig, null, 4), 'utf8');
561
+ logger.warn('build jsdoc view', jsDocsConfig.opts.destination);
562
+ shellExec(`npm run docs`, { silent: true });
563
+
564
+ // coverage
565
+ if (!fs.existsSync(`./coverage`)) {
566
+ shellExec(`npm test`);
567
+ }
568
+ const coverageBuildPath = `${jsDocsConfig.opts.destination}/coverage`;
569
+ fs.mkdirSync(coverageBuildPath, { recursive: true });
570
+ fs.copySync(`./coverage`, coverageBuildPath);
571
+
572
+ // uml
573
+ shellExec(`node bin/deploy uml ${host} ${path}`);
574
+
575
+ // https://swagger-autogen.github.io/docs/
576
+
577
+ const basePath = path === '/' ? `${process.env.BASE_API}` : `/${process.env.BASE_API}`;
578
+
579
+ const doc = {
580
+ info: {
581
+ version: packageData.version, // by default: '1.0.0'
582
+ title: metadata?.title ? `${metadata.title}` : 'REST API', // by default: 'REST API'
583
+ description: metadata?.description ? metadata.description : '', // by default: ''
584
+ },
585
+ servers: [
586
+ {
587
+ url:
588
+ process.env.NODE_ENV === 'development'
589
+ ? `http://localhost:${port}${path}${basePath}`
590
+ : `https://${host}${path}${basePath}`, // by default: 'http://localhost:3000'
591
+ description: `${process.env.NODE_ENV} server`, // by default: ''
592
+ },
593
+ ],
594
+ tags: [
595
+ // by default: empty Array
596
+ {
597
+ name: 'user', // Tag name
598
+ description: 'User API operations', // Tag description
599
+ },
600
+ ],
601
+ components: {
602
+ schemas: {
603
+ userRequest: {
604
+ username: 'user123',
605
+ password: 'Password123',
606
+ email: 'user@example.com',
607
+ },
608
+ userResponse: {
609
+ status: 'success',
610
+ data: {
611
+ token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Il9pZCI6IjY2YzM3N2Y1N2Y5OWU1OTY5YjgxZG...',
612
+ user: {
613
+ _id: '66c377f57f99e5969b81de89',
614
+ email: 'user@example.com',
615
+ emailConfirmed: false,
616
+ username: 'user123',
617
+ role: 'user',
618
+ profileImageId: '66c377f57f99e5969b81de87',
619
+ },
620
+ },
621
+ },
622
+ userUpdateResponse: {
623
+ status: 'success',
624
+ data: {
625
+ _id: '66c377f57f99e5969b81de89',
626
+ email: 'user@example.com',
627
+ emailConfirmed: false,
628
+ username: 'user123222',
629
+ role: 'user',
630
+ profileImageId: '66c377f57f99e5969b81de87',
631
+ },
632
+ },
633
+ userGetResponse: {
634
+ status: 'success',
635
+ data: {
636
+ _id: '66c377f57f99e5969b81de89',
637
+ email: 'user@example.com',
638
+ emailConfirmed: false,
639
+ username: 'user123222',
640
+ role: 'user',
641
+ profileImageId: '66c377f57f99e5969b81de87',
642
+ },
643
+ },
644
+ userLogInRequest: {
645
+ email: 'user@example.com',
646
+ password: 'Password123',
647
+ },
648
+ userBadRequestResponse: {
649
+ status: 'error',
650
+ message: 'Bad request. Please check your inputs, and try again',
651
+ },
652
+ },
653
+ securitySchemes: {
654
+ bearerAuth: {
655
+ type: 'http',
656
+ scheme: 'bearer',
657
+ },
658
+ },
659
+ },
660
+ };
661
+
662
+ // plantuml
663
+ logger.info('copy plantuml', `${rootClientPath}/docs/plantuml`);
664
+ fs.copySync(`./src/client/public/default/plantuml`, `${rootClientPath}/docs/plantuml`);
665
+
666
+ logger.warn('build swagger api docs', doc.info);
667
+
668
+ const outputFile = `./public/${host}${path === '/' ? path : `${path}/`}swagger-output.json`;
669
+ const routes = [];
670
+ for (const api of apis) {
671
+ if (['user'].includes(api)) routes.push(`./src/api/${api}/${api}.router.js`);
672
+ }
673
+
674
+ /* NOTE: If you are using the express Router, you must pass in the 'routes' only the
675
+ root file where the route starts, such as index.js, app.js, routes.js, etc ... */
676
+
677
+ await swaggerAutoGen({ openapi: '3.0.0' })(outputFile, routes, doc);
678
+ }
679
+ if (!enableLiveRebuild && process.argv.includes('zip')) {
680
+ logger.warn('build zip', rootClientPath);
681
+
682
+ if (!fs.existsSync('./build')) fs.mkdirSync('./build');
683
+
684
+ const zip = new AdmZip();
685
+ const files = await fs.readdir(rootClientPath, { recursive: true });
686
+
687
+ for (const relativePath of files) {
688
+ const filePath = dir.resolve(`${rootClientPath}/${relativePath}`);
689
+ if (!fs.lstatSync(filePath).isDirectory()) {
690
+ const folder = dir.relative(`public/${host}${path}`, dir.dirname(filePath));
691
+ zip.addLocalFile(filePath, folder);
692
+ }
693
+ }
694
+
695
+ const buildId = `${host}-${path.replaceAll('/', '')}`;
696
+
697
+ logger.warn('write zip', `./build/${buildId}.zip`);
698
+
699
+ zip.writeZip(`./build/${buildId}.zip`);
700
+ }
701
+ }
702
+ }
703
+ };
704
+
705
+ export { buildClient };