-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
243 lines (198 loc) · 7.89 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
'use strict'
var fs = require('fs')
var helper = require('./lib/iedriver')
var request = require('request')
var kew = require('kew')
var npmconf = require('npmconf')
var mkdirp = require('mkdirp')
var path = require('path')
var rimraf = require('rimraf').sync
var util = require('util')
var extract = require('extract-zip')
var exec = require('child_process').exec;
var libPath = path.join(__dirname, 'lib', 'iedriver')
var libPath64 = path.join(__dirname, 'lib', 'iedriver64')
var baseUrl = process.env.IEDRIVER_CDNURL || process.env.npm_config_iedriver_cdnurl
|| 'https://github.com/SeleniumHQ/selenium/releases/download'
var downloadUrl = baseUrl + '/selenium-%s/IEDriverServer_Win32_%s.zip'
var downloadUrl64 = baseUrl + '/selenium-%s/IEDriverServer_x64_%s.zip'
downloadUrl = util.format(downloadUrl, helper.version, helper.binaryversion);
downloadUrl64 = util.format(downloadUrl64, helper.version, helper.binaryversion);
var fileName = util.format('IEDriverServer_Win32_%s.zip', helper.binaryversion);
var fileName64 = util.format('IEDriverServer_x64_%s.zip', helper.binaryversion);
var promise = kew.resolve(true)
promise = promise
.then(function () {
console.log('');
console.log('Downloading 64 bit Windows IE driver server');
console.log('-----');
return downloadDriver(downloadUrl64, fileName64, helper.md564, libPath64, 'iedriver64');
})
.then(function () {
console.log('');
console.log('Downloading 32 bit Windows IE driver server');
console.log('-----');
return downloadDriver(downloadUrl, fileName, helper.md5, libPath, 'iedriver');
});
function downloadDriver(_downloadUrl, _fileName, _md5, _libPath, _driverTmpDirName) {
var deferred = kew.defer();
npmconf.load(function (err, conf) {
if (err) {
console.log('Error loading npm config')
console.error(err)
process.exit(1)
return
}
var tmpPath = findSuitableTempDirectory(conf, _driverTmpDirName)
//console.log("tmp path", tmpPath);
var downloadedFile = path.join(tmpPath, _fileName)
// Start the install.
var downloadPromise = requestBinary(_downloadUrl, downloadedFile)
var extractPromise = downloadPromise.then(function () {
return extractDownload(downloadedFile, tmpPath)
.fail(function (err) {
console.error('Error extracting ' + downloadedFile, err, err.stack);
});
});
var copyPromise = extractPromise.then(function () {
console.log('copying ' + tmpPath + ' to ' + _libPath);
return copyIntoPlace(tmpPath, _libPath)
});
kew.all(downloadPromise, extractPromise, copyPromise)
.then(function () {
console.log('Success! IEDriverServer binary available at', _libPath + "\\IEDriverServer.exe");
deferred.resolve(true);
})
.fail(function (err) {
console.error('iedriver installation failed', err, err.stack);
process.exit(1);
})
});
return deferred.promise;
}
function findSuitableTempDirectory(npmConf, driverDir) {
var now = Date.now()
var candidateTmpDirs = [
process.env.TMPDIR || './tmp',
npmConf.get('tmp'),
path.join(process.cwd(), 'tmp')
]
for (var i = 0; i < candidateTmpDirs.length; i++) {
var candidatePath = path.join(candidateTmpDirs[i], driverDir)
try {
mkdirp.sync(candidatePath, '0777')
var testFile = path.join(candidatePath, now + '.tmp')
fs.writeFileSync(testFile, 'test')
fs.unlinkSync(testFile)
rimraf(candidatePath);
mkdirp.sync(candidatePath, '0777')
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/barretts/iedriver/issues/ with as much information as possible.');
process.exit(1);
}
function requestBinary(_downloadUrl, filePath) {
console.log('Downloading', _downloadUrl)
var deferred = kew.defer()
const requestOptions = getRequestOptions(_downloadUrl)
const client = request(requestOptions)
client
.on('error', function (err) {
deferred.reject('Error with http request: ' + util.inspect(err))
})
.on('end', function () {
deferred.resolve(true)
})
.pipe(fs.createWriteStream(filePath))
return deferred.promise
}
function getRequestOptions(_downloadUrl) {
const options = {uri: _downloadUrl, method: 'GET'};
const proxyUrl = process.env.npm_config_https_proxy || process.env.npm_config_proxy || process.env.npm_config_http_proxy;
if (proxyUrl) {
options.proxy = proxyUrl;
}
return options;
}
function extractDownload(filePath, tmpPath) {
var deferred = kew.defer()
var options = { dir: fs.realpathSync(tmpPath) }
try {
extract(filePath, options, function (err) {
if (err) {
console.error(err)
deferred.reject('Error extracting archive ' + err.stack)
}
else {
console.log(filePath + ' extracted to ' + tmpPath)
deferred.resolve(true)
}
});
} catch (err) {
deferred.reject('Error extracting archive ' + err.stack)
}
return deferred.promise
}
function copyIntoPlace(tmpPath, targetPath) {
rimraf(targetPath);
//console.log("Copying to target path", targetPath, "from", tmpPath);
fs.mkdirSync(targetPath);
// Look for the extracted directory, so we can rename it.
var files = fs.readdirSync(tmpPath);
var promises = files.map(function (name) {
var deferred = kew.defer();
var file = path.join(tmpPath, name);
var reader = fs.createReadStream(file);
var targetFile = path.join(targetPath, name);
var writer = fs.createWriteStream(targetFile);
writer.on("close", function () {
//console.log("copied", name);
deferred.resolve(true);
});
reader.pipe(writer);
return deferred.promise;
});
return kew.all(promises);
}
var regContent = `
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects]
[-HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects\\{1FD49718-1D00-4B19-AF5F-070AF6D5D54C}]
[HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects]
[-HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects\\{1FD49718-1D00-4B19-AF5F-070AF6D5D54C}]
`;
var regFileName = 'Remove_IE11_to_Edge_redirect.reg';
fs.writeFile(regFileName, regContent, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
exec('regedit /s ' + regFileName, function(err, data) {
if(err) {
console.log(err);
} else {
console.log(data ? data.toString() : "Registry file executed successfully.");
// Check if the registry keys have been deleted
var regKey1 = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects\\{1FD49718-1D00-4B19-AF5F-070AF6D5D54C}';
var regKey2 = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects\\{1FD49718-1D00-4B19-AF5F-070AF6D5D54C}';
exec('reg query ' + regKey1, function(err, data) {
if(err) {
console.log('Registry key ' + regKey1 + ' has been deleted successfully');
} else {
console.log('Failed to delete registry key ' + regKey1);
}
});
exec('reg query ' + regKey2, function(err, data) {
if(err) {
console.log('Registry key ' + regKey2 + ' has been deleted successfully');
} else {
console.log('Failed to delete registry key ' + regKey2);
}
});
}
});
}
});