-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate-cotw.ts
More file actions
32 lines (24 loc) · 899 Bytes
/
update-cotw.ts
File metadata and controls
32 lines (24 loc) · 899 Bytes
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
import fs from 'fs';
import glob from 'glob';
import path from 'path';
// relative path to the directory into which the docs repo is cloned by the github action
const contentRepoPath = 'docs/';
const storePath = path.join(
__dirname,
contentRepoPath,
'bin/concept-of-the-week.txt'
);
const getRandomElement = (arr: any[]) =>
arr[Math.floor(Math.random() * arr.length)];
const conceptPaths = glob
.sync(path.join(contentRepoPath, 'content/*/concepts/*/*.md'))
.map((conceptPath) => conceptPath.replace(contentRepoPath, ''));
const currentConceptPath = fs.existsSync(storePath)
? fs.readFileSync(storePath)
: '';
let newConceptPath: string;
do {
newConceptPath = getRandomElement(conceptPaths);
} while (newConceptPath === currentConceptPath);
fs.writeFileSync(storePath, newConceptPath, 'utf8');
console.log(`Update successful: ${currentConceptPath} -> ${newConceptPath}`);