-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildNarrations.js
162 lines (146 loc) · 4.7 KB
/
buildNarrations.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fs = require('fs');
const path = require('path');
const { snakeCase, capitalize } = require('lodash');
buildNarrations();
function buildNarrations() {
const narrationsListPath = path.join(
__dirname,
'public/narrations',
'list.json'
);
const narrationsListRaw = fs.readFileSync(narrationsListPath, 'utf-8');
const narrationsList = JSON.parse(narrationsListRaw);
narrationsList.forEach(buildNarration);
}
function buildNarration(narrationTitle) {
const narrationPath = path.join(
__dirname,
'public/narrations',
narrationTitle
);
deleteTopLevelNarrationFiles(narrationPath);
const folders = fs
.readdirSync(narrationPath)
.filter((folderName) => isFolder(path.join(narrationPath, folderName)));
folders.forEach((folderName) => {
const folderPath = path.join(narrationPath, folderName);
buildScenes(folderPath, folderName, narrationPath);
});
}
function deleteTopLevelNarrationFiles(narrationPath) {
const files = fs.readdirSync(narrationPath);
files.forEach((file) => {
const extension = getFileExtension(file);
if (extension === 'md') {
const filePath = path.join(narrationPath, file);
fs.unlinkSync(filePath);
}
});
}
function getFileExtension(file) {
const extension = path.extname(file);
return extension.substring(1);
}
function isFolder(folderPath) {
const stats = fs.statSync(folderPath);
return stats.isDirectory();
}
function buildScenes(scenePath, prefix, outputFolderPath) {
console.log(scenePath);
const files = fs.readdirSync(scenePath);
const pairedFiles = pairFiles(files);
pairedFiles.forEach(({ yaml, md }) => {
try {
const yamlPath = path.join(scenePath, yaml);
const mdPath = path.join(scenePath, md);
buildScene(yamlPath, mdPath, prefix, outputFolderPath);
} catch (err) {
console.error('Error building scene', yaml, md);
throw err;
}
});
}
function pairFiles(files) {
return files.reduce((pairedFiles, file) => {
const extension = getFileExtension(file);
const fileNameWithoutExtension = getFileNameWithoutExtension(
file,
extension
);
return addToPairedFiles(
pairedFiles,
fileNameWithoutExtension,
extension
);
}, []);
}
function getFileNameWithoutExtension(file, extension) {
const extensionIndex = file.indexOf(extension);
return file.substring(0, extensionIndex - 1);
}
function addToPairedFiles(pairedFiles, fileNameWithoutExtension, extension) {
let existingPairedFilesEntry = pairedFiles.find(
({ id }) => id === fileNameWithoutExtension
);
if (!existingPairedFilesEntry) {
existingPairedFilesEntry = { id: fileNameWithoutExtension };
pairedFiles.push(existingPairedFilesEntry);
}
existingPairedFilesEntry[
extension
] = `${fileNameWithoutExtension}.${extension}`;
return pairedFiles;
}
function buildScene(yamlPath, mdPath, prefix, outputFolderPath) {
let yamlContent;
try {
yamlContent = fs.readFileSync(yamlPath, 'utf-8');
yamlContent = completeYamlContent(yamlContent, yamlPath);
} catch (err) {}
const mdContent = fs.readFileSync(mdPath, 'utf-8');
const sceneContent = buildSceneContent(yamlContent, mdContent);
let fileName = path.basename(mdPath);
if (fileName === 'index.md') {
fileName = `${prefix}.md`;
} else {
fileName = `${prefix}_${fileName}`;
}
const outputScenePath = path.join(outputFolderPath, fileName);
fs.writeFileSync(outputScenePath, sceneContent, { encoding: 'utf-8' });
}
function completeYamlContent(yamlContent, yamlPath) {
yamlContent = completeId(yamlContent, yamlPath);
yamlContent = completeTitle(yamlContent, yamlPath);
return yamlContent;
}
function completeId(yamlContent, yamlPath) {
const fileName = path.basename(yamlPath);
const extension = path.extname(fileName);
const folderName = path.basename(path.dirname(yamlPath));
const id =
fileName === 'index.yaml'
? folderName
: `${folderName}_${fileName.replace(extension, '')}`;
return `id: ${id}
${yamlContent}
`;
}
function completeTitle(yamlContent, yamlPath) {
if (yamlContent.indexOf('title:') >= 0) {
return yamlContent;
}
const folderName = path.basename(path.dirname(yamlPath));
const title = capitalize(snakeCase(folderName).replace(/_/g, ' '));
return `title: ${title}
${yamlContent}
`;
}
function buildSceneContent(yaml, md) {
if (!yaml) {
return md;
}
return `---
${yaml.trim()}
---
${md}`;
}