forked from outmoded/hapijs.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·195 lines (135 loc) · 4.82 KB
/
build.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node
var Fs = require('fs');
var Github = require('./lib/github');
var Jade = require('jade');
var Markdown = require('./lib/markdown');
var Npm = require('./lib/npm');
var Path = require('path');
// input paths
var templatesPath = Path.join(__dirname, 'templates');
// the output path
var cachePath = Path.join(__dirname, '.cache');
var finalPath = Path.join(__dirname, 'output');
var error = function (err) {
console.error(err.message);
process.exit(1);
};
// wrapper to take callback output and write it to a file
var writeFile = function (file) {
// hack to make sure paths are friendlier to write
var isJson = /\.json$/.test(file);
var filePath = Path.join(isJson ? cachePath : finalPath, file);
return function (err, data) {
if (err) {
return error(err);
}
Fs.writeFileSync(filePath, isJson ? JSON.stringify(data) : data, 'utf8');
};
};
var targets = {};
targets['issues.json'] = function (file) {
Github.issues(writeFile(file));
};
targets['weeklyIssues.json'] = function (file) {
Github.weeklyIssues(writeFile(file));
};
targets['commits.json'] = function (file) {
Github.commits(writeFile(file));
};
targets['weeklyCommits.json'] = function (file) {
Github.weeklyCommits(writeFile(file));
};
targets['tags.json'] = function (file) {
Github.tags(writeFile(file));
};
targets.apiCache = function (tag) {
Github.api(tag, function (err, content) {
if (err) {
return error(err);
}
content.tag = tag;
Fs.writeFileSync(Path.join(cachePath, 'api', tag + '.json'), JSON.stringify(content), 'utf8');
});
};
targets.api = function (tag, file) {
var locals = require('./.cache/api/' + tag + '.json');
locals.tags = require('./.cache/tags.json');
Jade.renderFile(Path.join(templatesPath, 'api.jade'), locals, writeFile(file));
};
targets['downloads.json'] = function (file) {
Npm.downloads(writeFile(file));
};
targets['changelog.json'] = function (file) {
Github.changelog(writeFile(file));
};
targets.tutorial = function (title, file) {
var tutorials = require('./lib/tutorials');
Markdown.file(Path.join(__dirname, 'tutorials', title + '.md'), function (err, content) {
if (err) {
return error(err);
}
tutorials.html = content.html;
tutorials.title = 'Tutorials | ' + content.title;
tutorials.active = title;
Jade.renderFile(Path.join(templatesPath, 'tutorial.jade'), tutorials, writeFile(file));
});
};
targets['index.html'] = function (file) {
var locals = require('./lib/locals');
locals.companies = require('./lib/community');
Markdown.file(Path.join(__dirname, 'index.md'), function (err, content) {
if (err) {
return error(err);
}
locals.guide = content.html;
Jade.renderFile(Path.join(templatesPath, 'index.jade'), locals, writeFile(file));
}, false, true);
};
targets['community.html'] = function (file) {
Jade.renderFile(Path.join(templatesPath, 'community.jade'), require('./lib/community'), writeFile(file));
};
targets['hapidays.html'] = function (file) {
Jade.renderFile(Path.join(templatesPath, 'hapidays.jade'), writeFile(file));
};
targets['help.html'] = function (file) {
Jade.renderFile(Path.join(templatesPath, 'help.jade'), writeFile(file));
};
targets['updates.html'] = function (file) {
var locals = require('./lib/locals');
locals.changelog = require('./.cache/changelog.json');
Jade.renderFile(Path.join(templatesPath, 'updates.jade'), locals, writeFile(file));
};
targets['plugins.html'] = function (file) {
var Plugins = require('./lib/plugins');
Jade.renderFile(Path.join(templatesPath, 'plugins.jade'), Plugins, writeFile(file));
};
targets['404.html'] = function (file) {
Jade.renderFile(Path.join(templatesPath, '404.jade'), writeFile(file));
};
targets['500.html'] = function (file) {
Jade.renderFile(Path.join(templatesPath, '500.jade'), writeFile(file));
};
targets['tags.mk'] = function () {
var tags = require(Path.join(cachePath, 'tags.json')).map(function (tag) {
return tag.name;
});
var mk = 'all: $(addprefix output/api/,$(addsuffix .html,' + tags.join(' ') + ' index))\n';
Fs.writeFileSync(Path.join(cachePath, 'tags.mk'), mk, 'utf8');
};
var target = process.argv[2];
if (targets[target]) {
targets[target](target);
}
else if (/^api\/[a-zA-Z0-9\.\-]+\.json$/.test(target)) {
targets.apiCache(target.replace(/^api\/|\.json$/g, ''), target);
}
else if (/^api\/[a-zA-Z0-9\.\-]+\.html$/.test(target)) {
targets.api(target.replace(/^api\/|\.html$/g, ''), target);
}
else if (/^tutorials\//.test(target)) {
targets.tutorial(target.replace(/^tutorials\/|\.html$/g, ''), target);
}
else {
console.error('Unknown target: ' + target);
process.exit(1);
}