This repository was archived by the owner on Feb 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall-libxl.js
77 lines (71 loc) · 1.96 KB
/
install-libxl.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
var fs = require('fs'),
http = require('http'),
os = require('os'),
path = require('path'),
spawn = require('child_process').spawn;
var isWin = !!os.platform().match(/^win/),
isX64 = !!os.arch().match(/64$/),
dependencyDir = 'deps',
libxlDir = path.join(dependencyDir, 'libxl'),
archiveUrl = 'http://www.libxl.com/download/libxl' + (isWin ? '.zip' : '.tar.gz'),
archiveFile = path.join(dependencyDir, path.basename(archiveUrl));
var download = function(url, file, callback) {
var writer = fs.createWriteStream(file);
http.get(url, function(response) {
response.pipe(writer);
response.on('end', function() {
callback();
});
});
};
var execute = function(cmd, args, callback) {
spawn(cmd, args).on('close', function(code) {
if (0 === code) {
callback();
} else {
process.exit(1);
}
});
};
var extractor = function(file, target, callback) {
if (isWin) {
execute(path.join('tools', '7zip', '7za.exe'), ['x', archiveFile, '-o' + dependencyDir], callback);
} else {
execute('tar', ['-C', dependencyDir, '-zxf', archiveFile], callback);
}
};
var finder = function(dir, pattern) {
var files = fs.readdirSync(dir),
i,
file;
for (i = 0; i < files.length; i++) {
file = files[i];
if (file.match(pattern)) {
return path.join(dir, file);
}
}
return null;
};
var register = function() {
var sharedObject = path.join(
libxlDir,
(isWin ? 'bin' : 'lib') + ((!isWin && isX64) ? '64' : ''),
'libxl' + (isWin ? '.dll' : '.so')
);
if (!isWin) {
execute('sudo cp', [sharedObject, '/usr/lib']);
}
};
if (fs.existsSync(libxlDir)) {
process.exit(0);
}
if (!fs.existsSync(dependencyDir)) {
fs.mkdirSync(dependencyDir);
}
download(archiveUrl, archiveFile, function() {
extractor(archiveFile, dependencyDir, function() {
fs.unlinkSync(archiveFile);
fs.renameSync(finder(dependencyDir, /^libxl/), libxlDir);
register();
});
});