-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate-sqlite.js
More file actions
96 lines (83 loc) · 2.49 KB
/
generate-sqlite.js
File metadata and controls
96 lines (83 loc) · 2.49 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
const { readFileSync, readdirSync, unlinkSync, statSync, writeFileSync } = require('fs')
const { Database } = require('node-sqlite3-wasm')
const { join } = require('path')
const { gzipSync } = require('zlib')
const { createHash } = require('crypto')
function splitCsvLine(line) {
// respect the quotes
const result = []
let current = ''
let inQuote = false
for (const char of line) {
if (char == ',') {
if (inQuote) {
current += char
} else {
result.push(current)
current = ''
}
} else if (char == '"') {
inQuote = !inQuote
} else {
current += char
}
}
result.push(current)
return result
}
/**
* @param {Database} db
* @param {string} locale locale code like zh-cn
* @param {string} type mods, modpacks, or resourcepacks
*/
function processLocaleType(db, locale, type) {
const csvFile = join('src', locale, type + '.csv')
const fileContent = readFileSync(csvFile, 'utf-8')
const lines = fileContent.split('\n')
lines.shift() // remove the header
for (const line of lines) {
const [name, modrinthId, curseforgeId, description] = splitCsvLine(line)
try {
db.run(`INSERT INTO project (curseforgeId, modrinthId, name, description) VALUES (?, ?, ?, ?)`, [curseforgeId, modrinthId, name ?? '', description ?? ''])
} catch (e) {
console.error(e)
}
}
}
const dirs = readdirSync('src')
for (const locale of dirs) {
const sqlitePath = join('build', `${locale}.sqlite`)
const db = new Database(join('build', `${locale}.sqlite`))
db.exec(`
CREATE TABLE IF NOT EXISTS project (
curseforgeId INTEGER,
modrinthId TEXT,
name TEXT NOT NULL,
description TEXT,
CONSTRAINT unique_ids UNIQUE (curseforgeId, modrinthId)
);`)
const files = readdirSync(join('src', locale))
if (files.includes('mods.csv')) {
processLocaleType(db, locale, 'mods')
}
if (files.includes('modpacks.csv')) {
processLocaleType(db, locale, 'modpacks')
}
if (files.includes('resourcepacks.csv')) {
processLocaleType(db, locale, 'resourcepacks')
}
db.close()
// gzip the file
const buffer = readFileSync(sqlitePath)
const compressed = gzipSync(buffer)
const compressedPath = join(sqlitePath + '.gz')
writeFileSync(compressedPath, compressed)
// sha256
const hash = createHash('sha256')
hash.update(buffer)
const sha256 = hash.digest('hex')
const sha256Path = join(sqlitePath + '.sha256')
writeFileSync(sha256Path, sha256)
// remove the original file
unlinkSync(sqlitePath)
}