From 84ee606acaaf8aba40945bb2dc9a8f7280369365 Mon Sep 17 00:00:00 2001 From: Avior Date: Wed, 3 Jan 2024 02:22:57 +0100 Subject: [PATCH] chore: Better and quicker implementation of the file Signed-off-by: Avior --- server/compiler/index.ts | 7 ++--- server/compiler/utils/cardUtil.ts | 15 ++--------- server/compiler/utils/util.ts | 45 ++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/server/compiler/index.ts b/server/compiler/index.ts index cdb6dfdd0d..fbf7313e96 100644 --- a/server/compiler/index.ts +++ b/server/compiler/index.ts @@ -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 = ['en', 'fr', 'es', 'it', 'pt', 'de'] @@ -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 !') diff --git a/server/compiler/utils/cardUtil.ts b/server/compiler/utils/cardUtil.ts index 885fde5f2e..4670073f88 100644 --- a/server/compiler/utils/cardUtil.ts +++ b/server/compiler/utils/cardUtil.ts @@ -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 { try { @@ -165,16 +165,5 @@ export async function getCards(lang: SupportedLanguages, set?: Set): Promise { 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) } diff --git a/server/compiler/utils/util.ts b/server/compiler/utils/util.ts index 25977828af..5c648634aa 100644 --- a/server/compiler/utils/util.ts +++ b/server/compiler/utils/util.ts @@ -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 { @@ -75,3 +76,45 @@ export function setIsLegal(type: 'standard' | 'expanded', set: Set): boolean { } return false } + +function runCommand(command: string): Promise { + return new Promise((res, rej) => { + exec(command, (err, out) => { + if (err) { + rej(err) + return + } + res(out) + }) + }) +} + +let lastEditsCache: Record = {} +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 +}