-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
191 lines (175 loc) · 7.78 KB
/
build.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
require('dotenv').config();
require('./modules/CoreConfig/src/CoreConfig').config().init(['AppVariables', 'CoreModules']);
const { execSync } = require('child_process');
const fs = require('fs-extra');
const path = require('path');
const { promisify } = require('util');
const writeFileAsync = promisify(fs.writeFile);
const readFileAsync = promisify(fs.readFile);
const crypto = require('crypto');
const ejs = require('ejs');
const { copyFilesAndMinify, createManifest, index, checkForIndex, buildExtensions, compileSCSS, compileJS, transferUncategorized } = require('./server.workers/server/building.files.js');
global.__PROJECT_DIR__ = path.join(__dirname, '.');
global.serverConfig = ini.parse('./server.ini');
const runArguments = process.argv.slice(2);
function generateToken(count, mode) {
const tokenization = mode === 'aes' ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*~≈×°₽„“”‘’£§‰≠±•«»:;,.<>?/|_\\-+.';
const key = Array.from({ length: count || 256 }, () => tokenization.charAt(Math.floor(Math.random() * tokenization.length))).join('');
const hash = crypto.createHash('sha256').update(key).digest('base64');
return `Random token = ${key}\nToken hash = ${hash} | ${hash.substring(0, 32)}\nRandom IV = ${crypto.randomBytes(16).toString('hex')}`;
}
function generateMuchTokens() {
let tokens = [];
for (let i = 0; i < 10; i++) {
tokens.push(generateToken(1024));
}
return tokens.join('\n\n');
}
const cmbScripts = {
'nginx-php-cgi-start': '@echo off\n' + 'cd /d C:\\nginx\n' + 'start nginx\n' + `php-cgi -b 127.0.0.1:9000 -c ${path.join(serverConfig.paths.root, 'tools', 'php.ini')}`,
'nginx-reload': '@echo off\n' + 'cd /d C:\\nginx\n' + 'nginx -s reload',
'nginx-kill': '@echo off\n' + 'cd /d C:\\nginx\n' + 'nginx -s stop',
'localtunnel-run': `lt --port ${serverConfig.server.HTTPPort} --subdomain ${serverConfig.server.localTunnel}`,
'lighthouse-analyzer-http': `lighthouse http://${serverConfig.server.host}:${serverConfig.server.HTTPPort} --output-path=./lighthouse_report.html`,
'lighthouse-analyzer-https': `lighthouse https://${serverConfig.server.host}:${serverConfig.server.HTTPSPort} --output-path=./lighthouse_report.html`,
'lighthouse-analyzer-nginx': `lighthouse https://${serverConfig.server.host}:${serverConfig.NGINX.HTTPSPort} --output-path=./lighthouse_report.html`,
'install-ltunnel-n-lhouse-via-npm-globally': 'npm install -g localtunnel lighthouse',
}
const phpIni =
'[PHP]\n' +
'engine = On\n' +
'short_open_tag = Off\n' +
'precision = 14\n' +
'output_buffering = 4096\n' +
'implicit_flush = Off\n' +
'serialize_precision = -1\n' +
'zend.enable_gc = On\n' +
'zend.exception_ignore_args = On\n' +
'zend.exception_string_param_max_len = 0\n' +
'expose_php = On\n' +
'max_execution_time = 30\n' +
'max_input_time = 60\n' +
'memory_limit = 128M\n' +
'display_errors = Off\n' +
'display_startup_errors = Off\n' +
'log_errors = Off\n' +
'post_max_size = 8M\n' +
'default_mimetype = "text/html"\n' +
'default_charset = "UTF-8"\n' +
'enable_dl = Off\n' +
'cgi.fix_pathinfo=1\n' +
'file_uploads = On\n' +
'upload_max_filesize = 2M\n' +
'max_file_uploads = 20\n' +
'default_socket_timeout = 60\n\n' +
`extension_dir = "${process.env.PHP}\\ext"\n` +
'extension=php_curl.dll\n' +
'extension=mysqli\n' +
'extension=php_openssl.dll\n\n' +
'[CLI Server]\n' +
'cli_server.color = On\n\n' +
'[MySQLi]\n' +
'mysqli.max_persistent = -1\n' +
'mysqli.allow_persistent = On\n' +
'mysqli.max_links = -1\n' +
`mysqli.default_port = ${serverConfig.dataBase.port}\n` +
'mysqli.default_socket =\n' +
'mysqli.default_host =\n' +
'mysqli.default_user =\n' +
'mysqli.default_pw =';
async function build() {
try {
const bashPromise = new Promise((resolve, reject) => {
try {
!fs.existsSync('./bin') && fs.mkdirSync('./bin', { recursive: true });
resolve();
} catch (err) {
console.log(err);
reject();
}
});
bashPromise.then(async () => {
try {
for (const [scriptName, scriptContent] of Object.entries(cmbScripts)) {
const filePath = path.join(__PROJECT_DIR__, 'bin', `${scriptName}.cmd`);
await writeFileAsync(filePath, scriptContent, 'utf-8');
}
} catch (err) {
console.log(err);
}
});
try {
await new Promise(async (resolve, reject) => {
try {
await transferUncategorized(path.join(__PROJECT_DIR__, 'src/serverside'), path.join(__PROJECT_DIR__, 'app'));
await compileJS(path.join(__PROJECT_DIR__, 'src/clientside/script'), path.join(__PROJECT_DIR__, 'static/public/script'));
await compileJS(path.join(__PROJECT_DIR__, 'src/serverside'), path.join(__PROJECT_DIR__, 'app'));
await compileSCSS(path.join(__PROJECT_DIR__, 'src/clientside/styles'), path.join(__PROJECT_DIR__, 'static/public/styles'));
console.log(`\x1b[32m[${new Date().toLocaleString().replace(',', '')}] :: 🟩 > [BUILDER] :: Files copied and minified successfully\x1b[39m`);
resolve();
} catch (err) {
console.error(`[${new Date().toLocaleString().replace(',', '')}] :: 🟥 > Error during copy and minify:`);
console.log(err);
reject();
}
});
await new Promise(async (resolve, reject) => {
try {
await buildExtensions(path.join(__PROJECT_DIR__, 'extensions'));
await buildExtensions(path.join(__PROJECT_DIR__, 'modules'));
resolve();
} catch (err) {
console.log(err);
reject(err);
}
});
} catch (err) {
console.log(err);
} finally {
try {
{
await require('./modules/GenerateConfigs/GenerateConfigs').init();
await writeFileAsync(path.join(__PROJECT_DIR__, 'Tools', `php.ini`), phpIni, 'utf-8');
}
{
const manifestOutput = path.join(__PROJECT_DIR__, 'assets/manifest/');
if (!fs.existsSync(manifestOutput)) fs.mkdirSync(manifestOutput, { recursive: true });
const { Manifest } = require('./app/templates/manifest_template.js');
const manifestTemplate = new Manifest();
const createManifestPromises = await serverConfig.language.supported.map(lang => createManifest(__PROJECT_DIR__, lang, manifestTemplate.getManifest()));
await Promise.all(createManifestPromises)
.then(() => console.log(`\x1b[32m[${new Date().toLocaleString().replace(',', '')}] :: 🟩 > [BUILDER] :: All manifests created successfully\x1b[39m`))
.catch(error => {
console.error(`[${new Date().toLocaleString().replace(',', '')}] :: 🟥 > Error during build:`);
console.error(err);
});
}
} catch (err) {
console.error(err);
}
}
await require('./server.workers/server/sitemap.gen.js').generateSiteMaps(__PROJECT_DIR__);
} catch (err) {
console.error(`[${new Date().toLocaleString().replace(',', '')}] :: 🟥 > Error during build:`);
console.error(err);
}
}
const BUILING_PROMISE = new Promise((resolve, reject) => {
try {
(async () => {
if (runArguments.includes('start')) await checkForIndex(__PROJECT_DIR__);
if (!runArguments.includes('index_rebuild')) await build();
if (runArguments.includes('index') || runArguments.includes('index_rebuild')) await index(__PROJECT_DIR__);
resolve();
})();
} catch (error) {
reject(error);
}
});
BUILING_PROMISE
.then(() => {
const exec = runArguments.includes('start') ? 'node' : (runArguments.includes('watch') ? 'nodemon' : null);
const index = runArguments.includes('watch') ? 'index.dev.js': 'index.js';
exec !== null && execSync(`${exec} ${index}`, { stdio: 'inherit' });
(!runArguments.includes('watch') && !runArguments.includes('start')) && process.exit();
});