-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-readme.js
executable file
·54 lines (44 loc) · 1.78 KB
/
build-readme.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
#! /usr/bin/env node
const README_FILE_TEMPLATE = "./README_TEMPLATE.md";
const CHALLENGES_DETAILS_FILE = "./challenges.json";
const HOST_URL = "http://dev-challenges.vercel.app/"
const OUTPUT_FILE = "./README.md";
const fs = require('fs');
let content = fs.readFileSync(README_FILE_TEMPLATE, {
encoding: 'utf-8'
})
const CHALLENGES_DATA = JSON.parse(
fs.readFileSync(CHALLENGES_DETAILS_FILE, {
encoding: 'utf-8'
})
);
/**
* This function creates a table from the data from the
* CHALLENGES_DETAILS_FILE
* @param {Array<Object>} challenges
* @exports String The generated table
*/
function createChallengeTable(challenges) {
const lines = [
"| Challenge Title | Demo | Status |",
"| --------------- | ---- | ------ |"
]
/**
* Creates a row for a specified challenge
* @param {Object} challenge
* @exports String The generated row for the challenge
*/
const createChallengeRow = (challenge) => {
const formatName = str => str.split('-').map(word => word[0].toUpperCase() + word.slice(1)).join(' ');
let demoUrl = HOST_URL + (challenge.challengeDirectory || challenge.challengeTitle);
return `| ${formatName(challenge.challengeTitle)} | [Demo](https://dev-challenges.vercel.app/${demoUrl}) | ${formatName(challenge.status)} |`;
}
challenges.map(createChallengeRow).forEach(row => lines.push(row));
return lines.join('\n');
}
let challengeTable = createChallengeTable(CHALLENGES_DATA);
const FILENAME = __filename.slice(__dirname.length + 1);
content = content
.replace("<challenges_table>", challengeTable)
.replace("<info_about_creation>", `This README was created using [${README_FILE_TEMPLATE}](${README_FILE_TEMPLATE}) by [${FILENAME}](./${FILENAME})`);
fs.writeFileSync(OUTPUT_FILE, content);