hot-reload: implememnt auto session/state backup on reload and display error stack in a popup
This commit is contained in:
parent
bcf80a1668
commit
42bd16df09
5 changed files with 109 additions and 52 deletions
|
|
@ -10,7 +10,8 @@ var path = require('path'),
|
|||
|
||||
var openedSessions = {},
|
||||
widgetHashTable = {},
|
||||
lastSavingClient
|
||||
lastSavingClient,
|
||||
sessionBackups = {}
|
||||
|
||||
module.exports = {
|
||||
|
||||
|
|
@ -21,6 +22,12 @@ module.exports = {
|
|||
ipc.send('readOnly')
|
||||
}
|
||||
|
||||
if (data.backupId && sessionBackups[data.backupId]) {
|
||||
ipc.send('loadBackup', sessionBackups[data.backupId])
|
||||
delete sessionBackups[data.backupId]
|
||||
return
|
||||
}
|
||||
|
||||
if (settings.read('newSession')) {
|
||||
ipc.send('sessionNew')
|
||||
return
|
||||
|
|
@ -295,11 +302,17 @@ module.exports = {
|
|||
fullscreen: function(data) {
|
||||
window.setFullScreen(!window.isFullScreen())
|
||||
},
|
||||
|
||||
refresh: function(data) {
|
||||
for (var w of BrowserWindow.getAllWindows()){
|
||||
w.reload()
|
||||
}
|
||||
|
||||
reload: function(data) {
|
||||
|
||||
ipc.send('reload')
|
||||
|
||||
},
|
||||
|
||||
storeBackup: function(data) {
|
||||
|
||||
sessionBackups[data.backupId] = data
|
||||
|
||||
},
|
||||
|
||||
reloadCss:function(){
|
||||
|
|
@ -317,5 +330,10 @@ module.exports = {
|
|||
|
||||
errorLog: function(data) {
|
||||
console.error(data)
|
||||
},
|
||||
|
||||
errorPopup: function(data) {
|
||||
ipc.send('error', data)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ module.exports = function(options={}) {
|
|||
}
|
||||
})
|
||||
|
||||
window.webContents.on('will-navigate', (event)=>event.preventDefault())
|
||||
// window.webContents.on('will-navigate', (event)=>event.preventDefault())
|
||||
window.webContents.on('new-window', (event)=>event.preventDefault())
|
||||
|
||||
if (options.fullscreen) {
|
||||
|
|
|
|||
|
|
@ -60,56 +60,43 @@ if (watch) {
|
|||
}
|
||||
|
||||
bundle()
|
||||
var pendingSocket
|
||||
const wsAddress = 'ws://127.0.0.1:8080/dev'
|
||||
if(autoRefresh){
|
||||
|
||||
if (autoRefresh) {
|
||||
var Convert = require('ansi-to-html')
|
||||
var convert = new Convert()
|
||||
var WS = require('../app/node_modules/ws')
|
||||
pendingSocket = new WS(wsAddress)
|
||||
pendingSocket.on('error', (err)=>{console.error(err)})
|
||||
pendingSocket.on('open', ()=>{})
|
||||
pendingSocket.on('close', ()=>{pendingSocket = null;})
|
||||
function send(msg, data){
|
||||
var ipc = new WS('ws://127.0.0.1:8080/dev')
|
||||
ipc.on('error', ()=>{})
|
||||
ipc.on('open', ()=>{
|
||||
ipc.send(JSON.stringify([msg, data]))
|
||||
ipc.close()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var hasError = false
|
||||
|
||||
function bundle() {
|
||||
|
||||
var output = b.bundle()
|
||||
output.on('end', (err)=> {
|
||||
console.log('build successful',pendingSocket?'sending refresh':'')
|
||||
if(autoRefresh && pendingSocket && hasError){
|
||||
pendingSocket.send('["refresh"]')
|
||||
hasError = false
|
||||
//pendingSocket.close();
|
||||
}
|
||||
})
|
||||
output.on('error', (err)=> {
|
||||
console.error(err.stack)
|
||||
var Convert = require('ansi-to-html');
|
||||
var convert = new Convert(
|
||||
{fg: '#0F0',
|
||||
bg: '#000'});
|
||||
const errFilePath = err.stack.split(':')?'file://'+err.stack.split(':')[1].trim()+':'+err.loc.line:''
|
||||
let formattedStack = err.stack.replace(new RegExp(path.resolve(__dirname + '/..'), 'g'),'.');
|
||||
// let stackWithoutAnsi = formattedStack.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,'')// escape ANSI codes
|
||||
|
||||
|
||||
fs.createWriteStream(outputPath).write(`
|
||||
window.onload = ()=>{
|
||||
document.write(\`<body> <div>${convert.toHtml(formattedStack)}</div><button onclick="refresh()"></button></body>\`)
|
||||
document.body.style['color']='#0F0'
|
||||
document.body.style['white-space']='pre-wrap'
|
||||
console.error(\`${errFilePath}\`)
|
||||
}
|
||||
`)
|
||||
|
||||
if(autoRefresh ){
|
||||
if(pendingSocket){
|
||||
pendingSocket.send('["refresh"]')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
output.on('end', (err)=> {
|
||||
console.log('Build successful', autoRefresh ? 'reloading...' : '')
|
||||
if (autoRefresh) send('reload')
|
||||
})
|
||||
|
||||
output.on('error', (err)=> {
|
||||
|
||||
console.error(err.stack)
|
||||
send('errorPopup', convert.toHtml(
|
||||
'<div style="white-space:pre">' +
|
||||
err.stack.replace(new RegExp(path.resolve(__dirname + '/..'), 'g'),'.').trim().replace('\n','\n\n') +
|
||||
'</div>'
|
||||
))
|
||||
|
||||
})
|
||||
|
||||
|
||||
if (!fast) output.pipe(exorcist(outputPath + '.map'))
|
||||
|
||||
output.pipe(fs.createWriteStream(outputPath))
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ var utils = require('../ui/utils'),
|
|||
widgetManager = require('../managers/widgets'),
|
||||
state = require('../managers/state'),
|
||||
editor = require('../editor/'),
|
||||
locales = require('../locales')
|
||||
locales = require('../locales'),
|
||||
ipc = require('./')
|
||||
|
||||
module.exports = {
|
||||
|
||||
|
|
@ -81,11 +82,61 @@ module.exports = {
|
|||
if (root) root.reCreateWidget()
|
||||
},100)
|
||||
|
||||
|
||||
GRIDWIDTH = getComputedStyle(document.documentElement).getPropertyValue('--grid-width')
|
||||
},
|
||||
|
||||
readOnly: function(){
|
||||
READ_ONLY = true
|
||||
},
|
||||
|
||||
reload: function(){
|
||||
|
||||
var id = Math.random(),
|
||||
search = location.search,
|
||||
query = 'backupId=' + id
|
||||
|
||||
// disable editor's warning
|
||||
window.onbeforeunload = null
|
||||
|
||||
if (!session.session.length) {
|
||||
window.location.href = window.location.href
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
// store session & state backup
|
||||
ipc.send('storeBackup', {
|
||||
backupId: id,
|
||||
session: session.session,
|
||||
state: state.get()
|
||||
})
|
||||
|
||||
// reload page and hold backup id
|
||||
|
||||
if (search) {
|
||||
search = search.replace(/backupId=[^&]*/, query)
|
||||
} else {
|
||||
search += '?' + query
|
||||
}
|
||||
|
||||
window.location.search = search
|
||||
|
||||
} catch(e) {
|
||||
|
||||
window.location.href = window.location.href
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
loadBackup: function(data) {
|
||||
|
||||
session.load(data.session, ()=>{
|
||||
state.set(data.state, false)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,8 @@ document.addEventListener('DOMContentLoaded', function(event) {
|
|||
ipc.init()
|
||||
|
||||
require('./app/ui/init')
|
||||
ipc.send('ready')
|
||||
|
||||
ipc.send('ready', {backupId: ARGV.backupId})
|
||||
|
||||
|
||||
}, 100)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue