-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
89 lines (73 loc) · 2.72 KB
/
mod.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
import { readLines } from 'https://deno.land/std/io/mod.ts'
const start: number = Date.now()
const tils: TILs = {}
let count: number = 0
async function readFiles(dirName: string, path: string): Promise<void> {
tils[dirName] = []
for await (const { isFile, name } of Deno.readDir(path)) {
if (isFile && name.endsWith('.md')) {
const file: Deno.File = await Deno.open(`${path}/${name}`)
const firstLine: IteratorResult<string, any> = await readLines(
file
).next()
file.close()
const title: string = firstLine?.value.replace(/^#*/, '').trim()
const deepCopy: Array<TIL> = tils[dirName].map((text: TIL) => ({
...text,
}))
tils[dirName] = [...deepCopy, { title, fileName: name }]
}
}
}
for await (const { isDirectory, name } of Deno.readDir('./'))
isDirectory && !name.startsWith('.') && (await readFiles(name, `./${name}`))
for (const category in tils) count += tils[category].length
const categories: string[] = Object.keys(tils).sort()
//prettier-ignore
const readme: string =
`# TIL - Today I Learned 🤤
Trying to keep a collection of short texts about things I've learned. TIL (Today I Learned). Inspired on [jbranchaud/til](https://github.com/jbranchaud/til) list.
> (pt-BR) Tentando manter uma lista de pequenos textos sobre as coisas que aprendi. HEA (Hoje Eu Aprendi 😅). Inspirado na lista [jbranchaud/til](https://github.com/jbranchaud/til).
_[ ${count} ] TILs and counting...?_ 🙈
I also made a [script](./mod.ts) to generate the \`README.md\` file, maybe it's useful for your list:
> (pt-BR) Deixei um [script](./mod.ts) para gerar o \`README.md\`, talvez possa ser útil para a sua lista:
\`\`\`zsh
$ deno run --allow-read --allow-write mod.ts
\`\`\`
---
### Categories
${categories
.map((category: string) => `* [${category}](#${category.toLowerCase()})\n`)
.join('')}
---
${categories
.map(
(category) =>
`### ${category}
${tils[category]
.map((text: TIL) => `- [${text.title}](${category}/${text.fileName})\n`)
.join('')}`
)
.join('')}
`
// Replace all consecutive white spaces that are not a line break
.replace(/[^\S\r\n]+/g, ' ')
// Trim white spaces in each line
.replace(/^[^\S\r\n]+|[^\S\r\n]+$/gm, '')
// Replace multiple line break
.replace(/\n{3,}/g, '\n\n')
// Prevent start/end line break
.trim()
const encoder: TextEncoder = new TextEncoder()
await Deno.writeFile('./README.md', encoder.encode(readme))
const delta: number = Date.now() - start
console.log('\x1b[1m\x1b[34m%s\x1b[0m', `Done[✓] : ${delta}ms`)
console.table(Deno.metrics())
// Types
interface TILs {
[key: string]: Array<TIL>
}
type TIL = {
title: string
fileName: string
}