/
install.js
executable file
·260 lines (227 loc) · 7.58 KB
/
install.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* install browser,
*
* chrome( windows + ubuntu )
*
* @type {exports}
*/
var AdmZip = require( 'adm-zip' )
var cp = require( 'child_process' )
var fs = require( 'fs' )
var http = require( 'http' )
var kew = require( 'kew' )
var ncp = require( 'ncp' )
var npmconf = require( 'npmconf' )
var mkdirp = require( 'mkdirp' )
var path = require( 'path' )
var rimraf = require( 'rimraf' ).sync
var url = require( 'url' )
var util = require( 'util' )
var which = require( 'which' )
var browserPath = path.join( __dirname, 'browsers' )
var originalPath = process.env.PATH
var tmpPath = null;
if( !fs.existsSync( browserPath ) ){
fs.mkdirSync( browserPath );
}
if( process.platform == 'linux' ){
//priviledge,x
var cp = require( 'child_process' );
cp.exec( 'chmod -R u+x ' + path.resolve( __dirname ) );
var BrowserScript = require( './libs/browserScript' );
var bs = new BrowserScript( {browser:'chrome'} );
bs.installXvfb( function(err, res){
if( !err ){
console.log( 'install xvfb succ!' );
bs.installBrowser( function(err, res){
if( !err ){
console.log( 'install browser succ!' );
exit( 0 );
}else{
console.log( res );
exit( 1 );
}
} );
}else{
console.log( res );
exit( 1 );
}
} );
}else{
//download portable browser
var downloadUrl = 'http://pagetimeline.duapp.com/browser/ChromiumPortable.zip';
var windowsPath = path.join( browserPath, 'windows' );
if( !fs.existsSync( windowsPath ) ){
fs.mkdirSync( windowsPath );
}
var chromePath = path.join( windowsPath, 'chrome' );
var whichDeferred = kew.defer()
which( 'pagetimeline', whichDeferred.makeNodeResolver() )
whichDeferred.promise.then( function(path){
if( /NPM_INSTALL_MARKER/.test( fs.readFileSync( path, 'utf8' ) ) ){
console.log( 'Looks like an `npm install -g`; unable to check for already installed version.' )
throw new Error( 'Global install' )
}else{
throw new Error( 'not installed' );
}
} ).fail( function(err){
var npmconfDeferred = kew.defer()
npmconf.load( npmconfDeferred.makeNodeResolver() )
return npmconfDeferred.promise
} ).then( function(conf){
tmpPath = findSuitableTempDirectory( conf )
var fileName = downloadUrl.split( '/' ).pop()
var downloadedFile = path.join( tmpPath, fileName )
// Start the install.
if( !fs.existsSync( downloadedFile ) ){
console.log( 'Downloading', downloadUrl )
console.log( 'Saving to', downloadedFile )
return requestBinary( getRequestOptions( conf.get( 'proxy' ) ), downloadedFile )
}else{
console.log( 'Download already available at', downloadedFile )
return downloadedFile
}
} ).then( function(downloadedFile){
return extractDownload( downloadedFile )
} ).then( function(extractedPath){
return copyIntoPlace( extractedPath, chromePath )
} ).fail( function(err){
console.error( 'pagetimeline browser installation failed', err, err.stack )
exit( 1 )
} )
}
function exit(code){
process.env.PATH = originalPath
process.exit( code || 0 )
}
function findSuitableTempDirectory(npmConf){
var now = Date.now()
var candidateTmpDirs = [
process.env.TMPDIR || process.env.TEMP || npmConf.get( 'tmp' ), '/tmp', path.join( process.cwd(), 'tmp' )
]
for( var i = 0; i < candidateTmpDirs.length; i++ ){
var candidatePath = path.join( candidateTmpDirs[i], 'pagetimeline' )
try{
mkdirp.sync( candidatePath, '0777' )
// Make double sure we have 0777 permissions; some operating systems
// default umask does not allow write by default.
fs.chmodSync( candidatePath, '0777' )
var testFile = path.join( candidatePath, now + '.tmp' )
fs.writeFileSync( testFile, 'test' )
fs.unlinkSync( testFile )
return candidatePath
}catch( e ){
console.log( candidatePath, 'is not writable:', e.message )
}
}
console.error( 'Can not find a writable tmp directory, please report issue ' + 'on https://github.com/fex/pagetimeline' )
exit( 1 )
}
function getRequestOptions(proxyUrl){
if( proxyUrl ){
var options = url.parse( proxyUrl )
options.path = downloadUrl
options.headers = {
Host:url.parse( downloadUrl ).host
}
// If going through proxy, spoof the User-Agent, since may commerical proxies block blank or unknown agents in headers
options.headers['User-Agent'] = 'curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5'
// Turn basic authorization into proxy-authorization.
if( options.auth ){
options.headers['Proxy-Authorization'] = 'Basic ' + new Buffer( options.auth ).toString( 'base64' )
delete options.auth
}
return options
}else{
return url.parse( downloadUrl )
}
}
function requestBinary(requestOptions, filePath){
var deferred = kew.defer()
var count = 0
var notifiedCount = 0
var writePath = filePath + '-download-' + Date.now()
var outFile = fs.openSync( writePath, 'w' )
var client = http.get( requestOptions, function(response){
var status = response.statusCode
console.log( 'Receiving...' )
if( status === 200 ){
response.addListener( 'data', function(data){
fs.writeSync( outFile, data, 0, data.length, null )
count += data.length
if( (count - notifiedCount) > 800000 ){
console.log( 'Received ' + Math.floor( count / 1024 ) + 'K...' )
notifiedCount = count
}
} )
response.addListener( 'end', function(){
console.log( 'Received ' + Math.floor( count / 1024 ) + 'K total.' )
fs.closeSync( outFile )
fs.renameSync( writePath, filePath )
deferred.resolve( filePath )
} )
}else{
client.abort()
console.error( 'Error requesting archive' )
deferred.reject( new Error( 'Error with http request: ' + util.inspect( response.headers ) ) )
}
} )
return deferred.promise
}
function extractDownload(filePath){
var deferred = kew.defer()
// extract to a unique directory in case multiple processes are
// installing and extracting at once
var extractedPath = filePath + '-extract-' + Date.now()
var options = {
cwd:extractedPath
}
mkdirp.sync( extractedPath, '0777' )
// Make double sure we have 0777 permissions; some operating systems
// default umask does not allow write by default.
fs.chmodSync( extractedPath, '0777' )
if( filePath.substr( -4 ) === '.zip' ){
console.log( 'Extracting zip contents' )
try{
var zip = new AdmZip( filePath )
zip.extractAllTo( extractedPath, true )
deferred.resolve( extractedPath )
}catch( err ){
console.error( 'Error extracting archive' )
deferred.reject( err )
}
}else{
console.log( 'Extracting tar contents (via spawned process)' )
cp.execFile( 'tar', ['jxf', filePath], options, function(err, stdout, stderr){
if( err ){
console.error( 'Error extracting archive' )
deferred.reject( err )
}else{
deferred.resolve( extractedPath )
}
} )
}
return deferred.promise
}
function copyIntoPlace(extractedPath, targetPath){
rimraf( targetPath )
var deferred = kew.defer()
// Look for the extracted directory, so we can rename it.
var files = fs.readdirSync( extractedPath )
for( var i = 0; i < files.length; i++ ){
var file = path.join( extractedPath, files[i] )
if( fs.statSync( file ).isDirectory() ){
console.log( 'Copying extracted folder', file, '->', targetPath )
ncp( file, targetPath, deferred.makeNodeResolver() )
break
}
}
// Cleanup extracted directory after it's been copied
return deferred.promise.then( function(){
try{
return rimraf( extractedPath )
}catch( e ){
console.warn( 'Unable to remove temporary files at "' + extractedPath + '", see https://github.com/fex/pagetimeline.' )
}
} );
}