Skip to content

Commit

Permalink
adjust build workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Oct 5, 2024
1 parent e31740d commit 9234d30
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 43 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/sqlite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: pnpm install
- name: Run genreate sqlite action
run:
node ./generate-sqlite.mjs
pnpm build
- name: Draft Release
id: create_release
uses: voxelum/create-release@master
Expand All @@ -37,3 +37,11 @@ jobs:
prerelease: false
body: v${{ github.run_number }}
asset_dir_path: ./build
- name: Upload to Azure
uses: ci010/upload-blob-to-azure@master
env:
AZURE_ACCOUNT_KEY: ${{ secrets.AZURE_ACCOUNT_KEY }}
with:
account: xmcl
container: project-mapping
directory: ./build
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/node_modules/**
**/node_modules/**

build/
79 changes: 39 additions & 40 deletions generate-sqlite.mjs → generate-sqlite.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync, readdirSync, unlinkSync, statSync, writeFileSync } from 'fs'
import { Database } from 'node-sqlite3-wasm'
import { join } from 'path'
import { gzipSync } from 'zlib'
import { createHash } from 'crypto'
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
Expand All @@ -28,21 +28,11 @@ function splitCsvLine(line) {
}

/**
* @param {Database} db
* @param {string} locale locale code like zh-cn
* @param {string} type mods, modpacks, or resourcepacks
*/
function processLocaleType(locale, type) {
const db = new Database(join('build', `${type}-${locale}.sqlite`))

db.exec(`
CREATE TABLE IF NOT EXISTS i18n (
curseforgeId INTEGER,
modrinthId TEXT,
name TEXT NOT NULL,
description TEXT,
CONSTRAINT unique_ids UNIQUE (curseforgeId, modrinthId)
);`)

function processLocaleType(db, locale, type) {
const csvFile = join('src', locale, type + '.csv')

const fileContent = readFileSync(csvFile, 'utf-8')
Expand All @@ -54,44 +44,53 @@ function processLocaleType(locale, type) {
for (const line of lines) {
const [name, modrinthId, curseforgeId, description] = splitCsvLine(line)
try {
db.run(`INSERT INTO i18n (curseforgeId, modrinthId, name, description) VALUES (?, ?, ?)`, [curseforgeId, modrinthId, name, description])
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(join('build', `${type}-${locale}.sqlite`))
const buffer = readFileSync(sqlitePath)
const compressed = gzipSync(buffer)
const compressedPath = join('build', `${type}-${locale}.sqlite.gz`)
const compressedPath = join(sqlitePath + '.gz')
writeFileSync(compressedPath, compressed)

// sha256
const hash = createHash('sha256')
hash.update(compressed)
hash.update(buffer)
const sha256 = hash.digest('hex')
const sha256Path = join('build', `${type}-${locale}.sqlite.gz.sha256`)
const sha256Path = join(sqlitePath + '.sha256')
writeFileSync(sha256Path, sha256)

// remove the original file
unlinkSync(join('build', `${type}-${locale}.sqlite`))
}

const dirs = readdirSync('src')

for (const locale of dirs) {
if (statSync(locale).isDirectory()) {
const files = readdirSync(locale)
if (files.includes('mods.csv')) {
processLocaleType(locale, 'mods')
}
if (files.includes('modpacks.csv')) {
processLocaleType(locale, 'modpacks')
}
if (files.includes('resourcepacks.csv')) {
processLocaleType(locale, 'resourcepacks')
}
}
unlinkSync(sqlitePath)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"scripts": {
"sort": "node sort.mjs",
"sync": "node sync.mjs",
"validate": "node validate.mjs"
"validate": "node validate.mjs",
"build": "node generate-sqlite.js"
},
"dependencies": {
"node-sqlite3-wasm": "^0.8.16"
Expand Down

0 comments on commit 9234d30

Please sign in to comment.