ssb_metadata.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const fs = require('fs');
  2. const os = require('os');
  3. const path = require('path');
  4. const pkg = require('./package.json');
  5. const config = require('./ssb_config');
  6. const updater = require('../backend/updater.js');
  7. let printed = false;
  8. let checkedForUpdate = false;
  9. function getModules() {
  10. const nodeModulesPath = path.resolve(__dirname, 'node_modules');
  11. try {
  12. return fs.readdirSync(nodeModulesPath)
  13. .filter(m => fs.existsSync(path.join(nodeModulesPath, m, 'package.json')));
  14. } catch {
  15. return [];
  16. }
  17. }
  18. const colors = {
  19. blue: '\x1b[38;5;33m',
  20. yellow: '\x1b[38;5;226m',
  21. orange: '\x1b[38;5;214m',
  22. cyan: '\x1b[36m',
  23. reset: '\x1b[0m'
  24. };
  25. async function checkForUpdate() {
  26. if (checkedForUpdate) return;
  27. checkedForUpdate = true;
  28. const updateFlagPath = path.join(__dirname, '../server/.update_required');
  29. if (fs.existsSync(updateFlagPath)) {
  30. fs.unlinkSync(updateFlagPath);
  31. }
  32. await updater.getRemoteVersion();
  33. }
  34. async function printMetadata(mode, modeColor = colors.cyan) {
  35. if (printed) return;
  36. printed = true;
  37. const modules = getModules();
  38. const version = pkg.version;
  39. const name = pkg.name;
  40. const logLevel = config.logging?.level || 'info';
  41. const publicKey = config.keys?.public || '';
  42. console.log("=========================");
  43. console.log(`Running mode: ${modeColor}${mode}${colors.reset}`);
  44. console.log("=========================");
  45. console.log(`- Package: ${colors.blue}${name} ${colors.yellow}[Version: ${version}]${colors.reset}`);
  46. console.log("- Logging Level:", logLevel);
  47. console.log(`- Oasis ID: [ ${colors.orange}@${publicKey}${colors.reset} ]`);
  48. const ifaces = os.networkInterfaces();
  49. const isOnline = Object.values(ifaces).some(list =>
  50. list && list.some(i => !i.internal && i.family === 'IPv4')
  51. );
  52. console.log(`- Mode: ${isOnline ? 'online' : 'offline'}`);
  53. console.log("");
  54. console.log("=========================");
  55. console.log("Modules loaded: [", modules.length, "]");
  56. console.log("=========================");
  57. // Check for updates
  58. await checkForUpdate();
  59. console.log("=========================");
  60. }
  61. module.exports = {
  62. printMetadata,
  63. colors
  64. };