Commit b261aa18 authored by nanahira's avatar nanahira

add git ref things

parent 9f838a29
Pipeline #24711 passed with stages
in 3 minutes and 40 seconds
FROM node:lts-bookworm-slim as base
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/log/*
RUN apt update && apt -y install python3 build-essential git && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/log/*
WORKDIR /usr/src/app
COPY ./package*.json ./
......
import fs from 'fs';
import initSqlJs from 'sql.js';
import SQL from 'sql.js';
import child_process from 'child_process';
import util from 'util';
const execFileAsync = util.promisify(child_process.execFile);
const systemStrings = new Map<number, string>();
const setcodeStrings = new Map<number, string>();
......@@ -122,6 +125,12 @@ interface YGOProCard extends YGOProCardLike {
sets: string[];
overallString: string;
picUrl: string;
createTime?: number;
createCommit?: string;
updateTime?: number;
updateCommit?: string;
created?: boolean;
updated?: boolean;
}
const ygoproConstants = {
......@@ -202,7 +211,86 @@ function getMetaText(data: Partial<YGOProCard>) {
return lines.join(' ');
}
function formatCard(data: YGOProCardLike): YGOProCard {
async function getCardHistory(data: YGOProCardLike) {
const gitPath = process.env.GIT_PATH;
if (!gitPath) return;
const filename =
data.type ===
(ygoproConstants.TYPES.TYPE_MONSTER | ygoproConstants.TYPES.TYPE_NORMAL)
? `pics/${data.id}.jpg`
: `script/c${data.id}.lua`;
const { stdout } = await execFileAsync(
'git',
['log', '--follow', '--format=%ct,%H', '--', filename],
{
cwd: gitPath,
},
);
if (!stdout?.length) return;
const stdoutLines = stdout.trim().split('\n');
const commits = stdoutLines.map((line) => {
const [timestamp, commit] = line.split(',');
return {
timestamp: parseInt(timestamp),
commit,
};
});
const createCommit = commits[commits.length - 1];
const updateCommit = commits[0];
const refCommitHash = process.env.REF_COMMIT;
const res: {
createTime: number;
createCommit: string;
updateTime: number;
updateCommit: string;
created?: boolean;
updated?: boolean;
} = {
createTime: createCommit.timestamp,
createCommit: createCommit.commit,
updateTime: updateCommit.timestamp,
updateCommit: updateCommit.commit,
};
if (refCommitHash) {
// diff against ref commit to see if the card is created/updated
const { stdout: createdStdout } = await execFileAsync(
'git',
['diff', '--name-only', '--diff-filter=A', refCommitHash, '--', filename],
{
cwd: gitPath,
},
);
if (createdStdout?.trim()?.length) {
res.created = true;
res.updated = false;
} else {
res.created = false;
// diff against ref commit to see if the card is updated
const { stdout: updatedStdout } = await execFileAsync(
'git',
[
'diff',
'--name-only',
'--diff-filter=M',
refCommitHash,
'--',
filename,
],
{
cwd: gitPath,
},
);
if (updatedStdout?.trim()?.length) {
res.updated = true;
} else {
res.updated = false;
}
}
}
return res;
}
async function formatCard(data: YGOProCardLike): Promise<YGOProCard> {
const result: Partial<YGOProCard> = {
...data,
displayLevel: data.level & 0xff,
......@@ -247,6 +335,15 @@ function formatCard(data: YGOProCardLike): YGOProCard {
'https://cdn02.moecube.com:444/images/ygopro-images-zh-CN/') +
data.id.toString() +
(process.env.YGOPRO_PIC_URL_SUFFIX || '.jpg');
const history = (await getCardHistory(data)) || {
createTime: 0,
createCommit: '',
updateTime: 0,
updateCommit: '',
};
Object.assign(result, history);
if (process.env.FIELDS) {
const fields = new Set(process.env.FIELDS.split(','));
// keep only the fields specified
......@@ -269,7 +366,7 @@ async function main() {
db,
'select datas.*,texts.name,texts.desc from datas,texts where datas.id = texts.id and datas.type & 0x4000 = 0 and (datas.alias = 0 or datas.id - datas.alias > 10 or datas.id - datas.alias < -10)',
);
const formattedCards = cards.map(formatCard);
const formattedCards = await Promise.all(cards.map(formatCard));
console.log(JSON.stringify(formattedCards, null, 2));
process.exit(0);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment