/static/index.js

https://gitlab.com/ananthurv/atom · JavaScript · 186 lines · 151 code · 28 blank · 7 comment · 17 complexity · 48cce2a85b6878ffc76dcb3a29769104 MD5 · raw file

  1. (function () {
  2. var fs = require('fs')
  3. var path = require('path')
  4. var loadSettings = null
  5. var loadSettingsError = null
  6. window.onload = function () {
  7. try {
  8. var startTime = Date.now()
  9. process.on('unhandledRejection', function (error, promise) {
  10. console.error('Unhandled promise rejection %o with error: %o', promise, error)
  11. })
  12. // Ensure ATOM_HOME is always set before anything else is required
  13. setupAtomHome()
  14. // Normalize to make sure drive letter case is consistent on Windows
  15. process.resourcesPath = path.normalize(process.resourcesPath)
  16. if (loadSettingsError) {
  17. throw loadSettingsError
  18. }
  19. var devMode = loadSettings.devMode || !loadSettings.resourcePath.startsWith(process.resourcesPath + path.sep)
  20. if (devMode) {
  21. setupDeprecatedPackages()
  22. }
  23. if (loadSettings.profileStartup) {
  24. profileStartup(loadSettings, Date.now() - startTime)
  25. } else {
  26. setupWindow(loadSettings)
  27. setLoadTime(Date.now() - startTime)
  28. }
  29. } catch (error) {
  30. handleSetupError(error)
  31. }
  32. }
  33. function setLoadTime (loadTime) {
  34. if (global.atom) {
  35. global.atom.loadTime = loadTime
  36. console.log('Window load time: ' + global.atom.getWindowLoadTime() + 'ms')
  37. }
  38. }
  39. function handleSetupError (error) {
  40. var currentWindow = require('remote').getCurrentWindow()
  41. currentWindow.setSize(800, 600)
  42. currentWindow.center()
  43. currentWindow.show()
  44. currentWindow.openDevTools()
  45. console.error(error.stack || error)
  46. }
  47. function setupWindow (loadSettings) {
  48. var CompileCache = require('../src/compile-cache')
  49. CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME)
  50. var ModuleCache = require('../src/module-cache')
  51. ModuleCache.register(loadSettings)
  52. ModuleCache.add(loadSettings.resourcePath)
  53. // Start the crash reporter before anything else.
  54. require('crash-reporter').start({
  55. productName: 'Atom',
  56. companyName: 'GitHub',
  57. // By explicitly passing the app version here, we could save the call
  58. // of "require('remote').require('app').getVersion()".
  59. extra: {_version: loadSettings.appVersion}
  60. })
  61. setupVmCompatibility()
  62. setupCsonCache(CompileCache.getCacheDirectory())
  63. require(loadSettings.bootstrapScript)
  64. require('ipc').sendChannel('window-command', 'window:loaded')
  65. }
  66. function setupAtomHome () {
  67. if (!process.env.ATOM_HOME) {
  68. var home
  69. if (process.platform === 'win32') {
  70. home = process.env.USERPROFILE
  71. } else {
  72. home = process.env.HOME
  73. }
  74. var atomHome = path.join(home, '.atom')
  75. try {
  76. atomHome = fs.realpathSync(atomHome)
  77. } catch (error) {
  78. // Ignore since the path might just not exist yet.
  79. }
  80. process.env.ATOM_HOME = atomHome
  81. }
  82. }
  83. function setupCsonCache (cacheDir) {
  84. require('season').setCacheDir(path.join(cacheDir, 'cson'))
  85. }
  86. function setupVmCompatibility () {
  87. var vm = require('vm')
  88. if (!vm.Script.createContext) {
  89. vm.Script.createContext = vm.createContext
  90. }
  91. }
  92. function setupDeprecatedPackages () {
  93. var metadata = require('../package.json')
  94. if (!metadata._deprecatedPackages) {
  95. try {
  96. metadata._deprecatedPackages = require('../build/deprecated-packages.json')
  97. } catch(requireError) {
  98. console.error('Failed to setup deprecated packages list', requireError.stack)
  99. }
  100. }
  101. }
  102. function profileStartup (loadSettings, initialTime) {
  103. function profile () {
  104. console.profile('startup')
  105. try {
  106. var startTime = Date.now()
  107. setupWindow(loadSettings)
  108. setLoadTime(Date.now() - startTime + initialTime)
  109. } catch (error) {
  110. handleSetupError(error)
  111. } finally {
  112. console.profileEnd('startup')
  113. console.log('Switch to the Profiles tab to view the created startup profile')
  114. }
  115. }
  116. var currentWindow = require('remote').getCurrentWindow()
  117. if (currentWindow.devToolsWebContents) {
  118. profile()
  119. } else {
  120. currentWindow.openDevTools()
  121. currentWindow.once('devtools-opened', function () {
  122. setTimeout(profile, 100)
  123. })
  124. }
  125. }
  126. function parseLoadSettings () {
  127. var rawLoadSettings = decodeURIComponent(window.location.hash.substr(1))
  128. try {
  129. loadSettings = JSON.parse(rawLoadSettings)
  130. } catch (error) {
  131. console.error('Failed to parse load settings: ' + rawLoadSettings)
  132. loadSettingsError = error
  133. }
  134. }
  135. function setupWindowBackground () {
  136. if (loadSettings && loadSettings.isSpec) {
  137. return
  138. }
  139. var backgroundColor = window.localStorage.getItem('atom:window-background-color')
  140. if (!backgroundColor) {
  141. return
  142. }
  143. var backgroundStylesheet = document.createElement('style')
  144. backgroundStylesheet.type = 'text/css'
  145. backgroundStylesheet.innerText = 'html, body { background: ' + backgroundColor + '; }'
  146. document.head.appendChild(backgroundStylesheet)
  147. // Remove once the page loads
  148. window.addEventListener('load', function loadWindow () {
  149. window.removeEventListener('load', loadWindow, false)
  150. setTimeout(function () {
  151. backgroundStylesheet.remove()
  152. backgroundStylesheet = null
  153. }, 1000)
  154. }, false)
  155. }
  156. parseLoadSettings()
  157. setupWindowBackground()
  158. })()