-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (122 loc) · 4.32 KB
/
index.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
var async = require('async');
var fs = require('fs-extra');
var marked = require('marked');
var request = require('request');
var util = require('util');
var archiver = require('archiver');
var output = fs.createWriteStream('docs.zip');
var archive = archiver('zip');
output.on('close', function () {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
// copy dosc.zip to nodenicausercontent
fs.copySync('./docs.zip', '/home/polin/github/nodenica/nodenicausercontent/dist/docs.zip');
// copy index.json
fs.copySync('./dist/index.json', '/home/polin/github/nodenica/nodenicausercontent/index.json');
});
archive.on('error', function(err){
throw err;
});
archive.pipe(output);
var url = {
dist: 'https://nodejs.org/dist/%s',
github: 'https://raw.githubusercontent.com/nodejs/node/%s/doc/api/%s.md',
};
// create dist directory if not exists
try {
fs.accessSync('./dist', fs.R_OK | fs.W_OK);
} catch (e) {
fs.mkdirSync('./dist');
}
// copy assets to dist
fs.copySync('./assets', './dist/assets');
// check list of nodejs versions
request(util.format(url.dist, 'index.json'), function (error, response, body) {
if (!error && response.statusCode == 200) {
var version = JSON.parse(body)[0].version;
var branch = util.format('%s.x', version.substring(0, 2));
console.log('node:', version);
console.log('branch:', branch);
// get list of files
request(util.format(url.dist, util.format('%s/docs/api/index.json', version)), function (error, response, body) {
if (!error && response.statusCode == 200) {
var items = JSON.parse(body).desc;
var modules = [];
items.forEach(function (item) {
if (item.type === 'text') {
// spit string based http://rubular.com/r/O212WaPVqA
var match = item.text.match(/\[(.*)\]\((.*)\.html\)/i);
if (match !== null) {
modules.push({
title: match[1],
name: match[2],
});
}
};
});
// instance index.json
var indexJson = {
version: version,
modules: modules,
};
// create index.json
fs.writeFile('./dist/index.json', JSON.stringify(indexJson), function (err) {
if (!err) {
console.log('✓ index.json');
}
});
// read template file
fs.readFile('./html/template.html', 'utf8', function (err, htmlTemplate) {
if (err) {
console.log('Error reading template file');
} else {
console.log('modules:', modules.length);
// download all markdown files and converts to html files
async.forEachOf(modules, function (value, key, callback) {
request(util.format(url.github, branch, value.name), function (error, response, body) {
if (!error && response.statusCode == 200) {
marked(body, function (err, content) {
if (err) {
callback(err);
} else {
var htmlContent = util.format(htmlTemplate, value.title, content);
fs.writeFile(util.format('./dist/%s.html', value.name), htmlContent, function (err) {
if (!err) {
console.log('✓', value.name);
callback();
} else {
console.log('✗', value.name);
callback(err);
}
});
}
});
} else {
callback(error);
}
});
}, function (err) {
// create zip file
archive.bulk([
{
expand: true,
cwd: 'dist',
src: [
'**/*'
],
dot: true
}
]);
archive.finalize();
console.log('done');
});
}
});
} else {
console.log('Error on load list of files from version:', version);
}
});
} else {
console.log('Error on load list of node versions');
}
});