forked from HeyPuter/puter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
353 lines (316 loc) · 14.7 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
Copyright (C) 2024 Puter Technologies Inc.
This file is part of Puter.com.
Puter.com is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { encode } from 'html-entities';
import fs from 'fs';
import path from 'path';
import webpack from 'webpack';
import CleanCSS from 'clean-css';
import uglifyjs from 'uglify-js';
import { lib_paths, css_paths, js_paths } from './src/static-assets.js';
import { fileURLToPath } from 'url';
// Polyfill __dirname, which doesn't exist in modules mode
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Builds the application by performing various tasks such as cleaning the distribution directory,
* merging and minifying JavaScript and CSS files, bundling GUI core files, handling images,
* and preparing the final distribution package. The process involves concatenating library
* scripts, optimizing them for production, and copying essential assets to the distribution
* directory. The function also supports a verbose mode for logging detailed information during
* the build process.
*
* @param {Object} [options] - Optional parameters to customize the build process.
* @param {boolean} [options.verbose=false] - Specifies whether to log detailed information during the build.
*
* @async
* @returns {Promise<void>} A promise that resolves when the build process is complete.
*
* @example
* build({ verbose: true }).then(() => {
* console.log('Build process completed successfully.');
* }).catch(error => {
* console.error('Build process failed:', error);
* });
*/
async function build(options){
// -----------------------------------------------
// Delete ./dist/ directory if it exists and create a new one
// -----------------------------------------------
if(fs.existsSync(path.join(__dirname, 'dist'))){
fs.rmSync(path.join(__dirname, 'dist'), {recursive: true});
}
fs.mkdirSync(path.join(__dirname, 'dist'));
// -----------------------------------------------
// Concat/merge the JS libraries and save them to ./dist/libs.js
// -----------------------------------------------
let js = '';
for(let i = 0; i < lib_paths.length; i++){
const file = path.join(__dirname, 'src', lib_paths[i]);
// js
if(file.endsWith('.js') && !file.endsWith('.min.js')){
let minified_code = await uglifyjs.minify(fs.readFileSync(file).toString(), {mangle: false});
if(minified_code && minified_code.code){
js += minified_code.code;
if(options?.verbose)
console.log('minified: ', file);
}
}else{
js += fs.readFileSync(file);
if(options?.verbose)
console.log('skipped minification: ', file);
}
js += '\n\n\n';
}
// -----------------------------------------------
// Combine all images into a single js file
// -----------------------------------------------
let icons = 'window.icons = [];\n\n\n';
fs.readdirSync(path.join(__dirname, 'src/icons')).forEach(file => {
// skip dotfiles
if(file.startsWith('.'))
return;
// load image
let buff = new Buffer.from(fs.readFileSync(path.join(__dirname, 'src/icons') + '/' + file));
// convert to base64
let base64data = buff.toString('base64');
// add to `window.icons`
if(file.endsWith('.png'))
icons += `window.icons['${file}'] = "data:image/png;base64,${base64data}";\n`;
else if(file.endsWith('.svg'))
icons += `window.icons['${file}'] = "data:image/svg+xml;base64,${base64data}";\n`;
});
// -----------------------------------------------
// concat/merge the CSS files and save them to ./dist/bundle.min.css
// -----------------------------------------------
let css = '';
for(let i = 0; i < css_paths.length; i++){
let fullpath = path.join(__dirname, 'src', css_paths[i]);
// minify CSS files if not already minified, then concatenate
if(css_paths[i].endsWith('.css') && !css_paths[i].endsWith('.min.css')){
const minified_css = new CleanCSS({}).minify(fs.readFileSync(fullpath).toString());
css += minified_css.styles;
}
// otherwise, just concatenate the file
else
css += fs.readFileSync(path.join(__dirname, 'src', css_paths[i]));
// add newlines between files
css += '\n\n\n';
}
fs.writeFileSync(path.join(__dirname, 'dist', 'bundle.min.css'), css);
// -----------------------------------------------
// Bundle GUI core and merge all files into a single JS file
// -----------------------------------------------
let main_array = [];
for(let i = 0; i < js_paths.length; i++){
main_array.push(path.join(__dirname, 'src', js_paths[i]));
}
webpack({
mode: 'production',
entry: {
main: main_array,
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},
optimization: {
minimize: true,
},
}, (err, stats) => {
if(err){
console.error(err);
return;
}
if(options?.verbose)
console.log(stats.toString());
// write to ./dist/bundle.min.js
fs.writeFileSync(path.join(__dirname, 'dist', 'bundle.min.js'), icons + '\n\n\n' + js + '\n\n\n' + fs.readFileSync(path.join(__dirname, 'dist', 'main.js')));
// remove ./dist/main.js
fs.unlinkSync(path.join(__dirname, 'dist', 'main.js'));
});
// Copy index.js to dist/gui.js
// Prepend `window.gui_env="prod";` to `./dist/gui.js`
fs.writeFileSync(
path.join(__dirname, 'dist', 'gui.js'),
`window.gui_env="prod"; \n\n` + fs.readFileSync(path.join(__dirname, 'src', 'index.js'))
);
const copy_these = [
'images',
'favicons',
'browserconfig.xml',
'manifest.json',
'favicon.ico',
'security.txt',
];
const recursive_copy = (src, dest) => {
const stat = fs.statSync(src);
if ( stat.isDirectory() ) {
if( ! fs.existsSync(dest) ) fs.mkdirSync(dest);
const files = fs.readdirSync(src);
for ( const file of files ) {
recursive_copy(path.join(src, file), path.join(dest, file));
}
} else {
fs.copyFileSync(src, dest);
}
};
for ( const to_copy of copy_these ) {
recursive_copy(path.join(__dirname, 'src', to_copy), path.join(__dirname, 'dist', to_copy));
}
}
/**
* Generates the HTML content for the GUI based on the specified configuration options. The function
* creates a new HTML document with the specified title, description, and other metadata. The function
* also includes the necessary CSS and JavaScript files, as well as the required meta tags for social
* media sharing and search engine optimization. The function is designed to be used in development
* environments to generate the HTML content for the GUI.
*
* @param {Object} options - The configuration options for the GUI.
* @param {string} options.env - The environment in which the GUI is running (e.g., "dev" or "prod").
* @param {string} options.api_origin - The origin of the API server.
* @param {string} options.title - The title of the GUI.
* @param {string} options.company - The name of the company or organization.
* @param {string} options.description - The description of the GUI.
* @param {string} options.app_description - The description of the application.
* @param {string} options.short_description - The short description of the GUI.
* @param {string} options.origin - The origin of the GUI.
* @param {string} options.social_card - The URL of the social media card image.
* @returns {string} The HTML content for the GUI based on the specified configuration options.
*
*/
function generateDevHtml(options){
let start_t = Date.now();
// final html string
let h = '';
h += `<!DOCTYPE html>`;
h += `<html lang="en">`;
h += `<head>`;
h += `<title>${encode((options.title))}</title>`
h += `<meta name="author" content="${encode(options.company)}">`
// description
let description = options.description;
// if app_description is set, use that instead
if(options.app_description){
description = options.app_description;
}
// if no app_description is set, use short_description if set
else if(options.short_description){
description = options.short_description;
}
// description
h += `<meta name="description" content="${encode((description).replace(/\n/g, " "))}">`
// facebook domain verification
h += `<meta name="facebook-domain-verification" content="e29w3hjbnnnypf4kzk2cewcdaxym1y" />`;
// canonical url
h += `<link rel="canonical" href="${options.origin}">`;
// DEV: load every CSS file individually
if(options.env === 'dev'){
for(let i = 0; i < css_paths.length; i++){
h += `<link rel="stylesheet" href="${css_paths[i]}">`;
}
}
// Facebook meta tags
h += `<meta property="og:url" content="https://${options.domain}">`;
h += `<meta property="og:type" content="website">`;
h += `<meta property="og:title" content="${encode(options.title)}">`;
h += `<meta property="og:description" content="${encode((options.short_description).replace(/\n/g, " "))}">`;
h += `<meta property="og:image" content="${options.social_card}">`;
// Twitter meta tags
h += `<meta name="twitter:card" content="summary_large_image">`;
h += `<meta property="twitter:domain" content="${options.domain}">`;
h += `<meta property="twitter:url" content="https://${options.domain}">`;
h += `<meta name="twitter:title" content="${encode(options.title)}">`;
h += `<meta name="twitter:description" content="${encode((options.short_description).replace(/\n/g, " "))}">`;
h += `<meta name="twitter:image" content="${options.social_card}">`;
// favicons
h += `
<link rel="apple-touch-icon" sizes="57x57" href="/favicons/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/favicons/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/favicons/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/favicons/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/favicons/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/favicons/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="/favicons/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/favicons/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/favicons/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="/favicons/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="/favicons/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicons/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/favicons/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">`;
// preload images when applicable
h += `<link rel="preload" as="image" href="./images/wallpaper.webp">`;
h += `</head>`;
h += `<body>`;
// To indicate that the GUI is running to any 3rd-party scripts that may be running on the page
// specifically, the `puter.js` script
// This line is also present verbatim in `src/index.js` for production builds
h += `<script>window.puter_gui_enabled = true;</script>`;
// DEV: load every JS library individually
if(options.env === 'dev'){
for(let i = 0; i < lib_paths.length; i++){
h += `<script src="${lib_paths[i]}"></script>`;
}
}
// load images and icons as base64 for performance
if(options.env === 'dev'){
h += `<script>`;
h += `window.icons = {};`
fs.readdirSync(path.join(__dirname, 'src/icons')).forEach(file => {
// skip dotfiles
if(file.startsWith('.'))
return;
// load image
let buff = new Buffer.from(fs.readFileSync(path.join(__dirname, 'src/icons') + '/' + file));
// convert to base64
let base64data = buff.toString('base64');
// add to `window.icons`
if(file.endsWith('.png'))
h += `window.icons['${file}'] = "data:image/png;base64,${base64data}";\n`;
else if(file.endsWith('.svg'))
h += `window.icons['${file}'] = "data:image/svg+xml;base64,${base64data}";\n`;
});
h += `</script>`;
}
// PROD: gui.js
if(options.env === 'prod'){
h += `<script src="/dist/gui.js"></script>`;
}
// DEV: load every JS file individually
else{
for(let i = 0; i < js_paths.length; i++){
h += `<script type="module" src="${js_paths[i]}"></script>`;
}
// load GUI
h += `<script type="module" src="/index.js"></script>`;
}
// ----------------------------------------
// Initialize GUI with config options
// ----------------------------------------
h += `
<script type="text/javascript">
window.addEventListener('load', function() {`
h += `gui()`;
h += `});
</script>`;
h += `</body>
</html>`;
console.log(`/index.js: ` + (Date.now() - start_t)/1000);
return h;
}
// export
export { generateDevHtml, build };