forked from buildspace/buildspace-projects
-
Notifications
You must be signed in to change notification settings - Fork 48
/
filegen.js
58 lines (50 loc) · 1.63 KB
/
filegen.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
55
56
57
58
// This is a script that generates 5 folders and markdown files for each folder
const fs = require('fs');
const path = require('path');
// Set the project folder name here
const folderName = 'AI_Avatar_Generator';
const sectionCount = 5;
const dirName = path.join(folderName, "/en/");
// Create folders "Section_0" through to "Section_4" if they don't exist
for (let i = 0; i < sectionCount; i++) {
const folder = path.join(dirName, `Section_${i}`);
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
}
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
function getUserInput() {
return new Promise((resolve, reject) => {
readline.question('Enter lesson name: ', (input) => {
resolve(input);
});
});
}
// Each Section folder will have markdown files, with it's own numbering
// So the folder/file structure would look like:
// Section_0
// Lesson_1_Getting_started.md
// Lesson_2_Installing.md
// Section_1
// Lesson_1_First_feature.md
// Get the user to enter file names, or enter nothing to go to the next secion
async function createFiles() {
for (let i = 0; i < 5; i++) {
let fileNumber = 1;
let fileName = await getUserInput();
while (fileName) {
const filePath = path.join(dirName, `Section_${i}`, `Lesson_${fileNumber}_${fileName}.md`);
// Replace spaces with _ and remove punctuation
fileName = fileName.replace(/ /g, '_').replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
fs.writeFileSync
(filePath, `# ${fileName}`);
fileNumber++;
fileName = await getUserInput();
}
}
readline.close();
}
createFiles();