Skip to content

Commit

Permalink
chore: Better and quicker implementation of the file
Browse files Browse the repository at this point in the history
Signed-off-by: Avior <[email protected]>
  • Loading branch information
Aviortheking committed Jan 3, 2024
1 parent 41f5a13 commit 84ee606
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
7 changes: 4 additions & 3 deletions server/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable max-statements */
import { FileFunction } from './compilerInterfaces'
import { promises as fs } from 'fs'
import { fetchRemoteFile } from './utils/util'
import { objectValues } from '@dzeio/object-util'
import { SupportedLanguages } from '../../interfaces'
import { FileFunction } from './compilerInterfaces'
import { fetchRemoteFile, loadLastEdits } from './utils/util'

const LANGS: Array<SupportedLanguages> = ['en', 'fr', 'es', 'it', 'pt', 'de']

Expand All @@ -21,6 +20,8 @@ const DIST_FOLDER = './generated'
await fs.rm(DIST_FOLDER, {recursive: true})
} catch {}

console.log('Loading files last edit')
await loadLastEdits()

console.log('Let\'s GO !')

Expand Down
15 changes: 2 additions & 13 deletions server/compiler/utils/cardUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Card, Set, SupportedLanguages, Types } from '../../../interfaces'
import { CardResume, Card as CardSingle } from '../../../meta/definitions/api'
import { setToSetSimple } from './setUtil'
import translate from './translationUtil'
import { DB_PATH, cardIsLegal, fetchRemoteFile, smartGlob } from './util'
import { DB_PATH, cardIsLegal, fetchRemoteFile, getLastEdit, smartGlob } from './util'

export async function getCardPictures(cardId: string, card: Card, lang: SupportedLanguages): Promise<string | undefined> {
try {
Expand Down Expand Up @@ -165,16 +165,5 @@ export async function getCards(lang: SupportedLanguages, set?: Set): Promise<Arr

async function getCardLastEdit(localId: string, card: Card): Promise<string> {
const path = `../data/${card.set.serie.name.en}/${card.set.name.en ?? card.set.name.fr}/${localId}.ts`
const command = `git log -1 --pretty="format:%ci" "${path}"`
// console.log(command)
return new Promise((res, rej) => {
exec(command, (err, out, _) => {
// console.log(err, out)
if (err) {
rej(err)
return
}
res(out)
})
})
return getLastEdit(path)
}
45 changes: 44 additions & 1 deletion server/compiler/utils/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Card, Set } from '../../../interfaces'
import glob from 'glob'
import fetch from 'node-fetch'
import { exec } from 'node:child_process'
import { Card, Set } from '../../../interfaces'
import * as legals from '../../../meta/legals'

interface fileCacheInterface {
Expand Down Expand Up @@ -75,3 +76,45 @@ export function setIsLegal(type: 'standard' | 'expanded', set: Set): boolean {
}
return false
}

function runCommand(command: string): Promise<string> {
return new Promise<string>((res, rej) => {
exec(command, (err, out) => {
if (err) {
rej(err)
return
}
res(out)
})
})
}

let lastEditsCache: Record<string, string> = {}
export async function loadLastEdits() {
const firstCommand = 'git ls-tree -r --name-only HEAD ../data'
const files = (await runCommand(firstCommand)).split('\n')
console.log('Loaded files tree', files.length, 'files')
console.log('Loading their last edit time')
let processed = 0
for (let file of files) {
file = file.replace(/"/g, '').replace("\\303\\251", "é")
try {
lastEditsCache[file] = await runCommand(`git log -1 --pretty="format:%cd" --date=iso-strict "${file}"`)
} catch {
console.warn('could not load file', file, 'hope it does not break everything else lol')
}
processed++
if (processed % 1000 === 0) {
console.log('loaded', processed, 'out of', files.length, 'files')
}
}
console.log('done loading files')
}

export function getLastEdit(path: string): string {
const date = lastEditsCache[path]
if (!date) {
throw new Error(`edit date not found for file ${path}`)
}
return date
}

0 comments on commit 84ee606

Please sign in to comment.