libroadcast-cli 2.8.0 → 2.9.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/README.md CHANGED
@@ -86,6 +86,11 @@ Commands:
86
86
  Convert Lichess Usernames source to Lichess Game IDs source for the specified broadcast round.
87
87
  Note: This command will fetch the source PGN, extract Lichess Game IDs from the PGN headers, and update the broadcast round to use these game IDs as the source.
88
88
 
89
+ pushReorder <roundId> <PGNFromPathOrUrl> [--loop <intervalInSeconds>]
90
+ Upload a PGN file from a local path or URL to the specified broadcast round, reordering games by round number.
91
+ Note: The PGN file must be accessible from the provided path or URL. Games will be reordered based on the 'Round' header in the PGN.
92
+ Options:
93
+ --loop <intervalInSeconds> Continuously push the PGN file at the specified interval in seconds.
89
94
 
90
95
  Examples:
91
96
  # Login with your Lichess token (interactive)
@@ -0,0 +1,61 @@
1
+ import { exit } from "node:process";
2
+ import { msgCommonErrorHelp, sleep, checkTokenScopes, } from "../utils/commandHandler.js";
3
+ import { parsePgn, makePgn } from "chessops/pgn";
4
+ import { getBroadcastRound } from "../utils/getInfoBroadcast.js";
5
+ import cl from "../utils/colors.js";
6
+ import { loopChecker, pushPGN, readPGNFromURL } from "../utils/pushTools.js";
7
+ const sortPgn = (pgn) => {
8
+ const parsed = parsePgn(pgn);
9
+ let sortedGames = parsed.sort((a, b) => {
10
+ const roundA = parseInt(a.headers.get("Round")?.split(".")[1] || "0", 10);
11
+ const roundB = parseInt(b.headers.get("Round")?.split(".")[1] || "0", 10);
12
+ return roundA - roundB;
13
+ });
14
+ return sortedGames.map((game) => makePgn(game)).join("\n\n");
15
+ };
16
+ let lastPGN = "";
17
+ const loop = async (roundInfo, pgnPath, loopTimer) => {
18
+ while (true) {
19
+ const pgnContent = await readPGNFromURL(pgnPath);
20
+ if (!pgnContent) {
21
+ console.error(cl.red(`Failed to read PGN content. Retrying in ${loopTimer} seconds...`));
22
+ await sleep(loopTimer * 1000);
23
+ continue;
24
+ }
25
+ const filteredPgn = sortPgn(pgnContent);
26
+ if (filteredPgn && filteredPgn !== lastPGN) {
27
+ await pushPGN(roundInfo, filteredPgn);
28
+ lastPGN = filteredPgn;
29
+ }
30
+ await sleep(loopTimer * 1000);
31
+ }
32
+ };
33
+ export const pushReorderCommand = async (args) => {
34
+ await checkTokenScopes();
35
+ const [roundId, pgnPath] = args.slice(0, 2);
36
+ if (!roundId || !pgnPath) {
37
+ msgCommonErrorHelp("Round ID and PGN path are required.");
38
+ exit(1);
39
+ }
40
+ const roundInfo = await getBroadcastRound(roundId);
41
+ if (!roundInfo) {
42
+ console.error(cl.red("Round not found."));
43
+ exit(1);
44
+ }
45
+ const loopTimer = loopChecker(args);
46
+ if (loopTimer) {
47
+ console.log(cl.green(`Starting loop to push reordered PGN every ${cl.whiteBold(loopTimer.toString())} seconds...`));
48
+ console.log(cl.blue("Press Ctrl+C to stop."));
49
+ await loop(roundInfo, pgnPath, loopTimer);
50
+ }
51
+ else {
52
+ const pgnContent = await readPGNFromURL(pgnPath);
53
+ if (!pgnContent) {
54
+ console.error(cl.red(`Failed to read PGN content.`));
55
+ exit(1);
56
+ }
57
+ const filteredPgn = sortPgn(pgnContent);
58
+ if (filteredPgn)
59
+ await pushPGN(roundInfo, filteredPgn);
60
+ }
61
+ };
@@ -17,6 +17,7 @@ import { pushFilterIDCommand } from "../cmd/pushFilterID.js";
17
17
  import { loginCommand } from "../cmd/login.js";
