-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathcourse.ts
More file actions
114 lines (99 loc) · 2.76 KB
/
course.ts
File metadata and controls
114 lines (99 loc) · 2.76 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use server'
import path from 'node:path'
import courseConfig from '@/courses/config.json'
import fs from 'node:fs'
import { notFound } from 'next/navigation'
import { getAbsoluteCoursePath } from './utils'
export type CourseIdentityDescription = {
sourceLanguageCode: string
targetLanguageCode: string
}
export type Course = {
id: string
languageCode: string
languageName: string
uiLanguage: string
repositoryURL: string
inProduction: boolean
}
function getFullJsonPath(jsonPath: string) {
return path.join(getAbsoluteCoursePath(jsonPath), 'courseData.json')
}
async function getCourseMetadataByJsonPath(jsonPath: string) {
const fileContent = await fs.promises.readFile(
getFullJsonPath(jsonPath),
'utf8'
)
return JSON.parse(fileContent)
}
export async function listAvailableCourses(): Promise<Course[]> {
return Promise.all(
courseConfig
.filter((item) => {
return (
item.deploy &&
fs.existsSync(getFullJsonPath(item.paths.jsonFolder))
)
})
.map(async (item) => {
const jsonPath = item.paths.jsonFolder
const data = await getCourseMetadataByJsonPath(jsonPath)
const {
uiLanguage,
languageName,
languageCode,
repositoryURL,
} = data
return {
id: jsonPath,
languageCode,
languageName,
uiLanguage,
repositoryURL,
inProduction: item.inProduction,
}
})
)
}
export async function getCourseId(
parameters: CourseIdentityDescription
): Promise<string> {
const availableCourses = await listAvailableCourses()
const course = availableCourses.find(
(item) =>
item.uiLanguage === parameters.sourceLanguageCode &&
item.languageCode === parameters.targetLanguageCode
)
if (course === undefined) {
notFound()
}
return course.id
}
export type Module = {
title: string
slug: string
}
export type CourseDetail = {
targetLanguage: {
name: string
code: string
}
sourceLanguage: {
code: string
}
modules: Module[]
}
export async function getCourseDetail(courseId: string): Promise<CourseDetail> {
const { languageName, modules, uiLanguage, languageCode } =
await getCourseMetadataByJsonPath(courseId)
return {
targetLanguage: {
name: languageName,
code: languageCode,
},
sourceLanguage: {
code: uiLanguage
},
modules,
}
}