forked from giggio/node-chromedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestInstall.js
155 lines (139 loc) · 5.36 KB
/
testInstall.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
#!/usr/bin/env node
"use strict";
const os = require('os');
const path = require('path');
const fs = require('fs');
const spawnSync = require('child_process').spawnSync;
const del = require('del').sync;
const versions = ['6', '8', '10', '11'];
const tmpdir = os.tmpdir();
function directoryExists(file) {
try {
const stat = fs.lstatSync(file);
return stat.isDirectory();
} catch (err) {
return false;
}
}
function fileExists(file) {
try {
const stat = fs.lstatSync(file);
return stat.isFile();
} catch (err) {
return false;
}
}
function removeFolder(dir) {
if (!fs.existsSync(dir)) return;
fs.readdirSync(dir).forEach((file) => {
const curPath = dir + path.sep + file;
if (fs.lstatSync(curPath).isDirectory())
removeFolder(curPath);
else
fs.unlinkSync(curPath);
});
fs.rmdirSync(dir);
}
const tempInstallPath = path.resolve(tmpdir, 'chromedriver-test');
if (directoryExists(tempInstallPath)) {
console.log(`Deleting directory '${tempInstallPath}'.`);
removeFolder(tempInstallPath);
}
fs.mkdirSync(tempInstallPath);
function checkSpawn(spawnInfo) {
if (spawnInfo.stdout) {
if (typeof (spawnInfo.stdout) !== 'string')
console.log(spawnInfo.stdout.toString('utf8'));
else
console.log(spawnInfo.stdout);
}
if (spawnInfo.stderr) {
if (typeof (spawnInfo.error) !== 'string')
console.error(spawnInfo.stderr.toString('utf8'));
else
console.error(spawnInfo.stderr);
}
if (spawnInfo.status !== 0 || spawnInfo.error) {
console.error('Failed when spawning.');
process.exit(1);
}
if (typeof (spawnInfo.stdout) !== 'string')
return spawnInfo.stdout.toString('utf8');
else
return spawnInfo.stdout;
}
function nvmUse(version) {
const versionsText = os.platform() === 'win32'
? checkSpawn(spawnSync('nvm', ['list']))
: checkSpawn(spawnSync('/bin/bash', ['-c', `source $HOME/.nvm/nvm.sh && nvm version ${version}`]));
const versionsAvailable = versionsText.split('\n').map(v => v.match(/\d+\.\d+\.\d+/)).filter(v => v).map(v => v[0]);
const largestMatch = versionsAvailable.filter(v => v.match(`^${version}\\.`)).map(v => v.match(/\d+\.(\d+)\.\d+/)).reduce(((max, v) => max[1] > v[1] ? max : v), [null, 0]);
if (largestMatch.length === 0) {
console.error(`Version '${version}' not found.`);
process.exit(3);
}
const largestMatchingVersion = largestMatch.input;
console.log(`Found version '${largestMatchingVersion}'.`);
if (os.platform() === 'win32')
checkSpawn(spawnSync('nvm', ['use', largestMatchingVersion]));
else
checkSpawn(spawnSync('/bin/bash', ['-c', `source $HOME/.nvm/nvm.sh && nvm use ${largestMatchingVersion}`]));
}
function sleep(milliseconds) {
const inAFewMilliseconds = new Date(new Date().getTime() + milliseconds);
// eslint-disable-next-line no-empty
while (inAFewMilliseconds > new Date()) { }
}
const packedFile = path.resolve(tempInstallPath, 'chromedriver.tgz');
function pack() {
del(path.resolve(__dirname, '*.tgz'));
if (os.platform() === 'win32') {
checkSpawn(spawnSync('cmd.exe', ['/c', `npm pack`], { cwd: __dirname }));
} else {
checkSpawn(spawnSync('npm', ['pack'], { cwd: __dirname }));
}
const fileNames = fs.readdirSync(__dirname).filter(f => f.endsWith(".tgz"));
if (fileNames.length !== 1) {
console.error("Could not find packed file.");
process.exit(3);
}
fs.renameSync(fileNames[0], packedFile);
}
pack();
for (const version of versions) {
console.log(`Testing version ${version}...`);
const tempInstallPathForVersion = path.resolve(tempInstallPath, version);
fs.mkdirSync(tempInstallPathForVersion);
nvmUse(version);
if (os.platform() === 'win32') {
sleep(2000); // wait 2 seconds until everything is in place
checkSpawn(spawnSync('cmd.exe', ['/c', `npm i --no-progress --chromedriver-force-download --no-save --no-audit --no-package-lock ${packedFile}`], { cwd: tempInstallPathForVersion }));
checkFile(tempInstallPathForVersion, version);
del(tempInstallPathForVersion, { force: true });
fs.mkdirSync(tempInstallPathForVersion);
checkSpawn(spawnSync('cmd.exe', ['/c', `npm i --no-progress --no-save --no-audit --no-package-lock ${packedFile}`], { cwd: tempInstallPathForVersion }));
checkFile(tempInstallPathForVersion, version);
} else {
checkSpawn(spawnSync('npm', ['i', '--no-progress', '--chromedriver-force-download', '--no-save', '--no-audit', '--no-package-lock', `${packedFile}`], { cwd: tempInstallPathForVersion }));
checkFile(tempInstallPathForVersion, version);
del(tempInstallPathForVersion, { force: true });
fs.mkdirSync(tempInstallPathForVersion);
checkSpawn(spawnSync('npm', ['i', '--no-progress', '--no-save', '--no-audit', '--no-package-lock', `${packedFile}`], { cwd: tempInstallPathForVersion }));
checkFile(tempInstallPathForVersion, version);
}
}
function checkFile(tempInstallPathForVersion, version) {
const executable = path.resolve(tempInstallPathForVersion, 'node_modules', 'chromedriver', 'lib', 'chromedriver', `chromedriver${os.platform() === 'win32' ? '.exe' : ''}`);
if (fileExists(executable)) {
console.log(`Version ${version} installed fine.`);
}
else {
console.error(`Version ${version} did not install correctly, file '${executable}' was not found.`);
process.exit(2);
}
}
try {
removeFolder(tempInstallPath);
} catch (err) {
console.error(`Could not delete folder '${tempInstallPath}'.`);
}