Skip to content

Commit

Permalink
npx eslint --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iluha168 committed Jan 26, 2025
1 parent ff3d01e commit 1bb32e8
Show file tree
Hide file tree
Showing 8 changed files with 604 additions and 604 deletions.
214 changes: 107 additions & 107 deletions tools/build/codegen/credits/target.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ts-check
// @ts-check
import Juke from 'juke-build';
import fs from 'fs';
import { randomUUID } from 'crypto';
Expand All @@ -23,20 +23,20 @@ const creditScreenLayoutTemplateFilePath = resolveNeighbourFilePath('pack_credit
const creditScreenContributorTemplateFilePath = resolveNeighbourFilePath('pack_credit_screen_contributor.template.txt');

export const CodegenCreditsTarget = new Juke.Target({
inputs: [
devCapeTemplateFilePath,
devCapesTemplateFilePath,

creditScreenLayoutTemplateFilePath,
creditScreenContributorTemplateFilePath,

contributorsFilePath,
],
outputs: [
devCapesFilePath,
creditScreenLayoutFilePath,
],
executes: () => {
inputs: [
devCapeTemplateFilePath,
devCapesTemplateFilePath,

creditScreenLayoutTemplateFilePath,
creditScreenContributorTemplateFilePath,

contributorsFilePath,
],
outputs: [
devCapesFilePath,
creditScreenLayoutFilePath,
],
executes: () => {
/* TODO: Replace JSDoc by a schema validator with static type inference. */
/**
* @typedef Contributor
Expand All @@ -45,107 +45,107 @@ export const CodegenCreditsTarget = new Juke.Target({
* @prop {string?} description
* @prop {string?} link
*/
/**
/**
* @typedef ContributorsFile
* @prop {Record<string, Contributor>} people
* @prop {Record<Contributor["role"], string>} cape_name_of_role
* @prop {Pick<Contributor, "role"|"description"> & {skin: string}} defaults
*/

/** @type {ContributorsFile} */
const { cape_name_of_role, people, defaults } = readDatafileJSON(contributorsFilePath);

// Apply default values to all people
for (const [name, data] of Object.entries(people)) {
people[name] = Object.assign(Object.create(null), defaults, data);
}

// ----------- Dev_Capes.js -----------
const devCapeTemplate = fs.readFileSync(
devCapeTemplateFilePath,
{ encoding: 'utf8' },
);

const getCapesByRole = (/** @type {Contributor["role"]} */ role) =>
Object
.entries(people)
.filter(([_, data]) => data.mcuuid && data.role.includes(role))
.map(([name, data]) =>
fillTemplates(
devCapeTemplate,
/** @type {ContributorsFile} */
const { cape_name_of_role, people, defaults } = readDatafileJSON(contributorsFilePath);

// Apply default values to all people
for (const [name, data] of Object.entries(people)) {
people[name] = Object.assign(Object.create(null), defaults, data);
}

// ----------- Dev_Capes.js -----------
const devCapeTemplate = fs.readFileSync(
devCapeTemplateFilePath,
{ encoding: 'utf8' },
);

const getCapesByRole = (/** @type {Contributor["role"]} */ role) =>
Object
.entries(people)
.filter(([, data]) => data.mcuuid && data.role.includes(role))
.map(([name, data]) =>
fillTemplates(
devCapeTemplate,
{
'{{NAME}}': name,
// @ts-ignore See filter above
'{{UUID}}': data.mcuuid,
'{{CAPE}}': cape_name_of_role[data.role],
},
)
)
.join('\r\n');

fillTemplateFile(
devCapesTemplateFilePath,
devCapesFilePath,
{
'{{NAME}}': name,
// @ts-ignore See filter above
'{{UUID}}': data.mcuuid,
'{{CAPE}}': cape_name_of_role[data.role],
'//{{CODEGEN_DEVS}}': getCapesByRole('dev'),
'//{{CODEGEN_CONTRIBUTORS}}': getCapesByRole('contributor'),
},
)
)
.join('\r\n');

fillTemplateFile(
devCapesTemplateFilePath,
devCapesFilePath,
{
'//{{CODEGEN_DEVS}}': getCapesByRole('dev'),
'//{{CODEGEN_CONTRIBUTORS}}': getCapesByRole('contributor'),
},
);

// ----------- pack_credit_screen_layout.txt -----------
const startX = 25, startY = 20;
const stepX = 25, stepY = 50;
const maxX = 450;
const hoverOffsetX = -6, hoverOffsetY = 5;
let x = startX, y = startY;

const creditScreenContributorTemplate = fs.readFileSync(
creditScreenContributorTemplateFilePath,
{ encoding: 'utf8' },
);

const creditScreenContributors = Object
.entries(people)
.filter(([_, data]) => !data.role.includes('alt'))
// Bubble up people with skins, as they would be more interesting
.sort((a, b) => Number(!!b[1].mcuuid) - Number(!!a[1].mcuuid))
.map(([name, data]) => {
const elements = fillTemplates(
creditScreenContributorTemplate,
{
'{{MC_UUID}}': data.mcuuid ?? defaults.skin,
'{{V4_UUID}}': data.mcuuid ?? randomUUID(),
'{{PLAYERNAME}}': name,
// "NAME - DESC" if description exists, just "NAME" otherwise
'{{DESCRIPTION}}': [name, data.description].filter((s) => s).join(' - '),
'{{X_SKIN}}': x,
'{{Y_SKIN}}': y,
'{{X_BUTTON}}': x + hoverOffsetX,
'{{Y_BUTTON}}': y + hoverOffsetY,
'{{IF_HAS_LINK}}([^]*?){{IF_HAS_LINK_END}}\n': data.link ? (_, match) => match : '',
'{{LINK}}': data.link ?? '',
'{{BUTTON_EXEC_ACTION_UUID}}': randomUUID(),
'{{BUTTON_EXEC_BLOCK_UUID}}': randomUUID(),
},
);
x += stepX;
if (x > maxX) {
// Next row
x = startX;
y += stepY;
}
return elements;
})
.join('');

fillTemplateFile(
creditScreenLayoutTemplateFilePath,
creditScreenLayoutFilePath,
{
'{{CODEGEN}}': creditScreenContributors,
},
);
},

// ----------- pack_credit_screen_layout.txt -----------
const startX = 25, startY = 20;
const stepX = 25, stepY = 50;
const maxX = 450;
const hoverOffsetX = -6, hoverOffsetY = 5;
let x = startX, y = startY;

const creditScreenContributorTemplate = fs.readFileSync(
creditScreenContributorTemplateFilePath,
{ encoding: 'utf8' },
);

const creditScreenContributors = Object
.entries(people)
.filter(([, data]) => !data.role.includes('alt'))
// Bubble up people with skins, as they would be more interesting
.sort((a, b) => Number(!!b[1].mcuuid) - Number(!!a[1].mcuuid))
.map(([name, data]) => {
const elements = fillTemplates(
creditScreenContributorTemplate,
{
'{{MC_UUID}}': data.mcuuid ?? defaults.skin,
'{{V4_UUID}}': data.mcuuid ?? randomUUID(),
'{{PLAYERNAME}}': name,
// "NAME - DESC" if description exists, just "NAME" otherwise
'{{DESCRIPTION}}': [name, data.description].filter((s) => s).join(' - '),
'{{X_SKIN}}': x,
'{{Y_SKIN}}': y,
'{{X_BUTTON}}': x + hoverOffsetX,
'{{Y_BUTTON}}': y + hoverOffsetY,
'{{IF_HAS_LINK}}([^]*?){{IF_HAS_LINK_END}}\n': data.link ? (_, match) => match : '',
'{{LINK}}': data.link ?? '',
'{{BUTTON_EXEC_ACTION_UUID}}': randomUUID(),
'{{BUTTON_EXEC_BLOCK_UUID}}': randomUUID(),
},
);
x += stepX;
if (x > maxX) {
// Next row
x = startX;
y += stepY;
}
return elements;
})
.join('');

fillTemplateFile(
creditScreenLayoutTemplateFilePath,
creditScreenLayoutFilePath,
{
'{{CODEGEN}}': creditScreenContributors,
},
);
},
});

export default CodegenCreditsTarget;
34 changes: 17 additions & 17 deletions tools/build/codegen/fill_templates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ts-check
// @ts-check
import fs from 'fs';

/**
Expand All @@ -12,15 +12,15 @@ import fs from 'fs';
* @returns {string} Modified string with all matches replaced.
*/
export const fillTemplates = (template, filler) =>
Object.entries(filler)
.reduce((t, [key, value]) => {
const regex = new RegExp(key, 'g');
// TS does not allow for ternary inside replaceAll
if (typeof value === 'function') {
return t.replaceAll(regex, value);
}
return t.replaceAll(regex, value.toString());
}, template);
Object.entries(filler)
.reduce((t, [key, value]) => {
const regex = new RegExp(key, 'g');
// TS does not allow for ternary inside replaceAll
if (typeof value === 'function') {
return t.replaceAll(regex, value);
}
return t.replaceAll(regex, value.toString());
}, template);

/**
* Replaces all specified marker substrings in a file and saves to another file.
Expand All @@ -30,10 +30,10 @@ export const fillTemplates = (template, filler) =>
* @returns {void}
*/
export const fillTemplateFile = (templateFilePath, saveFilePath, filler) =>
fs.writeFileSync(
saveFilePath,
fillTemplates(
fs.readFileSync(templateFilePath, { encoding: 'utf8' }),
filler,
),
);
fs.writeFileSync(
saveFilePath,
fillTemplates(
fs.readFileSync(templateFilePath, { encoding: 'utf8' }),
filler,
),
);
Loading

0 comments on commit 1bb32e8

Please sign in to comment.