This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
111 lines (97 loc) · 3.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var commands = require('./lib/commands.js'),
electron = require('electron'),
util = require('util'),
fs = require('fs'),
spawn = require('child_process').spawn,
ipc = electron.ipcMain,
minimist = require('minimist'),
launch = require('./lib/launch.js'),
directory = require('./lib/directory.js'),
file = require('./lib/file.js'),
watcher = require('./lib/watcher.js'),
errors = require('./lib/errors.js'),
Logger = require('./lib/logger.js');
var argv = minimist(process.argv.slice(2));
if (argv['electron-update']) {
// These modules are not available in render process.
var app = require('app');
var BrowserWindow = require('browser-window');
var relaunch = typeof argv.relaunch === 'boolean' ? argv.relaunch : true
var encodedArgs = argv['electron-update']
var decodedArgs = new Buffer(encodedArgs, 'base64').toString('ascii')
var args = JSON.parse(decodedArgs)
// {
// name: appName,
// publisher: publisher,
// exe: process.execPath,
// cwd: process.cwd(),
// argv: process.argv,
// debug: true
// }
var appDir = directory.appDir(args.publisher, args.appName)
var electronDir = path.dirname(args.exe)
var pendingUpdatePath = path.join(appDir, '.update')
var logger = new Logger(appDir, Logger.appendToFile, args.debug)
// listen for uncaught exceptions.
errors.listen(logger, pendingUpdatePath);
logger.log('Starting Update:')
logger.log(' args: ' + util.inspect(args))
// Flag an update as pending
file.touch(pendingUpdatePath, 'INPROGRESS', function (err) {
if(err) return errors.handle(err)
// Attempt to actually udpate now.
var win = new BrowserWindow({
width: 400,
height: 100,
frame: false
})
win.on('close', function (e) {
logger.log('Window is closing...')
})
win.loadURL('file://' + __dirname + '/update.html')
ipc.on('initialize', function (event, arg) {
logger.log('Initialized.')
event.sender.send('initialize', args)
commands.update(process.cwd(), electronDir, logger, function (err) {
if(err) {
// If the update fails for security reasons, then we have to attempt to relaunch this process
// with the right permissions.
if(err.code === 'EPERM') {
logger.log('No permission to update, elevating...')
var elevatedArgs = process.argv.slice(1)
// Tell the elevated process not to relaunch, we will relaunch from this process when its done.
elevatedArgs.push('--no-relaunch')
// relaunch self as an elevated process
launch.elevate(args.publisher, args.appName, process.execPath, elevatedArgs, process.cwd(), function (err) {
if(err) return errors.handle(err);
// Watch for changes to the .update file, it will become empty when the update succeeds.
watcher.watch(pendingUpdatePath, function (err) {
if (err) return errors.handle(err);
file.touch(pendingUpdatePath, '', function () {
if (relaunch) {
logger.log('relaunching from unelevated process.')
launch.detached(args, logger)
}
process.exit(0);
})
})
})
} else {
errors.handle(err);
}
} else {
// Update was successful!
logger.log('updated succeeded.')
file.touch(pendingUpdatePath, '', function (err) {
if(err) logger.log(err)
// If the app was already running as admin, this flag will be missing. Go ahead and re-launch the app.
if(relaunch) launch.detached(args, logger)
process.exit(0);
})
}
})
})
})
} else {
module.exports = commands
}