18
18
  import { getStoredCredentials } from "./credentials.js";
19
19
  import { convertNamesToIDCommand } from "../cmd/convertNamesToID.js";
20
+ import { pushReorderCommand } from "../cmd/pushReorder.js";
20
21
  const getToken = () => {
21
22
  const stored = getStoredCredentials();
22
23
  const envToken = process.env.LICHESS_TOKEN;
@@ -47,6 +48,7 @@ export var Command;
47
48
  Command["Push"] = "push";
48
49
  Command["PushFilterID"] = "pushFilterID";
49
50
  Command["ConvertNamesToID"] = "convertNamesToID";
51
+ Command["PushReorder"] = "pushReorder";
50
52
  })(Command || (Command = {}));
51
53
  export const commands = new Map([
52
54
  [Command.Login, loginCommand],
@@ -61,6 +63,7 @@ export const commands = new Map([
61
63
  [Command.Push, pushCommand],
62
64
  [Command.PushFilterID, pushFilterIDCommand],
63
65
  [Command.ConvertNamesToID, convertNamesToIDCommand],
66
+ [Command.PushReorder, pushReorderCommand],
64
67
  ]);
65
68
  export const client = createClient({
66
69
  baseUrl: LICHESS_DOMAIN,
@@ -87,6 +87,13 @@ const helpConvertNamesToID = [
87
87
  ` ${cl.gray("Convert Lichess Usernames source to Lichess Game IDs source for the specified broadcast round.")}`,
88
88
  ` ${cl.bold("Note:")} ${cl.gray("This command will fetch the source PGN, extract Lichess Game IDs from the PGN headers, and update the broadcast round to use these game IDs as the source.")}`,
89
89
  ].join("\n");
90
+ const helpPushReorder = [
91
+ ` ${cl.underItalic("pushReorder <roundId> <PGNFromPathOrUrl> [--loop <intervalInSeconds>]")}`,
92
+ ` ${cl.gray("Upload a PGN file from a local path or URL to the specified broadcast round, reordering games by round number.")}`,
93
+ ` ${cl.bold("Note:")} ${cl.gray("The PGN file must be accessible from the provided path or URL. Games will be reordered based on the 'Round' header in the PGN.")}`,
94
+ ` ${cl.bold("Options:")}`,
95
+ ` --loop <intervalInSeconds> ${cl.gray("Continuously push the PGN file at the specified interval in seconds.")}`,
96
+ ].join("\n");
90
97
  const msg = [
91
98
  `${cl.boldYellow("Usage:")} ${cl.underItalic("<command> [options]")}`,
92
99
  ``,
@@ -116,6 +123,7 @@ const msg = [
116
123
  ``,
117
124
  helpConvertNamesToID,
118
125
  ``,
126
+ helpPushReorder,
119
127
  ``,
120
128
  `${cl.boldYellow("Examples:")}`,
121
129
  ` ${cl.gray("# Login with your Lichess token (interactive)")}`,
@@ -161,6 +169,7 @@ export const showHelp = (cmd) => {
161
169
  [Command.Push]: helpPush,
162
170
  [Command.PushFilterID]: helpPushFilterID,
163
171
  [Command.ConvertNamesToID]: helpConvertNamesToID,
172
+ [Command.PushReorder]: helpPushReorder,
164
173
  };
165
174
  const range = cmd ? ranges[cmd] : undefined;
166
175
  console.info(range ? range : msg.join("\n"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libroadcast-cli",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "CLI to help with broadcast maintenance on Lichess",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [