pulsar/static/index.js

129 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-08-22 02:56:32 +03:00
(function () {
2017-02-27 16:48:45 +03:00
const electron = require('electron')
2017-02-27 16:47:20 +03:00
const path = require('path')
2017-02-28 11:34:55 +03:00
const Module = require('module')
2017-02-27 16:47:20 +03:00
const getWindowLoadSettings = require('../src/get-window-load-settings')
const entryPointDirPath = __dirname
let useSnapshot = false
window.onload = function () {
try {
2017-02-27 16:51:33 +03:00
const startTime = Date.now()
2017-02-27 16:47:20 +03:00
2015-08-22 02:56:32 +03:00
process.on('unhandledRejection', function (error, promise) {
console.error('Unhandled promise rejection %o with error: %o', promise, error)
})
2017-02-27 16:47:20 +03:00
// Normalize to make sure drive letter case is consistent on Windows
process.resourcesPath = path.normalize(process.resourcesPath)
setupAtomHome()
const devMode = getWindowLoadSettings().devMode || !getWindowLoadSettings().resourcePath.startsWith(process.resourcesPath + path.sep)
2017-02-27 16:47:20 +03:00
useSnapshot = !devMode && typeof snapshotResult !== 'undefined'
2017-02-27 16:47:20 +03:00
if (devMode) {
2017-02-27 16:51:33 +03:00
const metadata = require('../package.json')
2017-02-27 16:47:20 +03:00
if (!metadata._deprecatedPackages) {
try {
metadata._deprecatedPackages = require('../script/deprecated-packages.json')
} catch (requireError) {
console.error('Failed to setup deprecated packages list', requireError.stack)
}
}
2017-02-27 16:47:20 +03:00
} else if (useSnapshot) {
2017-02-28 11:34:55 +03:00
Module.prototype.require = function (module) {
const absoluteFilePath = Module._resolveFilename(module, this, false)
const relativeFilePath = path.relative(entryPointDirPath, absoluteFilePath)
2017-02-28 14:15:00 +03:00
let cachedModule = snapshotResult.customRequire.cache[relativeFilePath]
if (!cachedModule) {
cachedModule = Module._load(module, this, false)
snapshotResult.customRequire.cache[relativeFilePath] = cachedModule
}
return cachedModule
2017-02-27 16:47:20 +03:00
}
2017-02-27 18:37:48 +03:00
snapshotResult.setGlobals(global, process, window, document, require)
snapshotResult.entryPointDirPath = __dirname
}
2017-02-27 16:47:20 +03:00
if (getWindowLoadSettings().profileStartup) {
profileStartup(Date.now() - startTime)
} else {
setupWindow()
setLoadTime(Date.now() - startTime)
2015-08-22 02:56:32 +03:00
}
2017-02-27 16:47:20 +03:00
} catch (error) {
handleSetupError(error)
}
}
function setLoadTime (loadTime) {
if (global.atom) {
global.atom.loadTime = loadTime
}
}
function handleSetupError (error) {
2017-02-27 16:48:45 +03:00
const currentWindow = electron.remote.getCurrentWindow()
2017-02-27 16:47:20 +03:00
currentWindow.setSize(800, 600)
currentWindow.center()
currentWindow.show()
currentWindow.openDevTools()
console.error(error.stack || error)
}
function setupWindow () {
2017-02-28 19:17:02 +03:00
const CompileCache = useSnapshot ? snapshotResult.customRequire('../src/compile-cache.js') : require('../src/compile-cache')
2017-02-28 14:15:00 +03:00
CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME)
CompileCache.install(require)
2017-02-28 19:17:02 +03:00
const ModuleCache = useSnapshot ? snapshotResult.customRequire('../src/module-cache.js') : require('../src/module-cache')
2017-02-27 16:47:20 +03:00
ModuleCache.register(getWindowLoadSettings())
2017-02-28 19:17:02 +03:00
const startCrashReporter = useSnapshot ? snapshotResult.customRequire('../src/crash-reporter-start.js') : require('../src/crash-reporter-start')
2017-02-27 16:47:20 +03:00
startCrashReporter({_version: getWindowLoadSettings().appVersion})
2017-02-28 19:17:02 +03:00
const CSON = useSnapshot ? snapshotResult.customRequire('../node_modules/season/lib/cson.js') : require('season')
2017-02-27 16:47:20 +03:00
CSON.setCacheDir(path.join(CompileCache.getCacheDirectory(), 'cson'))
const initScriptPath = path.relative(entryPointDirPath, getWindowLoadSettings().windowInitializationScript)
2017-02-28 19:17:02 +03:00
const initialize = useSnapshot ? snapshotResult.customRequire(initScriptPath) : require(initScriptPath)
return initialize().then(function () {
2017-02-27 16:48:45 +03:00
electron.ipcRenderer.send('window-command', 'window:loaded')
2017-02-27 16:47:20 +03:00
})
}
2017-02-27 16:47:20 +03:00
function profileStartup (initialTime) {
function profile () {
console.profile('startup')
2017-02-27 16:51:33 +03:00
const startTime = Date.now()
2017-02-27 16:47:20 +03:00
setupWindow().then(function () {
setLoadTime(Date.now() - startTime + initialTime)
console.profileEnd('startup')
console.log('Switch to the Profiles tab to view the created startup profile')
})
}
2015-05-13 19:47:10 +03:00
2017-02-27 16:48:45 +03:00
const webContents = electron.remote.getCurrentWindow().webContents
2017-02-27 16:47:20 +03:00
if (webContents.devToolsWebContents) {
profile()
} else {
webContents.once('devtools-opened', () => { setTimeout(profile, 1000) })
webContents.openDevTools()
}
}
2017-02-27 16:47:20 +03:00
function setupAtomHome () {
if (process.env.ATOM_HOME) {
return
}
2015-06-04 02:11:16 +03:00
2017-02-27 16:47:20 +03:00
// Ensure ATOM_HOME is always set before anything else is required
// This is because of a difference in Linux not inherited between browser and render processes
// https://github.com/atom/atom/issues/5412
if (getWindowLoadSettings() && getWindowLoadSettings().atomHome) {
process.env.ATOM_HOME = getWindowLoadSettings().atomHome
}
}
2015-08-22 02:56:32 +03:00
})()