forked from webdriverio-community/node-edgedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
161 lines (130 loc) · 4.5 KB
/
Copy pathinstall.js
File metadata and controls
161 lines (130 loc) · 4.5 KB
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
'use strict'
var cp = require('child_process')
var fs = require('fs')
var helper = require('./lib/edgedriver')
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 url = require('url')
var util = require('util')
var md5file = require('md5-file')
var os = require('os')
var libPath = path.join(__dirname, 'lib', 'edgedriver')
// Select Microsoft WebDriver to download
var microsoftWebDriverDownloadUrls = {
'14393': 'https://download.microsoft.com/download/3/2/D/32D3E464-F2EF-490F-841B-05D53C848D15/MicrosoftWebDriver.exe',
'15063': 'https://download.microsoft.com/download/3/4/2/342316D7-EBE0-4F10-ABA2-AE8E0CDF36DD/MicrosoftWebDriver.exe',
'16299': 'https://download.microsoft.com/download/D/4/1/D417998A-58EE-4EFE-A7CC-39EF9E020768/MicrosoftWebDriver.exe',
'17134': 'https://download.microsoft.com/download/F/8/A/F8AF50AB-3C3A-4BC4-8773-DC27B32988DD/MicrosoftWebDriver.exe',
}
var osBuildNumber = os.release().split('.')[2]
var downloadUrl = ''
if (osBuildNumber in microsoftWebDriverDownloadUrls) {
downloadUrl = microsoftWebDriverDownloadUrls[osBuildNumber]
}
console.log('downloadUrl: ' + downloadUrl)
var fileName = 'MicrosoftWebDriver.exe';
npmconf.load(function(err, conf) {
if (downloadUrl === '') {
console.warn('NOTE: Cannot find Microsoft WebDriver for the current OS:', process.platform, process.arch, os.release())
process.exit(0)
return
}
if (err) {
console.log('Error loading npm config')
console.error(err)
process.exit(1)
return
}
var tmpPath = findSuitableTempDirectory(conf)
var downloadedFile = path.join(tmpPath, fileName)
var promise = kew.resolve(true)
// Start the install.
promise = promise.then(function () {
console.log('Downloading', downloadUrl)
console.log('Saving to', downloadedFile)
return requestBinary(downloadUrl, downloadedFile)
})
.then(function () {
return copyIntoPlace(tmpPath, libPath)
})
.then(function () {
console.log('Done. edgedriver binary available at', libPath+"\\MicrosoftWebDriver.exe")
})
.fail(function (err) {
console.error('edgedriver installation failed', err, err.stack)
process.exit(1)
})
})
function findSuitableTempDirectory(npmConf) {
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], 'edgedriver')
try {
mkdirp.sync(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/barretts/edgedriver/issues/ with as much information as possible.');
process.exit(1);
}
function requestBinary(_downloadUrl, filePath) {
var deferred = kew.defer()
request
.get(_downloadUrl)
.on('error', function (err) {
deferred.reject('Error with http request: ' + util.inspect(response.headers))
})
.on('end', function () {
deferred.resolve(true)
})
.pipe(fs.createWriteStream(filePath))
return deferred.promise
}
function validateMd5(filePath, md5value) {
var deferred = kew.defer()
console.log('Validating MD5 checksum')
try {
if (md5file(filePath).toLowerCase() == md5value.toLowerCase()) {
deferred.resolve(true)
} else {
deferred.reject('Error archive md5 checksum does not match')
}
} catch (err) {
deferred.reject('Error trying to match md5 checksum')
}
return deferred.promise
}
function copyIntoPlace(tmpPath, targetPath) {
rimraf(targetPath);
console.log("Copying to target path", targetPath);
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() {
deferred.resolve(true);
});
reader.pipe(writer);
return deferred.promise;
});
return kew.all(promises);
}