Skip to content

Commit

Permalink
Update tutorial data gen script (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
taycaldwell committed May 7, 2024
1 parent 1469c3b commit 6a798b1
Showing 1 changed file with 45 additions and 40 deletions.
85 changes: 45 additions & 40 deletions apps/base-docs/scripts/updateTutorials.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ async function getDuration(filePath) {
try {
let content = await readFile(filePath, 'utf8');
const words = content.trim().split(/\s+/).length;
const averageReadingSpeed = 225; // Average between 200 and 250 wpm
const readingTimeMinutes = (words / averageReadingSpeed) * 2; // Double estimated time
const averageReadingSpeed = 225;
const readingTimeMinutes = (words / averageReadingSpeed) * 2;

const hours = Math.floor(readingTimeMinutes / 60);
const minutes = Math.round(readingTimeMinutes % 60);
Expand All @@ -31,63 +31,68 @@ async function getDuration(filePath) {
async function getLastUpdated(filePath) {
try {
const stats = await fs.promises.stat(filePath);
const lastModified = stats.mtime;

const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
const month = months[lastModified.getMonth()];
const day = lastModified.getDate();

return `${month} ${day}`;
return stats.mtime;
} catch (error) {
console.error('Error getting file stats:', error);
return null;
}
}

async function formatDate(date) {
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
const month = months[date.getMonth()];
const day = date.getDate();
return `${month} ${day}`;
}

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

try {
existingData = JSON.parse(await readFile(outputFilePath, 'utf8'));
} catch (e) {
console.error('Failed to read existing data:', e);
}

try {
const files = await fs.promises.readdir(tutorialsDir);
for (const file of files) {
const tutorialsPath = path.join(tutorialsDir, file);
const tutorialsStat = await fs.promises.stat(tutorialsPath);
if (tutorialsStat.isDirectory()) {
// const files = await fs.promises.readdir(tutorialsPath);
// for (const file of files) {
// const tutorialPath = path.join(tutorialsPath, file);
// const tutorialStat = await fs.promises.stat(tutorialPath);
// if (tutorialStat.isFile()) {
// let content = await readFile(tutorialPath, 'utf8');
// content = content.split('---\n')[1];
// const frontMatter = yaml.load(content);
// tutorials[frontMatter.slug.substring(1)] = frontMatter;
// }
// }
} else if (tutorialsStat.isFile()) {
if (tutorialsStat.isFile()) {
let content = await readFile(tutorialsPath, 'utf8');
content = content.split('---\n')[1];
const frontMatter = yaml.load(content);
tutorials[frontMatter.slug.substring(1)] = frontMatter;
tutorials[frontMatter.slug.substring(1)].last_updated = await getLastUpdated(tutorialsPath);
tutorials[frontMatter.slug.substring(1)].duration = await getDuration(tutorialsPath);
const slug = frontMatter.slug.substring(1);
const lastUpdated = await getLastUpdated(tutorialsPath);

if (!existingData[slug] || existingData[slug].last_updated !== lastUpdated.toISOString()) {
tutorials[slug] = frontMatter;
tutorials[slug].last_updated = await formatDate(lastUpdated);
tutorials[slug].duration = await getDuration(tutorialsPath);
} else {
tutorials[slug] = existingData[slug];
}
}
}
} catch (e) {
console.error('Error updating tutorial data.', e);
console.error('Error updating tutorial data:', e);
}
const outputFilePath = path.join(__dirname, '..', 'tutorials', 'data.json');

fs.writeFileSync(outputFilePath, JSON.stringify(tutorials, null, 4));
})();

0 comments on commit 6a798b1

Please sign in to comment.