-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.js
More file actions
96 lines (78 loc) · 3.17 KB
/
sync.js
File metadata and controls
96 lines (78 loc) · 3.17 KB
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
import fs from 'fs';
import path from 'path';
function slugify(str) {
return str
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '');
}
function parseDuration(filename) {
const match = filename.match(/\((\d+)_(\d+)\)/);
if (!match) return '';
return `${match[1]}:${match[2].padStart(2, '0')}`;
}
function processSingleCourse(courseFolderPath) {
const courseTitle = path.basename(courseFolderPath);
const courseId = slugify(courseTitle);
const thumbnailUrl = `/courses/${courseTitle}/thumbnail.jpg`;
const courseContent = [];
const chapters = fs.readdirSync(courseFolderPath, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true }));
chapters.forEach(chapterDir => {
const chapterName = chapterDir.name;
const chapterPath = path.join(courseFolderPath, chapterName);
const videos = fs.readdirSync(chapterPath, { withFileTypes: true })
.filter(dirent => dirent.isFile() && (dirent.name.toLowerCase().endsWith('.mp4') || dirent.name.toLowerCase().endsWith('.pdf')))
.sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true }))
.map(file => {
const fileType = file.name.toLowerCase().endsWith('.pdf') ? 'pdf' : 'video';
// Handle both patterns: "59. title" and "6. 18- title" (chapter.video format)
const dualNumberMatch = file.name.match(/^(\d+)\.\s*(\d+)-/);
const id = dualNumberMatch ? dualNumberMatch[2] : (file.name.match(/^(\d+)\./)?.[1] || '');
const titlePart = file.name
.replace(/^(\d+\.\s*)?/, '') // Remove first number prefix
.replace(/^(\d+-\s*)?/, '') // Remove second number prefix if present (e.g., "18- ")
.replace(/\(.*\)\.(mp4|pdf)$/i, '') // Remove duration and extension
.trim();
const duration = fileType === 'video' ? parseDuration(file.name) : '';
return {
id,
title: titlePart,
duration,
completed: false,
path: `/public/courses/${courseTitle}/${chapterName}/${file.name}`,
type: fileType
};
});
courseContent.push({
chapter: chapterName || '',
videos
});
});
return {
id: courseId,
title: courseTitle,
thumbnailUrl,
courseContent
};
}
// ================================
// Main
// ================================
const coursesBasePath = './public/courses'; // Root directory containing multiple courses
if (!fs.existsSync(coursesBasePath)) {
console.error('❌ Base courses folder not found!');
process.exit(1);
}
const courseFolders = fs.readdirSync(coursesBasePath, { withFileTypes: true })
.filter(dirent => dirent.isDirectory());
const allCourses = courseFolders.map(courseDir => {
const courseFolderPath = path.join(coursesBasePath, courseDir.name);
return processSingleCourse(courseFolderPath);
});
fs.writeFileSync('./src/allCourses.json', JSON.stringify(allCourses, null, 2), 'utf8');
console.log('✅ Saved allCourses.json!');