This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
88 lines (78 loc) · 2.35 KB
/
index.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
// Copyright 2020 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// http://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
// modified, or distributed except according to those terms. Please review the Licences for the
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.
const downloader = require('electron-download-fork');
const unzip = require('unzip');
const os = require('os');
const fs = require('fs');
const path = require('path');
function checkError(err) {
if(err) {
console.log(err);
throw err
}
}
// wrap the downloader, replacing the filename
// with our custom version and unzip into target
// folder
module.exports = (options, cb) => {
if (!options.version) {
throw Error("You must specify a version!")
}
const getPlatform = () => {
switch (os.platform()) {
case 'linux':
return 'linux';
case 'win32':
return 'win';
case 'darwin':
return 'osx';
default:
return 'unknown';
}
};
const getArch = () => {
switch (os.arch()) {
case 'x64':
return 'x64';
case 'ia32':
return 'x86';
default:
return 'unknown';
}
};
const opts = Object.assign({
'arch': getArch(),
'platform': getPlatform(),
'targetDir': process.cwd()
}, options);
const prefix = opts.filename;
const filename = prefix + "-" + opts.version + "-" + opts.platform + "-" + opts.arch + '.zip';
const targetFilePattern = new RegExp(options.filePattern || '^(' + prefix + '|(lib)' + prefix +'.*\.(dll|so|dylib))$');
opts.customFilename = filename;
return downloader(opts, (err, zipPath) => {
checkError(err);
let targetFiles = [];
return fs.createReadStream(zipPath)
.pipe(unzip.Parse())
.on('entry', entry => {
const fileName = path.basename(entry.path);
const type = entry.type; // 'Directory' or 'File'
if (type == 'File' && fileName.match(targetFilePattern)) {
// only put the specific files into our target dir
targetFiles.push(fileName)
entry.pipe(fs.createWriteStream(path.join(options.targetDir || ".", fileName)));
} else {
entry.autodrain();
}
}).on('finish', () => {
// inform callback what we extracted
cb(targetFiles, opts);
});
});
}