Skip to content

Commit

Permalink
Update updateTutorials.js
Browse files Browse the repository at this point in the history
This code improves error handling, is made more readable, and uses asynchronous fs.promises operations for a more modern approach to reading files and working with the file system.
  • Loading branch information
cypherpepe authored Jun 30, 2024
1 parent 38b5405 commit 52a8b0f
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions apps/base-docs/scripts/updateTutorials.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const yaml = require('js-yaml');
const fs = require('fs');
const { readFile } = require('fs/promises');
const { readFile, readdir, stat } = fs.promises;
const path = require('path');
const crypto = require('crypto');

const tutorialsDir = path.join(__dirname, '..', 'tutorials', 'docs');
const outputFilePath = path.join(__dirname, '..', 'tutorials', 'data.json');

async function getDuration(filePath) {
try {
let content = await readFile(filePath, 'utf8');
const content = await readFile(filePath, 'utf8');
const words = content.trim().split(/\s+/).length;
const averageReadingSpeed = 225;
const readingTimeMinutes = (words / averageReadingSpeed) * 2;
Expand All @@ -31,18 +32,8 @@ async function getDuration(filePath) {

async function formatDate(date) {
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
];
const month = months[date.getMonth()];
const day = date.getDate();
Expand All @@ -62,7 +53,6 @@ async function calculateChecksum(filePath) {

(async () => {
const tutorials = {};
const outputFilePath = path.join(__dirname, '..', 'tutorials', 'data.json');
let existingData = {};

try {
Expand All @@ -72,23 +62,24 @@ async function calculateChecksum(filePath) {
}

try {
const files = await fs.promises.readdir(tutorialsDir);
const files = await readdir(tutorialsDir);
for (const file of files) {
const tutorialsPath = path.join(tutorialsDir, file);
const tutorialsStat = await fs.promises.stat(tutorialsPath);
const tutorialsStat = await stat(tutorialsPath);
if (tutorialsStat.isFile()) {
let content = await readFile(tutorialsPath, 'utf8');
content = content.split('---\n')[1];
const frontMatter = yaml.load(content);
const slug = frontMatter.slug.substring(1);
const frontMatter = yaml.load(content.split('---\n')[1]);
const slug = path.parse(file).name;
const checksum = await calculateChecksum(tutorialsPath);
const currentDate = new Date();

if (!existingData[slug] || existingData[slug].checksum !== checksum) {
tutorials[slug] = frontMatter;
tutorials[slug].last_updated = await formatDate(currentDate);
tutorials[slug].duration = await getDuration(tutorialsPath);
tutorials[slug].checksum = checksum;
tutorials[slug] = {
...frontMatter,
last_updated: await formatDate(currentDate),
duration: await getDuration(tutorialsPath),
checksum: checksum,
};
} else {
tutorials[slug] = existingData[slug];
}
Expand Down

0 comments on commit 52a8b0f

Please sign in to comment.