-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
177 lines (162 loc) · 5.28 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const EventEmitter = require( "events" ),
AppSwapStrategy = require( "./Lib/Strategy/AppSwap" ),
ScriptSwapStrategy = require( "./Lib/Strategy/ScriptSwap" ),
semver = require( "semver" ),
os = require( "os" ),
{lstatSync, existsSync} = require('fs'),
{ join, basename, dirname, parse } = require( "path" ),
unpackTarGz = require( "./Lib/unpackTarGz" ),
unpackZip = require( "./Lib/unpackZip" ),
debounce = require( "debounce" ),
{ readJson, download } = require( "./Lib/request" ),
{ launch, rtrim, remove } = require( "./Lib/utils" ),
{ PLATFORM_FULL, swapFactory,
getExecutable, UPDATE_DIR, EXEC_DIR, BACKUP_DIR, LOG_PATH } = require( "./Lib/env" ),
ERR_INVALID_REMOTE_MANIFEST = "Invalid manifest structure",
DEBOUNCE_TIME = 100,
DEFAULT_OPTIONS = {
executable: null,
backupDir: BACKUP_DIR,
execDir: EXEC_DIR,
updateDir: UPDATE_DIR,
logPath: LOG_PATH,
verbose: false,
swapScript: null,
strategy: "AppSwap",
accumulativeBackup: false
};
class AutoUpdater extends EventEmitter {
/**
* Create AutoUpdate
* @param {Object} manifest
* @param {Object} options
*/
constructor( manifest, options = {}){
super();
this.manifest = manifest;
if ( !this.manifest.manifestUrl ) {
throw new Error( `Manifest must contain manifestUrl field` );
}
this.release = "";
this.argv = nw.App.argv;
this.remoteManifest = "";
this.options = Object.assign( {}, DEFAULT_OPTIONS, options );
this.options.backupDir += this.options.accumulativeBackup ? `_${Math.floor(Date.now() / 1000)}` : ``;
this.options.execDir = rtrim( this.options.execDir );
this.options.executable = this.options.executable || getExecutable( manifest.name );
// Mixing up a chosen strategy
Object.assign( this, this.options.strategy === "ScriptSwap" ? ScriptSwapStrategy : AppSwapStrategy );
}
/**
* Read package.json from the release server
* @returns {Promise<JSON>}
*/
async readRemoteManifest(){
try {
return await readJson( this.manifest.manifestUrl );
} catch ( e ) {
throw new Error( `Cannot read remote manifest from ${this.manifest.manifestUrl}` );
}
}
/**
* Check if a new app version available
* @param {Object} remoteManifest
* @returns {Promise<boolean>}
*/
async checkNewVersion( remoteManifest ){
if ( !remoteManifest || !remoteManifest.packages ){
throw new TypeError( ERR_INVALID_REMOTE_MANIFEST );
}
return semver.gt( remoteManifest.version, this.manifest.version );
}
/**
* Download new version
* @param {Object} remoteManifest
* @param {Object} options
* @returns {Promise<string>}
*/
async download( remoteManifest, { debounceTime } = { debounceTime: DEBOUNCE_TIME }){
if ( !remoteManifest || !remoteManifest.packages ){
throw new TypeError( ERR_INVALID_REMOTE_MANIFEST );
}
const release = remoteManifest.packages[ PLATFORM_FULL ];
if ( !release ) {
throw new Error( `No release matches the platfrom ${PLATFORM_FULL}` );
}
const onProgress = ( length ) => {
this.emit( "download", length, release.size );
};
try {
remove( this.options.updateDir );
return await download( release.url, os.tmpdir(), debounce( onProgress, debounceTime ));
} catch ( e ) {
throw new Error( `Cannot download package from ${release.url}` );
}
}
/**
* Unpack downloaded version
* @param {string} updateFile
* @param {Object} options
* @returns {Promise<string>}
*/
async unpack( updateFile, { debounceTime } = { debounceTime: DEBOUNCE_TIME } ){
const isZipRe = /\.zip$/i,
isGzRe = /\.tar\.gz$/i,
onProgress = ( installFiles, totalFiles ) => {
this.emit( "install", installFiles, totalFiles );
},
updateDir = this.options.updateDir;
if ( !updateFile ){
throw new Error( "You have to call first download method" );
}
switch( true ) {
case isGzRe.test( updateFile ):
try {
await unpackTarGz( updateFile, updateDir, debounce( onProgress, debounceTime ) );
} catch ( e ) {
throw new Error( `Cannot unpack .tar.gz package ${updateFile}` );
}
break;
case isZipRe.test( updateFile ):
try {
await unpackZip( updateFile, updateDir, debounce( onProgress, debounceTime ) );
} catch ( e ) {
throw new Error( `Cannot unpack .zip package ${updateFile}: ${e.message}` );
}
break;
default:
throw new Error( "Release archive of unsupported type" );
break;
}
//If extract zip in new folder
let newPath = join(updateDir, parse(updateFile).name);
if (existsSync(newPath)) {
if (lstatSync(newPath).isDirectory()) {
this.options.updateDir = newPath;
}
}
return updateDir;
}
/**
* @deprecated since v.1.1.0
* @returns {Boolean}
*/
isSwapRequest(){
return false;
}
/**
* @deprecated since v.1.1.0
* @returns {Boolean}
*/
async swap(){
return false;
}
/**
* @deprecated since v.1.1.0
* @returns {Boolean}
*/
async restart(){
return false;
}
}
module.exports = AutoUpdater;