-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.process.js
executable file
·31 lines (23 loc) · 1001 Bytes
/
README.process.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
#!/usr/bin/env node
// npm run update-docs
// script that clumsily processes README to insert logged values as comments
// at marked locations
const [fs, intercept] = ['fs', 'intercept-stdout'].map(lib => require(lib));
const marker = /\/\*\*\//g;
const jsSections = /```\s*js\b([\s\S]+?)```/gi;
const separator = '--- 39e8e56c-1e57-4bb8-aee0-3df80b1a9fc6 ---';
const source = fs.readFileSync('README.source.md', 'utf-8');
const linePrefix = '// ';
const target = source.replace(jsSections, (match, js) => {
let sections = [''];
let instrumentedJs = js.replace(marker, `; console.log("${separator}");`);
const unintercept = intercept(line => {
if (line == separator + '\n') sections.push('');
else sections[sections.length - 1] += line.replace(/^(?!$)/gm, linePrefix);
});
eval(instrumentedJs);
unintercept();
let substitutedJS = js.replace(marker, () => sections.shift().trimRight());
return '```js' + substitutedJS + '```';
});
fs.writeFileSync('README.md', target);