libroadcast-cli 2.4.2 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cmd/push.js CHANGED
@@ -1,67 +1,16 @@
1
1
  import { exit } from "node:process";
2
- import { readFile } from "node:fs/promises";
3
- import path from "node:path";
4
- import { client, msgCommonErrorHelp, sleep, checkTokenScopes, packageJson, } from "../utils/commandHandler.js";
2
+ import { msgCommonErrorHelp, sleep, checkTokenScopes, } from "../utils/commandHandler.js";
5
3
  import { getBroadcastRound } from "../utils/getInfoBroadcast.js";
6
4
  import cl from "../utils/colors.js";
7
- const pushPGN = async (round, pgn) => {
8
- try {
9
- const res = await client
10
- .POST("/api/broadcast/round/{broadcastRoundId}/push", {
11
- params: {
12
- path: { broadcastRoundId: round.id },
13
- },
14
- body: pgn,
15
- bodySerializer: (body) => body,
16
- })
17
- .then((response) => response.data);
18
- console.log(cl.green(`✓ Successfully pushed PGN for round ${cl.whiteBold(round.id)}.`));
19
- console.table(res?.games.map((game, i) => {
20
- return {
21
- "Game #": i + 1,
22
- "White Player": game.tags["White"] || "Unknown",
23
- "Black Player": game.tags["Black"] || "Unknown",
24
- Result: game.tags["Result"] || "Unknown",
25
- "Ply Count": game.moves || "Unknown",
26
- Error: game.error || "None",
27
- };
28
- }));
29
- }
30
- catch (error) {
31
- console.error(cl.red(`Error pushing PGN for round ${cl.whiteBold(round.id)}:`), error);
32
- }
33
- };
34
- const readPGNFromURL = async (pgnURL) => {
35
- if (pgnURL.startsWith("http://") || pgnURL.startsWith("https://")) {
36
- const response = await fetch(pgnURL, {
37
- method: "GET",
38
- headers: {
39
- "User-Agent": packageJson.name + "/" + packageJson.version,
40
- },
41
- });
42
- if (!response.ok) {
43
- console.error(cl.red(`Failed to fetch PGN from URL: ${response.statusText}`));
44
- return undefined;
45
- }
46
- const pgnText = await response.text();
47
- return pgnText;
48
- }
49
- else {
50
- const resolvedPath = path.resolve(pgnURL);
51
- const stats = await readFile(resolvedPath, { encoding: "utf-8" }).catch((err) => {
52
- console.error(cl.red(`Failed to read PGN file: ${err.message}`));
53
- return undefined;
54
- });
55
- if (!stats)
56
- return undefined;
57
- return stats.toString();
58
- }
59
- };
5
+ import { pushPGN, readPGNFromURL } from "../utils/pushTools.js";
6
+ let lastPGN = "";
60
7
  const loop = async (roundInfo, pgnPath, loopTimer) => {
61
8
  while (true) {
62
9
  const pgnContent = await readPGNFromURL(pgnPath);
63
- if (pgnContent)
10
+ if (pgnContent && pgnContent !== lastPGN) {
64
11
  await pushPGN(roundInfo, pgnContent);
12
+ lastPGN = pgnContent;
13
+ }
65
14
  await sleep(loopTimer * 1000);
66
15
  }
67
16
  };
@@ -1,37 +1,9 @@
1
1
  import { exit } from "node:process";
2
- import { readFile } from "node:fs/promises";
3
- import path from "node:path";
4
- import { client, msgCommonErrorHelp, sleep, checkTokenScopes, packageJson, } from "../utils/commandHandler.js";
2
+ import { msgCommonErrorHelp, sleep, checkTokenScopes, } from "../utils/commandHandler.js";
5
3
  import { parsePgn, makePgn } from "chessops/pgn";
6
4
  import { getBroadcastRound } from "../utils/getInfoBroadcast.js";
7
5
  import cl from "../utils/colors.js";
