init.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. 'use strict'
  2. const {Buffer} = require('buffer')
  3. const fs = require('fs')
  4. const path = require('path')
  5. const util = require('util')
  6. const Module = require('module')
  7. const v8 = require('v8')
  8. // We modified the original process.argv to let node.js load the atom.js,
  9. // we need to restore it here.
  10. process.argv.splice(1, 1)
  11. // Clear search paths.
  12. require('../common/reset-search-paths')
  13. // Import common settings.
  14. require('../common/init')
  15. var globalPaths = Module.globalPaths
  16. // Expose public APIs.
  17. globalPaths.push(path.join(__dirname, 'api', 'exports'))
  18. if (process.platform === 'win32') {
  19. // Redirect node's console to use our own implementations, since node can not
  20. // handle console output when running as GUI program.
  21. var consoleLog = function (...args) {
  22. return process.log(util.format(...args) + '\n')
  23. }
  24. var streamWrite = function (chunk, encoding, callback) {
  25. if (Buffer.isBuffer(chunk)) {
  26. chunk = chunk.toString(encoding)
  27. }
  28. process.log(chunk)
  29. if (callback) {
  30. callback()
  31. }
  32. return true
  33. }
  34. console.log = console.error = console.warn = consoleLog
  35. process.stdout.write = process.stderr.write = streamWrite
  36. }
  37. // Don't quit on fatal error.
  38. process.on('uncaughtException', function (error) {
  39. // Do nothing if the user has a custom uncaught exception handler.
  40. var dialog, message, ref, stack
  41. if (process.listeners('uncaughtException').length > 1) {
  42. return
  43. }
  44. // Show error in GUI.
  45. dialog = require('electron').dialog
  46. stack = (ref = error.stack) != null ? ref : error.name + ': ' + error.message
  47. message = 'Uncaught Exception:\n' + stack
  48. dialog.showErrorBox('A JavaScript error occurred in the main process', message)
  49. })
  50. // Emit 'exit' event on quit.
  51. const {app} = require('electron')
  52. app.on('quit', function (event, exitCode) {
  53. process.emit('exit', exitCode)
  54. })
  55. if (process.platform === 'win32') {
  56. // If we are a Squirrel.Windows-installed app, set app user model ID
  57. // so that users don't have to do this.
  58. //
  59. // Squirrel packages are always of the form:
  60. //
  61. // PACKAGE-NAME
  62. // - Update.exe
  63. // - app-VERSION
  64. // - OUREXE.exe
  65. //
  66. // Squirrel itself will always set the shortcut's App User Model ID to the
  67. // form `com.squirrel.PACKAGE-NAME.OUREXE`. We need to call
  68. // app.setAppUserModelId with a matching identifier so that renderer processes
  69. // will inherit this value.
  70. const updateDotExe = path.join(path.dirname(process.execPath), '..', 'update.exe')
  71. if (fs.existsSync(updateDotExe)) {
  72. const packageDir = path.dirname(path.resolve(updateDotExe))
  73. const packageName = path.basename(packageDir).replace(/\s/g, '')
  74. const exeName = path.basename(process.execPath).replace(/\.exe$/i, '').replace(/\s/g, '')
  75. app.setAppUserModelId(`com.squirrel.${packageName}.${exeName}`)
  76. }
  77. }
  78. // Map process.exit to app.exit, which quits gracefully.
  79. process.exit = app.exit
  80. // Load the RPC server.
  81. require('./rpc-server')
  82. // Load the guest view manager.
  83. require('./guest-view-manager')
  84. require('./guest-window-manager')
  85. // Now we try to load app's package.json.
  86. let packagePath = null
  87. let packageJson = null
  88. const searchPaths = ['app', 'app.asar', 'default_app.asar']
  89. for (packagePath of searchPaths) {
  90. try {
  91. packagePath = path.join(process.resourcesPath, packagePath)
  92. packageJson = require(path.join(packagePath, 'package.json'))
  93. break
  94. } catch (error) {
  95. continue
  96. }
  97. }
  98. if (packageJson == null) {
  99. process.nextTick(function () {
  100. return process.exit(1)
  101. })
  102. throw new Error('Unable to find a valid app')
  103. }
  104. // Set application's version.
  105. if (packageJson.version != null) {
  106. app.setVersion(packageJson.version)
  107. }
  108. // Set application's name.
  109. if (packageJson.productName != null) {
  110. app.setName(packageJson.productName)
  111. } else if (packageJson.name != null) {
  112. app.setName(packageJson.name)
  113. }
  114. // Set application's desktop name.
  115. if (packageJson.desktopName != null) {
  116. app.setDesktopName(packageJson.desktopName)
  117. } else {
  118. app.setDesktopName((app.getName()) + '.desktop')
  119. }
  120. // Set v8 flags
  121. if (packageJson.v8Flags != null) {
  122. v8.setFlagsFromString(packageJson.v8Flags)
  123. }
  124. // Set the user path according to application's name.
  125. app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
  126. app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
  127. app.setAppPath(packagePath)
  128. // Load the chrome extension support.
  129. require('./chrome-extension')
  130. // Load internal desktop-capturer module.
  131. require('./desktop-capturer')
  132. // Load protocol module to ensure it is populated on app ready
  133. require('./api/protocol')
  134. // Set main startup script of the app.
  135. const mainStartupScript = packageJson.main || 'index.js'
  136. const KNOWN_XDG_DESKTOP_VALUES = ['Pantheon', 'Unity:Unity7', 'pop:GNOME']
  137. function currentPlatformSupportsAppIndicator () {
  138. if (process.platform !== 'linux') return false
  139. const currentDesktop = process.env.XDG_CURRENT_DESKTOP
  140. if (!currentDesktop) return false
  141. if (KNOWN_XDG_DESKTOP_VALUES.includes(currentDesktop)) return true
  142. // ubuntu based or derived session (default ubuntu one, communitheme…) supports
  143. // indicator too.
  144. if (/ubuntu/ig.test(currentDesktop)) return true
  145. return false
  146. }
  147. // Workaround for electron/electron#5050 and electron/electron#9046
  148. if (currentPlatformSupportsAppIndicator()) {
  149. process.env.XDG_CURRENT_DESKTOP = 'Unity'
  150. }
  151. // Finally load app's main.js and transfer control to C++.
  152. Module._load(path.join(packagePath, mainStartupScript), Module, true)