-
Notifications
You must be signed in to change notification settings - Fork 0
/
gather-snips.js
43 lines (31 loc) · 1 KB
/
gather-snips.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
import { readFile, writeFile } from 'fs/promises';
import { join } from 'path';
// ---------- file path constants ----------
const SNIPS_LIVE_HERE = join('snips.txt');
const SNIP_HERE = '-snip-';
const PUBLIC_SNIPS = join('public', 'data', 'snips.json');
// ---------- build array of snips ----------
const rawSnips = await readFile(SNIPS_LIVE_HERE, 'utf-8');
const log = (thing) => (console.log(thing), thing);
const snips = rawSnips
.split(new RegExp(`${SNIP_HERE}.*`,'ig'))
.map((snip) => {
const split = snip.split('\n');
for (const line of [...split]) {
if (!line.trim()) split.shift();
else break;
}
for (const line of [...split].reverse()) {
if (!line.trim()) split.pop();
else break;
}
return split.join('\n');
})
.filter((snip) => snip);
// ---------- write snip data to public folder ----------
await writeFile(
PUBLIC_SNIPS,
JSON.stringify(snips, null),
(err) => err && console.error(err),
);
console.log('------ done gathering snips ------');