-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogFiles.js
36 lines (34 loc) · 960 Bytes
/
logFiles.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
import fs from 'fs';
import path from 'path';
const directoryPath = path.join(process.cwd(), 'public/posts');
fs.readdir(directoryPath, (err, fileNames) => {
if (err) {
return console.error('Unable to scan directory: ' + err);
}
// make renderedPosts folder if it doesn't exist
if (!fs.existsSync('public/renderedPosts')) {
fs.mkdirSync('public/renderedPosts');
}
console.log('Listing files in public/posts:');
fileNames.forEach(fileName => {
/*
For each file, create a new html file with the file name as the title
in public/renderedPosts
*/
// get filename without extension
const baseName = fileName.split('.')[0];
const html = `
<!DOCTYPE html>
<html>
<head>
<title>${baseName}</title>
</head>
<body>
<h1>${baseName}</h1>
</body>
</html>
`;
fs.writeFileSync(`public/renderedPosts/${fileName}.html`, html);
console.log(fileName);
});
});