mirror of
https://github.com/tcgdex/cards-database.git
synced 2025-06-17 01:49:19 +00:00
feat: Add support for Asians Pokémon Cards (#481)
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
import { exec } from 'node:child_process'
|
||||
import { objectSize } from '@dzeio/object-util'
|
||||
import { glob } from 'glob'
|
||||
import { Card, Set } from '../../../interfaces'
|
||||
import { exec, spawn } from 'node:child_process'
|
||||
import { writeFileSync } from 'node:fs'
|
||||
import { Card, Set, SupportedLanguages } from '../../../interfaces'
|
||||
import * as legals from '../../../meta/legals'
|
||||
|
||||
interface fileCacheInterface {
|
||||
@ -81,11 +83,42 @@ export function setIsLegal(type: 'standard' | 'expanded', set: Set): boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
function runCommand(command: string): Promise<string> {
|
||||
export function getDataFolder(lang: SupportedLanguages) {
|
||||
return ['ja', 'ko', 'zh-tw', 'id', 'th', 'zh-cn'].includes(lang) ? 'data-asia' : 'data'
|
||||
}
|
||||
|
||||
/**
|
||||
* run a command on the OS, it uses Spawn by default because exec seems to have a bug linked to the Buffer
|
||||
*
|
||||
* @param command the command to run
|
||||
* @param useSpawn select the method to use to run the command
|
||||
* @returns a string with the stdout
|
||||
*/
|
||||
function runCommand(command: string, useSpawn = true): Promise<string> {
|
||||
if (!useSpawn) {
|
||||
return new Promise<string>((res, rej) => {
|
||||
exec(command, (err, out) => {
|
||||
if (err) {
|
||||
rej(err)
|
||||
}
|
||||
res(out)
|
||||
})
|
||||
})
|
||||
}
|
||||
const splitted = command.split(' ')
|
||||
command = splitted.shift()!
|
||||
|
||||
return new Promise<string>((res, rej) => {
|
||||
exec(command, (err, out) => {
|
||||
if (err) {
|
||||
rej(err)
|
||||
const cmd = spawn(command, splitted)
|
||||
let out: string = ''
|
||||
cmd.stdout.on('data', (data) => {
|
||||
out += data.toString()
|
||||
})
|
||||
|
||||
cmd.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
console.log(`command exited with code ${code}`);
|
||||
rej(code)
|
||||
return
|
||||
}
|
||||
res(out)
|
||||
@ -95,24 +128,28 @@ function runCommand(command: string): Promise<string> {
|
||||
|
||||
let lastEditsCache: Record<string, string> = {}
|
||||
export async function loadLastEdits() {
|
||||
console.log('Loading Git File Tree...')
|
||||
const firstCommand = 'git ls-tree -r --name-only HEAD ../data'
|
||||
const files = (await runCommand(firstCommand)).split('\n')
|
||||
const secondCommand = 'git ls-tree -r --name-only HEAD ../data-asia'
|
||||
files.push(...(await runCommand(secondCommand)).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}"`)
|
||||
// don't really know why but it does not correctly execute the command when using Spawn
|
||||
lastEditsCache[file] = await runCommand(`git log -1 --pretty="format:%cd" --date=iso-strict "${file}"`, false)
|
||||
} 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('loaded', processed, 'out of', files.length, 'files', `(${(processed / files.length * 100).toFixed(0)}%)`)
|
||||
}
|
||||
}
|
||||
console.log('done loading files')
|
||||
console.log('done loading files', objectSize(lastEditsCache))
|
||||
}
|
||||
|
||||
export function getLastEdit(path: string): string {
|
||||
|
Reference in New Issue
Block a user