8
- const pushPGN = async (round, pgn) => {
9
- try {
10
- const res = await client
11
- .POST("/api/broadcast/round/{broadcastRoundId}/push", {
12
- params: {
13
- path: { broadcastRoundId: round.id },
14
- },
15
- body: pgn,
16
- bodySerializer: (body) => body,
17
- })
18
- .then((response) => response.data);
19
- console.log(cl.green(`✓ Successfully pushed PGN for round ${cl.whiteBold(round.id)}.`));
20
- console.table(res?.games.map((game, i) => {
21
- return {
22
- "Game #": i + 1,
23
- "White Player": game.tags["White"] || "Unknown",
24
- "Black Player": game.tags["Black"] || "Unknown",
25
- Result: game.tags["Result"] || "Unknown",
26
- "Ply Count": game.moves ?? "Unknown",
27
- Error: game.error || "None",
28
- };
29
- }));
30
- }
31
- catch (error) {
32
- console.error(cl.red(`Error pushing PGN for round ${cl.whiteBold(round.id)}:`), error);
33
- }
34
- };
6
+ import { pushPGN, readPGNFromURL } from "../utils/pushTools.js";
35
7
  const filterPgnByIds = (pgn, filterIds) => {
36
8
  const parsed = parsePgn(pgn);
37
9
  const filteredGames = parsed.filter((game) => {
@@ -56,44 +28,19 @@ const filterPgnByIds = (pgn, filterIds) => {
56
28
  });
57
29
  return filteredGames.map((game) => makePgn(game)).join("\n\n");
58
30
  };
59
- const readPGNFromURL = async (pgnURL, filterIds) => {
60
- if (pgnURL.startsWith("http://") || pgnURL.startsWith("https://")) {
61
- const response = await fetch(pgnURL, {
62
- method: "GET",
63
- headers: {
64
- "User-Agent": packageJson.name + "/" + packageJson.version,
65
- },
66
- });
67
- if (!response.ok) {
68
- console.error(cl.red(`Failed to fetch PGN from URL: ${response.statusText}`));
69
- return undefined;
70
- }
71
- const pgnText = await response.text();
72
- const filteredPgn = filterPgnByIds(pgnText, filterIds);
73
- if (!filteredPgn) {
74
- console.error(cl.red(`No games found matching the provided filter IDs.`));
75
- return undefined;
76
- }
77
- return filteredPgn;
78
- }
79
- else {
80
- const resolvedPath = path.resolve(pgnURL);
81
- const stats = await readFile(resolvedPath, { encoding: "utf-8" }).catch((err) => {
82
- console.error(cl.red(`Failed to read PGN file: ${err.message}`));
83
- return undefined;
84
- });
85
- if (!stats)
86
- return undefined;
87
- return stats.toString();
88
- }
89
- };
90
31
  let lastPGN = "";
91
32
  const loop = async (roundInfo, pgnPath, loopTimer, filterIds) => {
92
33
  while (true) {
93
- const pgnContent = await readPGNFromURL(pgnPath, filterIds);
94
- if (pgnContent && pgnContent !== lastPGN) {
95
- await pushPGN(roundInfo, pgnContent);
96
- lastPGN = pgnContent;
34
+ const pgnContent = await readPGNFromURL(pgnPath);
35
+ if (!pgnContent) {
36
+ console.error(cl.red(`Failed to read PGN content. Retrying in ${loopTimer} seconds...`));
37
+ await sleep(loopTimer * 1000);
38
+ continue;
39
+ }
40
+ const filteredPgn = filterPgnByIds(pgnContent, filterIds);
41
+ if (filteredPgn && filteredPgn !== lastPGN) {
42
+ await pushPGN(roundInfo, filteredPgn);
43
+ lastPGN = filteredPgn;
97
44
  }
98
45
  await sleep(loopTimer * 1000);
99
46
  }
@@ -130,8 +77,13 @@ export const pushFilterIDCommand = async (args) => {
130
77
  await loop(roundInfo, pgnPath, loopTimer, filterIds);
131
78
  }
132
79
  else {
133
- const pgnContent = await readPGNFromURL(pgnPath, filterIds);
134
- if (pgnContent)
135
- await pushPGN(roundInfo, pgnContent);
80
+ const pgnContent = await readPGNFromURL(pgnPath);
81
+ if (!pgnContent) {
82
+ console.error(cl.red(`Failed to read PGN content.`));
83
+ exit(1);
84
+ }
85
+ const filteredPgn = filterPgnByIds(pgnContent, filterIds);
86
+ if (filteredPgn)
87
+ await pushPGN(roundInfo, filteredPgn);
136
88
  }
137
89
  };
@@ -0,0 +1,62 @@
1
+ import { client, packageJson } from "./commandHandler.js";
2
+ import cl from "./colors.js";
3
+ import path from "node:path";
4
+ import { readFile } from "node:fs/promises";
5
+ export const pushPGN = async (round, pgn) => {
6
+ try {
7
+ const res = await client
8
+ .POST("/api/broadcast/round/{broadcastRoundId}/push", {
9
+ params: {
10
+ path: { broadcastRoundId: round.id },
11
+ },
12
+ body: pgn,
13
+ bodySerializer: (body) => body,
14
+ })
15
+ .then((response) => response.data);
16
+ console.log(cl.green(`✓ Successfully pushed PGN for round ${cl.whiteBold(round.id)}.`));
17
+ console.table(res?.games
18
+ .map((game, i) => {
19
+ return {
20
+ id: i + 1,
21
+ "White Player": game.tags["White"] || "Unknown",
22
+ "Black Player": game.tags["Black"] || "Unknown",
23
+ Result: game.tags["Result"] || "Unknown",
24
+ "Ply Count": game.moves ?? "Unknown",
25
+ Error: game.error || "None",
26
+ };
27
+ })
28
+ .reduce((acc, { id, ...rest }) => {
29
+ acc[id] = rest;
30
+ return acc;
31
+ }, {}));
32
+ }
33
+ catch (error) {
34
+ console.error(cl.red(`Error pushing PGN for round ${cl.whiteBold(round.id)}:`), error);
35
+ }
36
+ };
37
+ export const readPGNFromURL = async (pgnURL) => {
38
+ if (pgnURL.startsWith("http://") || pgnURL.startsWith("https://")) {
39
+ const response = await fetch(pgnURL, {
40
+ method: "GET",
41
+ headers: {
42
+ "User-Agent": packageJson.name + "/" + packageJson.version,
43
+ },
44
+ });
45
+ if (!response.ok) {
46
+ console.error(cl.red(`Failed to fetch PGN from URL: ${response.statusText}`));
47
+ return undefined;
48
+ }
49
+ const pgnText = await response.text();
50
+ return pgnText;
51
+ }
52
+ else {
53
+ const resolvedPath = path.resolve(pgnURL);
54
+ const stats = await readFile(resolvedPath, { encoding: "utf-8" }).catch((err) => {
55
+ console.error(cl.red(`Failed to read PGN file: ${err.message}`));
56
+ return undefined;
57
+ });
58
+ if (!stats)
59
+ return undefined;
60
+ return stats.toString();
61
+ }
62
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libroadcast-cli",
3
- "version": "2.4.2",
3
+ "version": "2.5.0",
4
4
  "description": "CLI to help with broadcast maintenance on Lichess",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [