-
Notifications
You must be signed in to change notification settings - Fork 0
/
up.ts
194 lines (167 loc) · 4.88 KB
/
up.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Automating course progress update with a terminal command.
*
* @remarks
* To run the script from the terminal, use:
* ```
* ts-node <filename> <lecture> <module>
* ```
* Replace `<lecture>` and `<module>` with the appropriate values.
*
* - `<lecture>`: An integer representing the last finished lecture.
* - `<module>`: Lowercase Roman numeral (from 'i' to 'iv').
*/
import { promises as fs } from "fs"
/**
* Regular expression type.
*/
type Regex = RegExp
/**
* Module type.
*/
type Module = {
/**
* Label for the module.
*/
label: string;
/**
* Start index of the module.
*/
start: number;
/**
* Finish index of the module.
*/
finish: number;
}
/**
* Roman numerals enumeration.
*/
enum RomanNumerals {
I = "i",
II = "ii",
III = "iii",
IV = "iv"
}
/**
* Calculate progress percentage.
*
* @param start - Start index.
* @param current - Current index.
* @param finish - Finish index.
* @returns Calculated progress percentage (multiple of 5).
*/
const calculateProgress = (
start: number,
current: number,
finish: number
): number => {
const total = finish - start
const done = current - start
const progress = Math.ceil(done / total * 100 / 5) * 5
return progress
}
/**
* Update progress in README file.
*
* @param lecture - Last finished lecture.
* @param module - Module details.
* @returns A Promise that resolves when the progress is updated.
*/
const updateReadmeProgress = async (
lecture: number,
module: Module
): Promise<void> => {
const filePath = "./README.md"
try {
const data = await fs.readFile(filePath, "utf8")
const _progressBarRegex: string = `!\\[(\\d+)%\\]\\(https://geps.dev/progress/(\\d+)\\)`
const headingRegex: Regex = new RegExp(`###\\s*${_progressBarRegex}\\s*${module.label}`, "gim")
const replaceProgress = () => {
const newPercentage = calculateProgress(module.start, lecture, module.finish)
return `### ![${newPercentage}%](https://geps.dev/progress/${newPercentage}) ${module.label}`
}
const updatedContent = data.replace(headingRegex, replaceProgress)
await fs.writeFile(filePath, updatedContent, "utf8")
console.log(`Progress indicator for module "${module.label}" updated successfully.`)
} catch (error) {
console.error("Error updating the README file:", error)
}
}
/**
* Check if the given lecture is within a valid range for the specified module.
*
* @param lecture - The lecture to check.
* @param module - The module details.
* @returns True if the lecture is within the valid range, otherwise false.
*/
const isValidLecture = (lecture: number, module: Module): boolean => {
return lecture >= module.start && lecture <= module.finish;
}
/**
* Main function.
*
* @remarks
* This function extracts and validates command-line arguments.
* @example
* ```
* const modules: Readonly<{ [key: string]: Module }> = { ... }
* const [, , ...args] = process.argv
* main()
* ```
*/
const main = async () => {
try {
const modules: Readonly<{ [key: string]: Module }> = {
[RomanNumerals.I]: {
label: "I. Foundations",
start: 5,
finish: 44
},
[RomanNumerals.II]: {
label: "II. Generics & type manipulations",
start: 46,
finish: 65
},
[RomanNumerals.III]: {
label: "III. Classes",
start: 66,
finish: 79
},
[RomanNumerals.IV]: {
label: "IV. Decorators & configuration",
start: 80,
finish: 100
}
}
const [, , ...args] = process.argv
if (args.length !== 2) {
console.error("Error: Please provide both lecture and module arguments")
process.exit(1)
}
const [lectureArgument, moduleArgument] = args
const lecture = parseInt(lectureArgument)
if (isNaN(lecture)) {
console.error("Error: Lecture must be a valid number")
process.exit(1)
}
const moduleKey = moduleArgument.toLowerCase() as RomanNumerals
let module: Module
if (modules.hasOwnProperty(moduleKey)) {
module = modules[moduleKey]
if (isValidLecture(lecture, module)) {
await updateReadmeProgress(lecture, module)
} else {
console.error(`Error: Lecture is not within the valid range for module ${moduleKey}`)
process.exit(1)
}
} else {
console.error(`Error: Module ${moduleKey} not found`)
process.exit(1)
}
}
catch (error) {
console.error("An unexpected error occurred:", error)
process.exit(1)
}
}
main()