-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupdate-package.js
102 lines (88 loc) · 2.31 KB
/
update-package.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
const http = require("http");
const fs = require("fs");
const path = require("path");
function getJSON(URL, cb) {
http.get(URL, res => {
let body = "";
res.on("data", function(chunk) {
body += chunk;
});
res.on("end", function() {
try {
cb(null, JSON.parse(body));
} catch (e) {
cb(e);
}
});
});
}
const scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$')
function urlFriendly(name) {
return name === encodeURIComponent(name);
}
function validScopedName(name) {
const nameMatch = name.match(scopedPackagePattern)
if (nameMatch) {
return urlFriendly(nameMatch[1]) && urlFriendly(nameMatch[1])
}
}
function validName(name) {
return name.length > 0 && (urlFriendly(name) || validScopedName(name));
}
function sanitize(name) {
return name.trim(); // validName will filter out anything else that is a problem
}
function getPackageList(cb) {
getJSON(
"http://anvaka.github.io/npmrank/online/npmrank.json",
(err, json) => {
if (err) {
return cb(err);
}
if (json && json.rank) {
cb(null, Object.keys(json.rank).map(sanitize).filter(validName));
} else {
cb(new Error("Malformed data"));
}
}
);
}
function getVersion() {
const date = new Date();
const monthS = ("" + (date.getMonth() + 1)).padStart(2, "0");
const dayS = ("" + date.getDate()).padStart(2, "0");
return `${date.getFullYear()}.${monthS}.${dayS}`;
}
function packageEntry(name) {
return ` "${name}": "latest"`;
}
function buildJSON(packageList) {
// Some packages had to be left behind, to cirumvent
// npm ERR! code E400
// npm ERR! Too many dependencies. : no-one-left-behind
return `
{
"name": "no-one-left-behind",
"version": "${getVersion()}",
"description": "Every package is invited",
"repository": "Zalastax/no-one-left-behind",
"scripts": {
"update-packages": "node update-package.js",
"deploy": "node deploy.js"
},
"license": "MIT",
"dependencies": {
${packageList.slice(0, 1000).map(packageEntry).join(",\n")}
}
}
`;
}
getPackageList((err, packageList) => {
if (err) {
console.log(err);
process.exit(1);
}
const out = buildJSON(packageList);
const file = path.join(__dirname, "package.json");
fs.writeFileSync(file, out);
});