forked from oss-generic/process
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
127 lines (117 loc) · 3.5 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
'use strict';
const assert = require('assert');
const path = require('path');
const asciidoctor = require('asciidoctor.js')();
const fs = require('fs-extra');
const glob = require('glob');
/** Directory where asciidoctor stylesheets are stored. */
const stylesDir = path.join(__dirname, 'styles');
/** Destination directory. */
const destDir = 'dist';
/** `glob` paths to ignore */
const pathsToIgnore = [
'node_modules',
'node_modules/**',
`${destDir}`,
`${destDir}/**`,
'package.json',
];
if (require.main === module) {
fs.remove(destDir).then(buildAllFiles).catch(onError);
}
/**
* Builds all files in the current directory and all of its subdirectories.
*/
async function buildAllFiles() {
const filenames = glob.sync('**/*', {
ignore: pathsToIgnore,
nodir: true,
});
for (const filename of filenames) {
await buildFile(filename);
}
}
/**
* Builds a file.
*
* @param {string} filename - The file to build. Must not be an ignored file.
*/
async function buildFile(filename) {
if (filename.endsWith('.adoc')) {
await asciidocToHtml(filename);
} else {
await copyMediaFile(filename);
}
}
/**
* Renders an asciidoc file into a HTML file.
*
* @param {string} adocFilename - The asciidoc file to render.
*/
async function asciidocToHtml(adocFilename) {
console.log('asciidocToHtml', adocFilename);
const relativeStylesDir = path.relative(path.dirname(adocFilename), stylesDir);
const asocInput = await fs.readFile(adocFilename, 'utf8');
const htmlOutput = asciidoctor.convert(asocInput, {
safe: 'safe',
doctype: 'article',
header_footer: true,
attributes: [
'linkcss',
`stylesdir=${relativeStylesDir}`,
'experimental',
'sectlinks',
'idprefix=',
'idseparator=-',
'source-highlighter=highlightjs',
],
base_dir: path.dirname(adocFilename),
});
const outputFilename = path.join(destDir, path.dirname(adocFilename), path.basename(adocFilename, '.adoc')) + '.html';
await fs.mkdirs(path.dirname(outputFilename));
if (path.basename(adocFilename) === 'README.adoc') {
const indexOutputFilename = path.join(destDir, path.dirname(adocFilename), 'index.html');
await fs.writeFile(indexOutputFilename, htmlOutput);
await writeRedirectPage(outputFilename);
} else {
await fs.writeFile(outputFilename, htmlOutput);
}
}
/**
* Writes a redirect page to `filename` that redirects to `index.html`.
*
* @param {string} filename - File to write the redirect page to.
*/
async function writeRedirectPage(filename) {
const content = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url=index.html">
</head>
<body>
Redirecting to <a href="index.html">index.html</a>
</body>
</html>
`.trim();
await fs.writeFile(filename, content);
}
/**
* Copy a media file to the destination directory.
*
* @param {string} mediaFilename - Media filename to copy.
*/
async function copyMediaFile(mediaFilename) {
console.log('copyMediaFile', mediaFilename);
const outputFilename = path.join(destDir, mediaFilename);
await fs.mkdirs(path.dirname(outputFilename));
await fs.copy(mediaFilename, outputFilename);
}
/**
* Fatal error handler.
*/
function onError(err) {
console.error(err);
process.exit(1);
}