forked from ronaldlokers/grunt-casperjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
98 lines (85 loc) · 2.93 KB
/
Copy pathinstall.js
File metadata and controls
98 lines (85 loc) · 2.93 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
'use strict'
var cp = require('child_process')
var fs = require('fs')
var http = require('http')
var https = require('https')
var path = require('path')
var url = require('url')
var rimraf = require('rimraf').sync
var AdmZip = require('adm-zip')
fs.existsSync = fs.existsSync || path.existsSync
var libPath = path.join(__dirname, 'lib', 'casperjs')
var tmpPath = path.join(__dirname, 'tmp')
var downloadUrl = 'https://github.com/n1k0/casperjs/archive/1.0.3.zip'
function isCasperInstalled(notInstalledCallback) {
findInstalledCasper(function(path, version) {
if (path) {
console.log("Use already installed Casperjs.");
console.log(" Version: " + version);
console.log(" Path: " + path);
fs.symlinkSync(path, './casperjs');
} else {
console.log("Casperjs not installed. Installing.");
notInstalledCallback();
}
});
}
function findInstalledCasper(foundCallback) {
// Note that "which" doesn't work on windows...
cp.exec("which casperjs", function(error, path, stderr) {
if (path) {
cp.exec("casperjs --version", function(error, version, stderr) {
foundCallback(path.trim(), version.trim());
});
} else {
foundCallback();
}
});
}
function tidyUp() {
rimraf(tmpPath);
}
function unzipTheZippedFile() {
var zip = new AdmZip(path.join(tmpPath, 'archive.zip'));
zip.extractAllTo(libPath, true);
if (process.platform != 'win32') {
var pathToCommand = path.join(libPath, 'casperjs-1.0.3', 'bin', 'casperjs');
fs.symlinkSync(pathToCommand, './casperjs');
var stat = fs.statSync(pathToCommand);
if (!(stat.mode & 64)) {
fs.chmodSync(pathToCommand, '755')
}
}
tidyUp();
}
function downloadZipFromGithub() {
var file = fs.createWriteStream(path.join(tmpPath, "archive.zip"));
var lengthSoFar = 0;
var request = https.get(downloadUrl, function(response) {
if (response.statusCode === 301 || response.statusCode === 302) {
downloadUrl = response.headers.location;
downloadZipFromGithub();
} else {
response.pipe(file);
response.on('data', function(chunk) {
console.log('Receiving ' + Math.floor((lengthSoFar += chunk.length) / 1024) + 'K...' );
}).
on('end', unzipTheZippedFile).
on('error', function(e) {
console.log('An error occured whilst trying to download Casper.JS ' + e.message);
tidyUp();
});
}
});
request.on('error', function(e) {
console.log('An error occured whilst trying to download Casper.JS ' + e.message);
tidyUp();
});
}
isCasperInstalled(function() {
if (!fs.existsSync(tmpPath)) {
fs.mkdirSync(tmpPath);
}
rimraf(libPath);
downloadZipFromGithub();
});