-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·40 lines (29 loc) · 1.5 KB
/
build.js
File metadata and controls
executable file
·40 lines (29 loc) · 1.5 KB
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
#! /usr/bin/env node
const curry = require('lodash.curry');
const flip = require('lodash.flip');
const groupBy = curry(flip(require('lodash.groupby')), 2);
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const markdownRoot = process.env.npm_package_config_markdownRoot;
execAndSplitOutput = (command, separator) => execSync(command).toString().split(separator).filter(x => x);
const getMarkdownFiles = (root) => execAndSplitOutput(`find ${root} -name \\*.md`, '\n');
const groupByDirectory = groupBy(file => {
const dirName = path.dirname(file);
return dirName.substring(markdownRoot.length + 1);
});
const files = getMarkdownFiles(markdownRoot);
const fileGroups = groupByDirectory(files);
const renderGroup = (group) => [`## ${group}`, ...fileGroups[group].map(renderEntry)].join('\n');
const renderEntry = (file) => `* [${getTitle(file)}](${file}) - last modified ${getModifiedDate(file)}`;
const getTitle = (file) => execSync(`sed -n "s/^# \\(.*\\)/\\1/p" ${file} | tr -d '\\n'`).toString();
const getModifiedDate = (file) => fs.statSync(file).mtime.toLocaleDateString();
const createTOC = (fileGroups) => {
const header = fs.readFileSync('./readme.header.md');
const footer = fs.readFileSync('./readme.footer.md');
const TOC = Object.keys(fileGroups).map(renderGroup).join('\n');
return `${header}\n${TOC}\n${footer}`
}
fs.writeFileSync('./readme.md', createTOC(fileGroups))
execSync('./write_html.sh');
execSync('./update_readme_links.sh');