-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (53 loc) · 1.9 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
const debug = require('debug')('download');
const fs = require('fs');
const { exec } = require('child_process');
/**
* Download file from a given sourceUrl to a targetPath
*
* Note that a {targetPath}.part is created while downloading and is renamed to {targetPath}
* when download is complete.
*
* @param {Object} options
* @param {String} options.sourceUrl
* @param {String} options.targetPath
* @param {Boolean} [options.downloadIfExists=true]
* @param {Boolean} [options.unsafeSsl=false] disable certificate checking
* @returns {string} the target path
*/
async function download(options) {
const {
downloadIfExists = true,
unsafeSsl = false
} = options;
debug('Downloading ' + options.sourceUrl + ' to ' + options.targetPath + ' ...');
return new Promise(function (resolve, reject) {
if ((!downloadIfExists) && fs.existsSync(options.targetPath)) {
debug('File ' + options.targetPath + ' already exists');
resolve(options.targetPath);
return;
}
/* Prepare temp file to avoid problems on script interruption */
var tempPath = options.targetPath + '.part';
if (fs.existsSync(tempPath)) {
fs.unlinkSync(tempPath);
}
/* Download to tempPath, rename and resolve when download is complete */
let command = 'wget -nv ';
if (unsafeSsl) {
command += ' --no-check-certificate ';
}
command += `-O "${tempPath}" "${options.sourceUrl}"`;
debug(command);
exec(command, (error, stdout, stderr) => {
if (error) {
debug(stderr);
fs.unlinkSync(tempPath);
reject("command fail : " + command);
} else {
fs.renameSync(tempPath, options.targetPath);
resolve(options.targetPath);
}
});
});
};
module.exports = download;