-
Notifications
You must be signed in to change notification settings - Fork 6
/
grabem.js
138 lines (118 loc) · 3.94 KB
/
grabem.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*jslint node: true, nomen: true */
/*eslint-env node*/
/*eslint no-console: 0 */
"use strict";
var request = require("request"),
DecompressZip = require("decompress-zip"),
fs = require("fs"),
path = require("path"),
async = require("async"),
rimraf = require("rimraf"),
_ = require("lodash");
var REGISTRY_BASE = "https://s3.amazonaws.com/extend.brackets",
REGISTRY_URL = REGISTRY_BASE + "/registry.json",
lastRegistry = {},
DOWNLOADS = "downloads",
ZIPS = path.join(DOWNLOADS, "zips"),
REGISTRY = path.join(DOWNLOADS, "registry.json");
if (!fs.existsSync(DOWNLOADS)) {
console.log("Creating downloads directory");
fs.mkdirSync(DOWNLOADS);
fs.mkdirSync(ZIPS);
}
if (fs.existsSync(REGISTRY)) {
console.log("Reading previously downloaded registry");
lastRegistry = JSON.parse(fs.readFileSync(REGISTRY));
}
console.log("Downloading latest registry");
function getZipPath(name, version) {
return path.join(ZIPS, name + "-" + version + ".zip");
}
function doExpansion(source, destination, complete) {
console.log("Unpacking", source);
var unzipper = new DecompressZip(source);
unzipper.on("error", function (err) {
console.error("Problem unpacking", source);
console.error(err);
complete();
});
unzipper.on("extract", function () {
console.log("Extraction complete");
complete();
});
unzipper.extract({
path: destination
});
}
function deleteOldAndExpand(name, version, complete) {
var source = getZipPath(name, version),
destination = path.join(DOWNLOADS, name);
if (fs.existsSync(destination)) {
console.log("Erasing old ", destination);
rimraf(destination, function (err) {
if (err) {
console.error("Problem erasing ", destination);
complete();
return;
}
doExpansion(source, destination, complete);
});
} else {
doExpansion(source, destination, complete);
}
}
function downloadAndExpand(name, version, complete) {
var url = REGISTRY_BASE + "/" + name + "/" + name + "-" + version + ".zip",
destination = getZipPath(name, version);
console.log("Downloading", url);
request.get(url)
.on("error", function (err) {
console.log("Error downloading", url);
console.error(err);
complete();
return;
})
.pipe(fs.createWriteStream(destination))
.on("finish", function () {
deleteOldAndExpand(name, version, complete);
});
}
function saveRegistry(registry, complete) {
console.log("Saving registry");
fs.writeFile(REGISTRY, JSON.stringify(registry), complete);
}
request.get({
url: REGISTRY_URL,
gzip: true,
json: true
}, function (err, response, registry) {
if (err) {
console.log("Error downloading registry");
console.error(err);
return;
}
var tasks = [];
if (response.statusCode !== 200) {
console.log("Unexpected response", response.statusCode);
console.log(registry);
return;
}
Object.keys(registry).forEach(function (name) {
var version = registry[name].metadata.version,
zipfile = getZipPath(name, version),
zipExists = fs.existsSync(zipfile);
if (!lastRegistry[name] || lastRegistry[name].metadata.version !== version || !zipExists) {
if (zipExists) {
tasks.push(_.partial(deleteOldAndExpand, name, version));
} else {
tasks.push(_.partial(downloadAndExpand, name, version));
}
}
});
console.log(tasks.length, "packages to update");
async.series(tasks, function () {
saveRegistry(registry, function () {
console.log("Update complete");
});
});
});