-
Notifications
You must be signed in to change notification settings - Fork 19
/
deck.js
69 lines (64 loc) · 1.54 KB
/
deck.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
"use strict";
const fs = require("fs");
const request = require("request");
const URL = require("url");
const dl = require("deck-lightning");
const opts = {
delaySeconds: 90
};
function getGist (url, cb) {
const id = URL.parse(url).pathname.split("/").pop();
request.get(`https://api.github.com/gists/${id}`, { headers: { "user-agent": "request" } }, function (err, res, body) {
if (err) {
cb(err);
}
else if (res.statusCode !== 200) {
cb(new Error(`Unexpected http response: ${res.statusCode}`));
}
else {
cb(null, body);
}
});
}
function extractContentOfFile (res, filename) {
return JSON.parse(res).files[filename].content;
}
function saveContentLocally (filename, data, cb) {
fs.writeFile(filename, data, "utf8", cb);
}
function saveAllRemoteGists (items) {
return Promise.all(items.map(function (item) {
return new Promise(function (resolve, reject) {
getGist(item.url, function (err, res) {
if (err) {
return reject(err);
}
else {
const content = extractContentOfFile(res, item.filename);
saveContentLocally(item.filename, content, function (err) {
if (err) {
reject(err);
}
else {
resolve();
}
});
}
});
});
}));
}
module.exports = function (items, cb) {
saveAllRemoteGists(items.filter(function (item) { return typeof item === "object"; }))
.then(function () {
opts.files = items.map(function (item) {
if (typeof item === "string") {
return item;
}
else {
return item.filename;
}
});
dl(opts, cb);
});
};