-
Notifications
You must be signed in to change notification settings - Fork 0
/
localize.ts
57 lines (46 loc) · 1.27 KB
/
localize.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
import { promises as fs } from "fs"
import path from "path"
const main = async () => {
const en = await walk("./pages/en")
const fr = await walk("./pages/fr")
const missingFiles = [...en]
.map((file) => file.replace("/en/", "/fr/"))
.filter((file) => !fr.includes(file))
for (const file of missingFiles) {
const dir = path.dirname(file)
if (!(await exists(dir))) {
fs.mkdir(dir, { recursive: true })
}
if (!(await exists(file))) {
await fs.writeFile(
"./" + file,
generateFile(file.replace("/fr/", "/en/")),
)
}
}
}
// https://gist.github.com/kethinov/6658166?permalink_comment_id=2733303#gistcomment-2733303
const walk = async (dir: string, filelist: string[] = []) => {
const files = await fs.readdir(dir)
for (const file of files) {
const filepath = path.join(dir, file)
const stat = await fs.stat(filepath)
if (stat.isDirectory()) {
filelist = await walk(filepath, filelist)
} else {
filelist.push(filepath)
}
}
return filelist
}
const exists = async (dir: string) => {
try {
await fs.access(dir)
return true
} catch {
return false
}
}
const generateFile = (path: string) =>
`export { default as default } from "${path.replace(".ts", "")}"\n`
main()