-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
62 lines (58 loc) · 2.15 KB
/
app.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
var fs = require('fs-extra');
var _ = require('lodash');
var request = require('urllib-sync').request;
var storageDir = process.argv[2];
console.log('operating on storage folder: ' + storageDir);
function downloadPackage(folder, distUrl, packageObj) {
try {
var fp = storageDir + '/' + folder + '/' + distUrl;
var exists = fs.existsSync(fp);
if (exists) {
console.log("Skipped, already exists.");
return true;
}
var res = request(packageObj._distfiles[distUrl].url, {
timeout: 60000 * 10
});
fs.writeFileSync(storageDir + '/' + folder + '/' + distUrl, res.data);
console.log('finished ' + distUrl);
return true;
} catch (err) {
console.log('timeout:'+err);
return false;
}
}
fs.readdir(storageDir, function (err, folders) {
if (err) {
console.log('error:' + err);
return;
}
folders.forEach(function (folder) {
fs.readdir(storageDir + '/' + folder, function (err, filesInModuleFolder) {
if (err) {
console.log('error:'+err);
return;
}
if (filesInModuleFolder.length > 0) {
var packageFilePath = storageDir + '/' + folder + '/package.json';
fs.readJson(packageFilePath, function (err, packageObj) {
if (err) {
console.log('error reading file as package json: ' + packageFilePath + ", error:"+err);
return;
}
if (!packageObj._distfiles) {
console.log('no distfiles in package json of ' + folder);
} else {
for (var distUrl in packageObj._distfiles) {
console.log('downloading ' + packageObj._distfiles[distUrl].url );
var downloadSuccessful;
do {
downloadSuccessful = downloadPackage(folder, distUrl, packageObj);
} while (!downloadSuccessful);
}
}
});
}
});
});
});