1
0

run-selfhosted.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // surrounding_box function
  2. //
  3. // It's really hard to see an error message without using
  4. // the surrounding_box function to highlight its location.
  5. // The implementation of this in packages/backend might not
  6. // work in older versions of node, so we instead re-implement
  7. // it here.
  8. import console from 'node:console';
  9. import process from 'node:process';
  10. const surrounding_box = (col, lines) => {
  11. const lengths = lines.map(line => line.length);
  12. const max_length = Math.max(...lengths);
  13. const c = str => `\x1b[${col}m${str}\x1b[0m`;
  14. const bar = c(Array(max_length + 4).fill('━').join(''));
  15. for ( let i = 0 ; i < lines.length ; i++ ) {
  16. while ( lines[i].length < max_length ) {
  17. lines[i] += ' ';
  18. }
  19. lines[i] = `${c('┃ ')} ${lines[i]} ${c(' ┃')}`;
  20. }
  21. lines.unshift(`${c('┏')}${bar}${c('┓')}`);
  22. lines.push(`${c('┗')}${bar}${c('┛')}`);
  23. };
  24. // node version check
  25. {
  26. // Keeping track of WHY certain versions don't work
  27. const ver_info = [
  28. { under: 14, reasons: ['optional chaining is not available'] },
  29. { under: 16, reasons: ['disk usage package ABI mismatch'] },
  30. ];
  31. const lowest_allowed = Math.max(...ver_info.map(r => r.under));
  32. // ACTUAL VERSION CHECK
  33. const [major, minor] = process.versions.node.split('.').map(Number);
  34. if ( major < lowest_allowed ) {
  35. const lines = [];
  36. lines.push(`Please use a version of Node.js ${lowest_allowed} or newer.`);
  37. lines.push(`Issues with node ${process.versions.node}:`);
  38. // We also show the user the reasons in case they want to know
  39. for ( const { under, reasons } of ver_info ) {
  40. if ( major < under ) {
  41. lines.push(` - ${reasons.join(', ')}`);
  42. }
  43. }
  44. surrounding_box('31;1', lines);
  45. console.error(lines.join('\n'));
  46. process.exit(1);
  47. }
  48. }
  49. const main = async () => {
  50. const {
  51. Kernel,
  52. CoreModule,
  53. DatabaseModule,
  54. PuterDriversModule,
  55. LocalDiskStorageModule,
  56. SelfHostedModule
  57. } = (await import('@heyputer/backend')).default;
  58. console.log('kerne', Kernel);
  59. const k = new Kernel();
  60. k.add_module(new CoreModule());
  61. k.add_module(new DatabaseModule());
  62. k.add_module(new PuterDriversModule());
  63. k.add_module(new LocalDiskStorageModule());
  64. k.add_module(new SelfHostedModule());
  65. k.boot();
  66. };
  67. const early_init_errors = [
  68. {
  69. text: `Cannot find package '@heyputer/backend'`,
  70. notes: [
  71. 'this usually happens if you forget `npm install`'
  72. ],
  73. suggestions: [
  74. 'try running `npm install`'
  75. ],
  76. technical_notes: [
  77. '@heyputer/backend is in an npm workspace'
  78. ]
  79. },
  80. {
  81. text: `Cannot find package`,
  82. notes: [
  83. 'this usually happens if you forget `npm install`'
  84. ],
  85. suggestions: [
  86. 'try running `npm install`'
  87. ],
  88. }
  89. ];
  90. // null coalescing operator
  91. const nco = (...args) => {
  92. for ( const arg of args ) {
  93. if ( arg !== undefined && arg !== null ) {
  94. return arg;
  95. }
  96. }
  97. return undefined;
  98. }
  99. const _print_error_help = (error_help) => {
  100. const lines = [];
  101. lines.push(nco(error_help.title, error_help.text));
  102. for ( const note of (nco(error_help.notes, [])) ) {
  103. lines.push(`📝 ${note}`)
  104. }
  105. if ( error_help.suggestions ) {
  106. lines.push('Suggestions:');
  107. for ( const suggestion of error_help.suggestions ) {
  108. lines.push(`- ${suggestion}`);
  109. }
  110. }
  111. if ( error_help.technical_notes ) {
  112. lines.push('Technical Notes:');
  113. for ( const note of error_help.technical_notes ) {
  114. lines.push(`- ${note}`);
  115. }
  116. }
  117. surrounding_box('31;1', lines);
  118. console.error(lines.join('\n'));
  119. }
  120. (async () => {
  121. try {
  122. await main();
  123. } catch (e) {
  124. for ( const error_help of early_init_errors ) {
  125. const message = e && e.message;
  126. if ( e.message && e.message.includes(error_help.text) ) {
  127. _print_error_help(error_help);
  128. break;
  129. }
  130. }
  131. throw e;
  132. }
  133. })();