forked from bahmutov/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-code-snippets.js
77 lines (68 loc) · 2.2 KB
/
update-code-snippets.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
// updates code snippets from remote repo
// needs to run in the secondary DevTools window (undocked)
// see "Updating local code snippets"
var source = localStorage.scriptSnippets;
if (typeof source === 'undefined') {
throw new Error('Cannot find scriptSnippets, are you running in the secondary DevTools?\n' +
'see https://github.com/bahmutov/code-snippets#updating-local-code-snippets');
}
var snippets = JSON.parse(source);
console.log('I have', snippets.length, 'code snippets');
console.table(snippets);
// read code snippets from this repo via RawGit.com
var repo = 'https://rawgit.com/bahmutov/code-snippets/master/';
function fetch(url) {
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
resolve(request.responseText);
} else {
reject(request.responseText);
}
};
request.onerror = function (err) {
reject(err);
};
request.send();
});
}
var updated = [];
function updateSnippet(k, id, filename) {
return fetch(repo + filename)
.then(function (source) {
console.log('fetched new source for', id, filename);
// console.log(source);
updated.push({
index: k,
id: id,
content: source,
name: filename
});
}, function () {
console.error('cannot find remote for', filename);
});
}
function chainSnippet(chain, snippet, k) {
return chain.then(function () {
return updateSnippet(k, snippet.id, snippet.name);
});
}
var allChecked = snippets.reduce(chainSnippet, Promise.resolve());
allChecked.then(function () {
console.log('fetched', updated.length, 'snippets');
updated.forEach(function (update) {
var snippet = snippets[update.index];
console.assert(update.name === snippet.name,
'name mismatch for update', update, snippet);
snippet.content = update.content;
});
if (updated.length) {
localStorage.scriptSnippets = JSON.stringify(snippets);
console.table(updated);
console.log('please reopen DevTools to load updated code snippets');
} else {
console.log('nothing to update.');
}
});