Skip to content

Commit

Permalink
feat: Add an updated field to the card field (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviortheking authored May 11, 2024
1 parent e4aba3b commit c476d82
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ FROM docker.io/oven/bun:1-alpine as BUILD_IMAGE
# go to work folder
WORKDIR /usr/src/app

# Add git as it is used to fetch updated times
RUN apk add git &&\
git config --global safe.directory '*'

ADD --chown=bun:bun package.json bun.lockb ./
ADD --chown=bun:bun server/package.json server/bun.lockb ./server/

Expand Down
14 changes: 14 additions & 0 deletions meta/codeshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ function simplify(base: ObjectExpression): ObjectField {
}
}

function exists(path: ObjectExpression | ArrayExpression, key: string | number) {
if (path.type === 'ObjectExpression') {
path.properties.forEach((p) => {
const prop = p as Property
if ((prop.key as Identifier).name === (key + '')) {
return true
}
})
return false
} else {

}
}

function set(j: JSCodeshift, path: ObjectExpression | ArrayExpression, value: Possible, key: string | number, options?: {override?: boolean}) {

let exists = false
Expand Down
4 changes: 3 additions & 1 deletion meta/definitions/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ export interface Card extends CardResume {
* Ability to play in expanded tournaments
*/
expanded: boolean;
};
}

updated: string
}

/**
Expand Down
4 changes: 3 additions & 1 deletion server/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { promises as fs } from 'fs'
import { SupportedLanguages } from '../../interfaces'
import { FileFunction } from './compilerInterfaces'
import { fetchRemoteFile } from './utils/util'
import { fetchRemoteFile, loadLastEdits } from './utils/util'

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

Expand All @@ -20,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
12 changes: 9 additions & 3 deletions server/compiler/utils/cardUtil.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable sort-keys */
import { exec } from 'child_process'
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 @@ -106,8 +107,8 @@ export async function cardToCardSingle(localId: string, card: Card, lang: Suppor
legal: {
standard: cardIsLegal('standard', card, localId),
expanded: cardIsLegal('expanded', card, localId)
}

},
updated: await getCardLastEdit(localId, card)
}
}

Expand Down Expand Up @@ -161,3 +162,8 @@ export async function getCards(lang: SupportedLanguages, set?: Set): Promise<Arr
return a >= b ? 1 : -1
})
}

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`
return getLastEdit(path)
}
43 changes: 43 additions & 0 deletions server/compiler/utils/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { exec } from 'node:child_process'
import { glob } from 'glob'
import { Card, Set } from '../../../interfaces'
import * as legals from '../../../meta/legals'
Expand Down Expand Up @@ -79,3 +80,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 c476d82

Please sign in to comment.