2024-07-09 08:10:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2024 Puter Technologies Inc.
|
|
|
|
*
|
|
|
|
* This file is part of Puter.
|
|
|
|
*
|
|
|
|
* Puter 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/>.
|
|
|
|
*/
|
2024-04-05 18:09:34 +00:00
|
|
|
// surrounding_box function
|
|
|
|
//
|
|
|
|
// It's really hard to see an error message without using
|
|
|
|
// the surrounding_box function to highlight its location.
|
|
|
|
// The implementation of this in packages/backend might not
|
|
|
|
// work in older versions of node, so we instead re-implement
|
|
|
|
// it here.
|
2024-05-01 13:30:03 +00:00
|
|
|
import console from 'node:console';
|
2024-05-01 10:39:01 +00:00
|
|
|
import process from 'node:process';
|
|
|
|
|
2024-04-05 18:09:34 +00:00
|
|
|
const surrounding_box = (col, lines) => {
|
|
|
|
const lengths = lines.map(line => line.length);
|
2024-03-30 23:08:03 +00:00
|
|
|
|
2024-04-05 18:09:34 +00:00
|
|
|
const max_length = Math.max(...lengths);
|
|
|
|
const c = str => `\x1b[${col}m${str}\x1b[0m`;
|
|
|
|
const bar = c(Array(max_length + 4).fill('━').join(''));
|
|
|
|
for ( let i = 0 ; i < lines.length ; i++ ) {
|
|
|
|
while ( lines[i].length < max_length ) {
|
|
|
|
lines[i] += ' ';
|
2024-04-05 01:39:32 +00:00
|
|
|
}
|
2024-04-05 18:09:34 +00:00
|
|
|
lines[i] = `${c('┃ ')} ${lines[i]} ${c(' ┃')}`;
|
|
|
|
}
|
|
|
|
lines.unshift(`${c('┏')}${bar}${c('┓')}`);
|
|
|
|
lines.push(`${c('┗')}${bar}${c('┛')}`);
|
|
|
|
};
|
2024-04-05 01:39:32 +00:00
|
|
|
|
2024-04-05 18:09:34 +00:00
|
|
|
// node version check
|
|
|
|
{
|
2024-04-05 01:39:32 +00:00
|
|
|
// Keeping track of WHY certain versions don't work
|
|
|
|
const ver_info = [
|
|
|
|
{ under: 14, reasons: ['optional chaining is not available'] },
|
2024-05-05 07:54:11 +00:00
|
|
|
{ under: 16, reasons: ['disk usage package ABI mismatch'] },
|
2024-04-05 01:39:32 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const lowest_allowed = Math.max(...ver_info.map(r => r.under));
|
|
|
|
|
|
|
|
// ACTUAL VERSION CHECK
|
|
|
|
const [major, minor] = process.versions.node.split('.').map(Number);
|
|
|
|
if ( major < lowest_allowed ) {
|
|
|
|
const lines = [];
|
|
|
|
lines.push(`Please use a version of Node.js ${lowest_allowed} or newer.`);
|
|
|
|
lines.push(`Issues with node ${process.versions.node}:`);
|
|
|
|
// We also show the user the reasons in case they want to know
|
|
|
|
for ( const { under, reasons } of ver_info ) {
|
|
|
|
if ( major < under ) {
|
|
|
|
lines.push(` - ${reasons.join(', ')}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
surrounding_box('31;1', lines);
|
|
|
|
console.error(lines.join('\n'));
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 04:41:02 +00:00
|
|
|
// Annoying polyfill for inconsistency in different node versions
|
|
|
|
if ( ! import.meta.filename ) {
|
|
|
|
Object.defineProperty(import.meta, 'filename', {
|
2024-06-18 12:06:00 +00:00
|
|
|
get: () => import.meta.url.slice('file://'.length),
|
2024-06-13 04:41:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-05 18:09:34 +00:00
|
|
|
const main = async () => {
|
2024-04-05 01:39:32 +00:00
|
|
|
const {
|
|
|
|
Kernel,
|
|
|
|
CoreModule,
|
|
|
|
DatabaseModule,
|
|
|
|
LocalDiskStorageModule,
|
2024-07-29 19:05:55 +00:00
|
|
|
SelfHostedModule,
|
|
|
|
TestDriversModule,
|
2024-08-01 03:37:17 +00:00
|
|
|
PuterAIModule,
|
2024-04-05 01:39:32 +00:00
|
|
|
} = (await import('@heyputer/backend')).default;
|
|
|
|
|
2024-06-11 17:57:03 +00:00
|
|
|
const k = new Kernel({
|
|
|
|
entry_path: import.meta.filename
|
|
|
|
});
|
2024-04-05 01:39:32 +00:00
|
|
|
k.add_module(new CoreModule());
|
|
|
|
k.add_module(new DatabaseModule());
|
|
|
|
k.add_module(new LocalDiskStorageModule());
|
2024-05-05 07:53:55 +00:00
|
|
|
k.add_module(new SelfHostedModule());
|
2024-07-29 19:05:55 +00:00
|
|
|
k.add_module(new TestDriversModule());
|
2024-08-01 03:37:17 +00:00
|
|
|
k.add_module(new PuterAIModule());
|
2024-04-05 01:39:32 +00:00
|
|
|
k.boot();
|
2024-04-05 18:09:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const early_init_errors = [
|
|
|
|
{
|
|
|
|
text: `Cannot find package '@heyputer/backend'`,
|
|
|
|
notes: [
|
|
|
|
'this usually happens if you forget `npm install`'
|
|
|
|
],
|
|
|
|
suggestions: [
|
|
|
|
'try running `npm install`'
|
|
|
|
],
|
|
|
|
technical_notes: [
|
|
|
|
'@heyputer/backend is in an npm workspace'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: `Cannot find package`,
|
|
|
|
notes: [
|
|
|
|
'this usually happens if you forget `npm install`'
|
|
|
|
],
|
|
|
|
suggestions: [
|
|
|
|
'try running `npm install`'
|
|
|
|
],
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2024-04-29 06:40:39 +00:00
|
|
|
// null coalescing operator
|
|
|
|
const nco = (...args) => {
|
|
|
|
for ( const arg of args ) {
|
|
|
|
if ( arg !== undefined && arg !== null ) {
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-04-05 18:09:34 +00:00
|
|
|
const _print_error_help = (error_help) => {
|
|
|
|
const lines = [];
|
2024-04-08 21:05:47 +00:00
|
|
|
lines.push(nco(error_help.title, error_help.text));
|
|
|
|
for ( const note of (nco(error_help.notes, [])) ) {
|
2024-04-05 18:09:34 +00:00
|
|
|
lines.push(`📝 ${note}`)
|
|
|
|
}
|
|
|
|
if ( error_help.suggestions ) {
|
|
|
|
lines.push('Suggestions:');
|
|
|
|
for ( const suggestion of error_help.suggestions ) {
|
|
|
|
lines.push(`- ${suggestion}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( error_help.technical_notes ) {
|
|
|
|
lines.push('Technical Notes:');
|
|
|
|
for ( const note of error_help.technical_notes ) {
|
|
|
|
lines.push(`- ${note}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
surrounding_box('31;1', lines);
|
|
|
|
console.error(lines.join('\n'));
|
|
|
|
}
|
2024-03-30 23:08:03 +00:00
|
|
|
|
2024-04-05 18:09:34 +00:00
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
await main();
|
|
|
|
} catch (e) {
|
|
|
|
for ( const error_help of early_init_errors ) {
|
2024-04-23 08:06:03 +00:00
|
|
|
const message = e && e.message;
|
|
|
|
if ( e.message && e.message.includes(error_help.text) ) {
|
2024-04-05 18:09:34 +00:00
|
|
|
_print_error_help(error_help);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
})();
|