-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
82 lines (54 loc) · 2.52 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
#!/usr/bin/env node
const { execSync, spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const allPaths = fs.readdirSync('./');
const bookReviews = [];
allPaths.forEach((somePath) => {
// console.log('somePath', somePath);
const readmeFilePath = path.join(somePath, './README.adoc');
const indexFilePath = path.join(somePath, './index.html');
if (fs.existsSync(readmeFilePath)) {
const readmeContent = fs.readFileSync(readmeFilePath, { encoding: 'utf8' });
const lines = readmeContent.split('\n');
const title = (lines[0].length > 0 ? lines[0].substr(2) : lines[1].substr(2)).trim();
bookReviews.push({
path: somePath,
title,
});
console.log(title);
console.log('Converting AsciiDoc to HTML...', somePath);
const cmd = 'asciidoctor';
const argv = ['-o', indexFilePath, readmeFilePath];
const formattedCommand = cmd + ' ' + argv.join(' ');
execSync(formattedCommand, { stdio: 'inherit' });
}
});
const bookReviewMarkdownListLinks = bookReviews
.sort((a, b) => {
return a.title - b.title;
})
.map((bookReview) => {
return `* [${bookReview.title}](./${bookReview.path}/)`;
});
// console.log(bookReviewMarkdownListLinks);
console.log('Generating list of book reviews...');
const readmeFilePath = './README.md';
const readmeContent = fs.readFileSync(readmeFilePath, { encoding: 'utf8' });
const readmeBookReviewsStartTag = '<!--book-reviews:begin-->';
const readmeBookReviewsEndTag = '<!--book-reviews:end-->';
const readmeCurrentBookReviewsStartIx = readmeContent.indexOf(readmeBookReviewsStartTag);
const readmeCurrentBookReviewsEndIx = readmeContent.indexOf(readmeBookReviewsEndTag, readmeCurrentBookReviewsStartIx) + readmeBookReviewsEndTag.length;
const readmeCurrentBookReviews = readmeContent.substr(readmeCurrentBookReviewsStartIx, readmeCurrentBookReviewsEndIx - readmeCurrentBookReviewsStartIx);
// console.log('');
// console.log('Current reviews:');
// console.log(readmeCurrentBookReviews);
const readmeNewBookReviews = `${readmeBookReviewsStartTag}
${bookReviewMarkdownListLinks.join('\n')}
${readmeBookReviewsEndTag}`;
// console.log('');
// console.log('New reviews:');
// console.log(readmeNewBookReviews);
const readmeNewContent = readmeContent.replace(readmeCurrentBookReviews, readmeNewBookReviews);
fs.writeFileSync(readmeFilePath, readmeNewContent, { encoding: 'utf8' });
console.log('Book reviews successfully generated!');