Skip to content

Commit

Permalink
fix server build error, optimize folderSize, experimental _html
Browse files Browse the repository at this point in the history
  • Loading branch information
schlawg committed Jan 11, 2025
1 parent 242a9fa commit a2474f4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion modules/local/src/main/LocalRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lila.local

import reactivemongo.api.Cursor
import reactivemongo.api.bson.*
import lila.common.Json.opaqueFormat
import lila.common.Json.given
import lila.db.JSON
import play.api.libs.json.*

Expand Down
11 changes: 11 additions & 0 deletions ui/.build/src/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ export async function stopEsbuildWatch(): Promise<void> {
}

const plugins = [
// {
// name: '_html', // experimental
// setup(build: es.PluginBuild) {
// build.onLoad({ filter: /\.ts$/ }, async (args: es.OnLoadArgs) => ({
// loader: 'ts',
// contents: (await fs.promises.readFile(args.path, 'utf8')).replace(/_html`([\s\S]*?)`/g, (_, s) =>
// env.prod ? `\`${s.replace(/\s+/g, ' ').replaceAll('> <', '><').trim()}\`` : `\`${s}\``,
// ),
// }));
// },
// },
{
name: 'onBundleDone',
setup(build: es.PluginBuild) {
Expand Down
20 changes: 12 additions & 8 deletions ui/.build/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ export async function globArrays(globs: string[] | undefined, opts: fg.Options =
}

export async function folderSize(folder: string): Promise<number> {
let totalSize = 0;
async function getSize(dir: string): Promise<number> {
const entries = await fs.promises.readdir(dir, { withFileTypes: true });

async function getSize(dir: string) {
for (const file of await fs.promises.readdir(dir, { withFileTypes: true })) {
if (file.isDirectory()) await getSize(path.join(dir, file.name));
else if (file.isFile()) totalSize += (await fs.promises.stat(path.join(dir, file.name))).size;
}
const sizes = await Promise.all(
entries.map(async entry => {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) return getSize(fullPath);
if (entry.isFile()) return (await fs.promises.stat(fullPath)).size;
return 0;
}),
);
return sizes.reduce((acc: number, size: number) => acc + size, 0);
}
await getSize(folder);
return totalSize;
return getSize(folder);
}

export async function readable(file: string): Promise<boolean> {
Expand Down
1 change: 1 addition & 0 deletions ui/@types/lichess/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,4 @@ declare const site: Site;
declare const fipr: Fipr;
declare const i18n: I18n;
declare module 'tablesort';
declare const _html: (s: TemplateStringsArray, ...k: any) => string; // file://./../../.build/src/esbuild.ts
7 changes: 3 additions & 4 deletions ui/bits/src/bits.polyglot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ async function makeBookPgn(
if (filter && !filter(game)) continue;
traverseTree(co.pgn.startingPosition(game.headers).unwrap(), game.moves, maxPly);
}
// calc weights and collapse secondary maps into arrays for less memory overhead
const posMap = new Map<bigint, OpeningMove[]>();
for (const [hash, map] of bigMap) {
const moves = Array.from(map, ([uci, weight]) => ({ uci, weight }));
Expand Down Expand Up @@ -127,16 +126,16 @@ async function* pgnFromBlob(blob: Blob, chunkSize: number, progress?: PgnProgres
const textChunk = await chunk.text();
const crlfLast = textChunk.lastIndexOf('\r\n\r\n[');
const lfLast = textChunk.lastIndexOf('\n\n[');
const gamesThisChunk =
const wholePgnsChunk =
offset + chunk.size === totalSize || Math.max(crlfLast, lfLast) === -1
? textChunk
: textChunk.slice(0, Math.max(lfLast + 2, crlfLast + 4));
const games = co.pgn.parsePgn(residual + gamesThisChunk).filter(game => {
const games = co.pgn.parsePgn(residual + wholePgnsChunk).filter(game => {
const tag = game.headers.get('Variant');
return !tag || tag.toLowerCase() === 'standard';
});
for (const game of games) yield game;
residual = textChunk.slice(gamesThisChunk.length);
residual = textChunk.slice(wholePgnsChunk.length);
offset += chunkSize;
}
}
Expand Down

0 comments on commit a2474f4

Please sign in to comment.