libroadcast-cli 2.4.0 → 2.4.2

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/delay.js CHANGED
@@ -1,19 +1,13 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.delayCommand = void 0;
7
- const node_process_1 = require("node:process");
8
- const commandHandler_1 = require("../utils/commandHandler");
9
- const getInfoBroadcast_1 = require("../utils/getInfoBroadcast");
10
- const colors_1 = __importDefault(require("../utils/colors"));
1
+ import { exit } from "node:process";
2
+ import { client, msgCommonErrorHelp, sleep, handleApiResponse, translateRoundsToFix, checkTokenScopes, } from "../utils/commandHandler.js";
3
+ import { getBroadcast } from "../utils/getInfoBroadcast.js";
4
+ import cl from "../utils/colors.js";
11
5
  const setDelayRounds = async (rounds, delay, onlyDelay, noDelay, roundsToFix) => {
12
6
  let filteredRounds = rounds.filter((_, i) => !roundsToFix?.length || roundsToFix.includes(i + 1));
13
7
  if (filteredRounds.length === 0)
14
8
  filteredRounds = rounds;
15
9
  for (const round of filteredRounds) {
16
- await (0, commandHandler_1.handleApiResponse)(commandHandler_1.client.POST("/broadcast/round/{broadcastRoundId}/edit", {
10
+ await handleApiResponse(client.POST("/broadcast/round/{broadcastRoundId}/edit", {
17
11
  params: {
18
12
  path: { broadcastRoundId: round.id },
19
13
  query: { patch: 1 },
@@ -24,39 +18,38 @@ const setDelayRounds = async (rounds, delay, onlyDelay, noDelay, roundsToFix) =>
24
18
  ? round.startsAt + delay * 1000
25
19
  : undefined,
26
20
  },
27
- }), `Successfully set delay for round ${colors_1.default.whiteBold(round.id)} to ${colors_1.default.whiteBold(delay.toString())} seconds.`, `Error setting delay for round ${colors_1.default.whiteBold(round.id)}`);
28
- await (0, commandHandler_1.sleep)(200);
21
+ }), `Successfully set delay for round ${cl.whiteBold(round.id)} to ${cl.whiteBold(delay.toString())} seconds.`, `Error setting delay for round ${cl.whiteBold(round.id)}`);
22
+ await sleep(200);
29
23
  }
30
24
  };
31
- const delayCommand = async (args) => {
32
- await (0, commandHandler_1.checkTokenScopes)();
25
+ export const delayCommand = async (args) => {
26
+ await checkTokenScopes();
33
27
  const [broadcastId, delay] = args.slice(0, 2);
34
28
  if (!broadcastId || !delay) {
35
- (0, commandHandler_1.msgCommonErrorHelp)("Broadcast ID and delay are required.");
36
- (0, node_process_1.exit)(1);
29
+ msgCommonErrorHelp("Broadcast ID and delay are required.");
30
+ exit(1);
37
31
  }
38
32
  const delayNum = parseInt(delay, 10);
39
33
  if (isNaN(delayNum) || delayNum < 0 || delayNum > 3600) {
40
- (0, commandHandler_1.msgCommonErrorHelp)("Delay must be a number between 0 and 3600 seconds.");
41
- (0, node_process_1.exit)(1);
34
+ msgCommonErrorHelp("Delay must be a number between 0 and 3600 seconds.");
35
+ exit(1);
42
36
  }
43
37
  const onlyDelay = args.includes("--onlyDelay");
44
38
  const noDelay = args.includes("--noDelay");
45
39
  if (onlyDelay && noDelay) {
46
- console.error(colors_1.default.red("Cannot use --onlyDelay and --noDelay together."));
47
- (0, node_process_1.exit)(1);
40
+ console.error(cl.red("Cannot use --onlyDelay and --noDelay together."));
41
+ exit(1);
48
42
  }
49
43
  const roundsArgIndex = args.findIndex((arg) => arg === "--rounds");
50
44
  let roundsToFix = undefined;
51
45
  if (roundsArgIndex !== -1 && roundsArgIndex + 1 < args.length) {
52
46
  const roundsArg = args[roundsArgIndex + 1];
53
- roundsToFix = roundsArg ? (0, commandHandler_1.translateRoundsToFix)(roundsArg) : undefined;
47
+ roundsToFix = roundsArg ? translateRoundsToFix(roundsArg) : undefined;
54
48
  }
55
- const broadcast = await (0, getInfoBroadcast_1.getBroadcast)(broadcastId);
49
+ const broadcast = await getBroadcast(broadcastId);
56
50
  if (!broadcast?.rounds || broadcast.rounds.length === 0) {
57
- console.error(colors_1.default.red("No rounds found for the specified broadcast."));
58
- (0, node_process_1.exit)(1);
51
+ console.error(cl.red("No rounds found for the specified broadcast."));
52
+ exit(1);
59
53
  }
60
54
  await setDelayRounds(broadcast.rounds, delayNum, onlyDelay, noDelay, roundsToFix);
61
55
  };
62
- exports.delayCommand = delayCommand;
@@ -1,24 +1,18 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.fixScheduleCommand = void 0;
7
- const node_process_1 = require("node:process");
8
- const commandHandler_1 = require("../utils/commandHandler");
9
- const getInfoBroadcast_1 = require("../utils/getInfoBroadcast");
10
- const colors_1 = __importDefault(require("../utils/colors"));
11
- const ms_1 = require("ms");
1
+ import { exit } from "node:process";
2
+ import { client, msgCommonErrorHelp, sleep, handleApiResponse, translateRoundsToFix, checkTokenScopes, } from "../utils/commandHandler.js";
3
+ import { getBroadcast } from "../utils/getInfoBroadcast.js";
4
+ import cl from "../utils/colors.js";
5
+ import { parse as ms } from "ms";
12
6
  const fixScheduleRounds = async (rounds, timeDiff, roundsToFix) => {
13
7
  rounds = rounds
14
8
  .filter((_, i) => !roundsToFix?.length || roundsToFix.includes(i + 1))
15
9
  .filter((el) => el.startsAt !== undefined);
16
10
  if (rounds.length === 0) {
17
- console.error(colors_1.default.red("No rounds to fix after applying filters."));
18
- (0, node_process_1.exit)(1);
11
+ console.error(cl.red("No rounds to fix after applying filters."));
12
+ exit(1);
19
13
  }
20
14
  for (const round of rounds) {
21
- await (0, commandHandler_1.handleApiResponse)(commandHandler_1.client.POST("/broadcast/round/{broadcastRoundId}/edit", {
15
+ await handleApiResponse(client.POST("/broadcast/round/{broadcastRoundId}/edit", {
22
16
  params: {
23
17
  path: { broadcastRoundId: round.id },
24
18
  query: { patch: 1 },
@@ -26,34 +20,33 @@ const fixScheduleRounds = async (rounds, timeDiff, roundsToFix) => {
26
20
  body: {
27
21
  startsAt: round.startsAt + timeDiff,
28
22
  },
29
- }), `Successfully fixed schedule for round ${colors_1.default.whiteBold(round.id)}.`, `Error fixing schedule for round ${colors_1.default.whiteBold(round.id)}`);
30
- await (0, commandHandler_1.sleep)(200);
23
+ }), `Successfully fixed schedule for round ${cl.whiteBold(round.id)}.`, `Error fixing schedule for round ${cl.whiteBold(round.id)}`);
24
+ await sleep(200);
31
25
  }
32
26
  };
33
- const fixScheduleCommand = async (args) => {
34
- await (0, commandHandler_1.checkTokenScopes)();
27
+ export const fixScheduleCommand = async (args) => {
28
+ await checkTokenScopes();
35
29
  const [broadcastId, timeDiffStr] = args.slice(0, 2);
36
30
  if (!broadcastId || !timeDiffStr) {
37
- (0, commandHandler_1.msgCommonErrorHelp)("Broadcast ID and time difference are required.");
38
- (0, node_process_1.exit)(1);
31
+ msgCommonErrorHelp("Broadcast ID and time difference are required.");
32
+ exit(1);
39
33
  }
40
- const timeDiff = (0, ms_1.parse)(timeDiffStr);
34
+ const timeDiff = ms(timeDiffStr);
41
35
  if (isNaN(timeDiff)) {
42
- console.error(colors_1.default.red("Error: Time difference must be a valid duration string (e.g., '1h', '30m', '15s')."));
43
- (0, node_process_1.exit)(1);
36
+ console.error(cl.red("Error: Time difference must be a valid duration string (e.g., '1h', '30m', '15s')."));
37
+ exit(1);
44
38
  }
45
- console.log(`Applying time difference of ${colors_1.default.whiteBold(timeDiffStr)} (${colors_1.default.whiteBold(timeDiff.toString())} ms) to broadcast ${colors_1.default.whiteBold(broadcastId)}.`);
39
+ console.log(`Applying time difference of ${cl.whiteBold(timeDiffStr)} (${cl.whiteBold(timeDiff.toString())} ms) to broadcast ${cl.whiteBold(broadcastId)}.`);
46
40
  const roundsArgIndex = args.findIndex((arg) => arg === "--rounds");
47
41
  let roundsToFix = undefined;
48
42
  if (roundsArgIndex !== -1 && roundsArgIndex + 1 < args.length) {
49
43
  const roundsArg = args[roundsArgIndex + 1];
50
- roundsToFix = roundsArg ? (0, commandHandler_1.translateRoundsToFix)(roundsArg) : undefined;
44
+ roundsToFix = roundsArg ? translateRoundsToFix(roundsArg) : undefined;
51
45
  }
52
- const broadcast = await (0, getInfoBroadcast_1.getBroadcast)(broadcastId);
46
+ const broadcast = await getBroadcast(broadcastId);
53
47
  if (!broadcast?.rounds || broadcast.rounds.length === 0) {
54
- console.error(colors_1.default.red(`Broadcast with ID ${colors_1.default.whiteBold(broadcastId)} not found or has no rounds.`));
55
- (0, node_process_1.exit)(1);
48
+ console.error(cl.red(`Broadcast with ID ${cl.whiteBold(broadcastId)} not found or has no rounds.`));
49
+ exit(1);
56
50
  }
57
51
  await fixScheduleRounds(broadcast.rounds, timeDiff, roundsToFix);
58
52
  };
59
- exports.fixScheduleCommand = fixScheduleCommand;
package/dist/cmd/login.js CHANGED
@@ -1,23 +1,17 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.loginCommand = void 0;
7
- const node_process_1 = require("node:process");
8
- const node_readline_1 = require("node:readline");
9
- const credentials_1 = require("../utils/credentials");
10
- const colors_1 = __importDefault(require("../utils/colors"));
11
- const loginCommand = async (args) => {
1
+ import { exit } from "node:process";
2
+ import { createInterface } from "node:readline";
3
+ import { saveCredentials, clearCredentials, fetchTokenScopes, } from "../utils/credentials.js";
4
+ import cl from "../utils/colors.js";
5
+ export const loginCommand = async (args) => {
12
6
  if (args.includes("--logout") || args.includes("-lo")) {
13
7
  try {
14
- (0, credentials_1.clearCredentials)();
15
- console.log(colors_1.default.green("✓ Credentials cleared successfully. You are now logged out."));
8
+ clearCredentials();
9
+ console.log(cl.green("✓ Credentials cleared successfully. You are now logged out."));
16
10
  return;
17
11
  }
18
12
  catch (error) {
19
- console.error(colors_1.default.red("Error clearing credentials:"), error);
20
- (0, node_process_1.exit)(1);
13
+ console.error(cl.red("Error clearing credentials:"), error);
14
+ exit(1);
21
15
  }
22
16
  }
23
17
  try {
@@ -25,7 +19,7 @@ const loginCommand = async (args) => {
25
19
  let token = positionalArgs[0];
26
20
  let domain = positionalArgs[1] || "https://lichess.org";
27
21
  if (!token) {
28
- const readline = (0, node_readline_1.createInterface)({
22
+ const readline = createInterface({
29
23
  input: process.stdin,
30
24
  output: process.stdout,
31
25
  });
@@ -36,59 +30,58 @@ const loginCommand = async (args) => {
36
30
  });
37
31
  });
38
32
  };
39
- console.log(colors_1.default.whiteBold("Lichess Token Login"));
40
- console.log(colors_1.default.blue("Please enter your Lichess token \n" +
33
+ console.log(cl.whiteBold("Lichess Token Login"));
34
+ console.log(cl.blue("Please enter your Lichess token \n" +
41
35
  "You can generate one at https://lichess.org/account/oauth/token/create?scopes[]=study:write&scopes[]=study:read&scopes[]=web:mod&description=Broadcast+CLI"));
42
36
  console.log("");
43
- token = await question(colors_1.default.whiteBold("Lichess Token: "));
37
+ token = await question(cl.whiteBold("Lichess Token: "));
44
38
  if (!token?.trim()) {
45
- console.error(colors_1.default.red("Error: Token cannot be empty."));
39
+ console.error(cl.red("Error: Token cannot be empty."));
46
40
  readline.close();
47
- (0, node_process_1.exit)(1);
41
+ exit(1);
48
42
  }
49
43
  if (!token.startsWith("lip_")) {
50
- console.error(colors_1.default.red("Error: Invalid token format. Token must start with 'lip_'. Please check your token."));
44
+ console.error(cl.red("Error: Invalid token format. Token must start with 'lip_'. Please check your token."));
51
45
  readline.close();
52
- (0, node_process_1.exit)(1);
46
+ exit(1);
53
47
  }
54
48
  domain =
55
- (await question(colors_1.default.whiteBold("Lichess Domain (default: https://lichess.org): "))) || "https://lichess.org";
49
+ (await question(cl.whiteBold("Lichess Domain (default: https://lichess.org): "))) || "https://lichess.org";
56
50
  readline.close();
57
51
  }
58
52
  else {
59
53
  if (!token.startsWith("lip_")) {
60
- console.error(colors_1.default.red("Error: Invalid token format. Token must start with 'lip_'. Please check your token."));
61
- (0, node_process_1.exit)(1);
54
+ console.error(cl.red("Error: Invalid token format. Token must start with 'lip_'. Please check your token."));
55
+ exit(1);
62
56
  }
63
57
  }
64
58
  domain = domain.replace(/\/$/, "");
65
59
  const skipValidation = args.includes("--skip-validation");
66
60
  let scopes = [];
67
61
  if (!skipValidation) {
68
- console.log(colors_1.default.blue("Validating token and fetching scopes..."));
62
+ console.log(cl.blue("Validating token and fetching scopes..."));
69
63
  try {
70
- scopes = await (0, credentials_1.fetchTokenScopes)(token, domain);
71
- console.log(colors_1.default.green(`✓ Token valid with scopes: ${scopes.join(", ")}`));
64
+ scopes = await fetchTokenScopes(token, domain);
65
+ console.log(cl.green(`✓ Token valid with scopes: ${scopes.join(", ")}`));
72
66
  }
73
67
  catch (error) {
74
- console.error(colors_1.default.red(`Error: Failed to validate token: ${error instanceof Error ? error.message : String(error)}`));
75
- (0, node_process_1.exit)(1);
68
+ console.error(cl.red(`Error: Failed to validate token: ${error instanceof Error ? error.message : String(error)}`));
69
+ exit(1);
76
70
  }
77
71
  }
78
72
  else {
79
- console.log(colors_1.default.blue("Skipping token validation..."));
73
+ console.log(cl.blue("Skipping token validation..."));
80
74
  }
81
- (0, credentials_1.saveCredentials)({
75
+ saveCredentials({
82
76
  lichessToken: token,
83
77
  lichessDomain: domain,
84
78
  scopes: scopes,
85
79
  });
86
80
  console.log("");
87
- console.log(colors_1.default.green("✓ Credentials saved successfully! You can now use the CLI without setting environment variables."));
81
+ console.log(cl.green("✓ Credentials saved successfully! You can now use the CLI without setting environment variables."));
88
82
  }
89
83
  catch (error) {
90
- console.error(colors_1.default.red("Error during login:"), error);
91
- (0, node_process_1.exit)(1);
84
+ console.error(cl.red("Error during login:"), error);
85
+ exit(1);
92
86
  }
93
87
  };
94
- exports.loginCommand = loginCommand;
@@ -1,19 +1,13 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.periodCommand = void 0;
7
- const node_process_1 = require("node:process");
8
- const commandHandler_1 = require("../utils/commandHandler");
9
- const getInfoBroadcast_1 = require("../utils/getInfoBroadcast");
10
- const colors_1 = __importDefault(require("../utils/colors"));
1
+ import { exit } from "node:process";
2
+ import { client, msgCommonErrorHelp, sleep, handleApiResponse, translateRoundsToFix, checkTokenScopes, } from "../utils/commandHandler.js";
3
+ import { getBroadcast } from "../utils/getInfoBroadcast.js";
4
+ import cl from "../utils/colors.js";
11
5
  const setPeriodRounds = async (rounds, period, roundsToFix) => {
12
6
  let filteredRounds = rounds.filter((_, i) => !roundsToFix?.length || roundsToFix.includes(i + 1));
13
7
  if (filteredRounds.length === 0)
14
8
  filteredRounds = rounds;
15
9
  for (const round of filteredRounds) {
16
- await (0, commandHandler_1.handleApiResponse)(commandHandler_1.client.POST("/broadcast/round/{broadcastRoundId}/edit", {
10
+ await handleApiResponse(client.POST("/broadcast/round/{broadcastRoundId}/edit", {
17
11
  params: {
18
12
  path: { broadcastRoundId: round.id },
19
13
  query: { patch: 1 },
@@ -21,33 +15,32 @@ const setPeriodRounds = async (rounds, period, roundsToFix) => {
21
15
  body: {
22
16
  period: period,
23
17
  },
24
- }), `Successfully set period for round ${colors_1.default.whiteBold(round.id)} to ${colors_1.default.whiteBold(period.toString())} seconds.`, `Error setting period for round ${colors_1.default.whiteBold(round.id)}`);
25
- await (0, commandHandler_1.sleep)(200);
18
+ }), `Successfully set period for round ${cl.whiteBold(round.id)} to ${cl.whiteBold(period.toString())} seconds.`, `Error setting period for round ${cl.whiteBold(round.id)}`);
19
+ await sleep(200);
26
20
  }
27
21
  };
28
- const periodCommand = async (args) => {
29
- await (0, commandHandler_1.checkTokenScopes)(true);
22
+ export const periodCommand = async (args) => {
23
+ await checkTokenScopes(true);
30
24
  const [broadcastId, period] = args.slice(0, 2);
31
25
  if (!broadcastId || !period) {
32
- (0, commandHandler_1.msgCommonErrorHelp)("Broadcast ID and period are required.");
33
- (0, node_process_1.exit)(1);
26
+ msgCommonErrorHelp("Broadcast ID and period are required.");
27
+ exit(1);
34
28
  }
35
29
  const periodNum = parseInt(period, 10);
36
30
  if (isNaN(periodNum) || periodNum < 2 || periodNum > 60) {
37
- (0, commandHandler_1.msgCommonErrorHelp)("Period must be a number between 2 and 60 seconds.");
38
- (0, node_process_1.exit)(1);
31
+ msgCommonErrorHelp("Period must be a number between 2 and 60 seconds.");
32
+ exit(1);
39
33
  }
40
34
  const roundsArgIndex = args.findIndex((arg) => arg === "--rounds");
41
35
  let roundsToFix = undefined;
42
36
  if (roundsArgIndex !== -1 && roundsArgIndex + 1 < args.length) {
43
37
  const roundsArg = args[roundsArgIndex + 1];
44
- roundsToFix = roundsArg ? (0, commandHandler_1.translateRoundsToFix)(roundsArg) : undefined;
38
+ roundsToFix = roundsArg ? translateRoundsToFix(roundsArg) : undefined;
45
39
  }
46
- const broadcast = await (0, getInfoBroadcast_1.getBroadcast)(broadcastId);
40
+ const broadcast = await getBroadcast(broadcastId);
47
41
  if (!broadcast?.rounds || broadcast.rounds.length === 0) {
48
- console.error(colors_1.default.red("No rounds found for the specified broadcast."));
49
- (0, node_process_1.exit)(1);
42
+ console.error(cl.red("No rounds found for the specified broadcast."));
43
+ exit(1);
50
44
  }
51
45
  await setPeriodRounds(broadcast.rounds, periodNum, roundsToFix);
52
46
  };
53
- exports.periodCommand = periodCommand;
package/dist/cmd/push.js CHANGED
@@ -1,18 +1,12 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.pushCommand = void 0;
7
- const node_process_1 = require("node:process");
8
- const promises_1 = require("node:fs/promises");
9
- const node_path_1 = __importDefault(require("node:path"));
10
- const commandHandler_1 = require("../utils/commandHandler");
11
- const getInfoBroadcast_1 = require("../utils/getInfoBroadcast");
12
- const colors_1 = __importDefault(require("../utils/colors"));
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";
5
+ import { getBroadcastRound } from "../utils/getInfoBroadcast.js";
6
+ import cl from "../utils/colors.js";
13
7
  const pushPGN = async (round, pgn) => {
14
8
  try {
15
- const res = await commandHandler_1.client
9
+ const res = await client
16
10
  .POST("/api/broadcast/round/{broadcastRoundId}/push", {
17
11
  params: {
18
12
  path: { broadcastRoundId: round.id },
@@ -21,7 +15,7 @@ const pushPGN = async (round, pgn) => {
21
15
  bodySerializer: (body) => body,
22
16
  })
23
17
  .then((response) => response.data);
24
- console.log(colors_1.default.green(`✓ Successfully pushed PGN for round ${colors_1.default.whiteBold(round.id)}.`));
18
+ console.log(cl.green(`✓ Successfully pushed PGN for round ${cl.whiteBold(round.id)}.`));
25
19
  console.table(res?.games.map((game, i) => {
26
20
  return {
27
21
  "Game #": i + 1,
@@ -34,7 +28,7 @@ const pushPGN = async (round, pgn) => {
34
28
  }));
35
29
  }
36
30
  catch (error) {
37
- console.error(colors_1.default.red(`Error pushing PGN for round ${colors_1.default.whiteBold(round.id)}:`), error);
31
+ console.error(cl.red(`Error pushing PGN for round ${cl.whiteBold(round.id)}:`), error);
38
32
  }
39
33
  };
40
34
  const readPGNFromURL = async (pgnURL) => {
@@ -42,20 +36,20 @@ const readPGNFromURL = async (pgnURL) => {
42
36
  const response = await fetch(pgnURL, {
43
37
  method: "GET",
44
38
  headers: {
45
- "User-Agent": commandHandler_1.packageJson.name + "/" + commandHandler_1.packageJson.version,
39
+ "User-Agent": packageJson.name + "/" + packageJson.version,
46
40
  },
47
41
  });
48
42
  if (!response.ok) {
49
- console.error(colors_1.default.red(`Failed to fetch PGN from URL: ${response.statusText}`));
43
+ console.error(cl.red(`Failed to fetch PGN from URL: ${response.statusText}`));
50
44
  return undefined;
51
45
  }
52
46
  const pgnText = await response.text();
53
47
  return pgnText;
54
48
  }
55
49
  else {
56
- const resolvedPath = node_path_1.default.resolve(pgnURL);
57
- const stats = await (0, promises_1.readFile)(resolvedPath, { encoding: "utf-8" }).catch((err) => {
58
- console.error(colors_1.default.red(`Failed to read PGN file: ${err.message}`));
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}`));
59
53
  return undefined;
60
54
  });
61
55
  if (!stats)
@@ -68,20 +62,20 @@ const loop = async (roundInfo, pgnPath, loopTimer) => {
68
62
  const pgnContent = await readPGNFromURL(pgnPath);
69
63
  if (pgnContent)
70
64
  await pushPGN(roundInfo, pgnContent);
71
- await (0, commandHandler_1.sleep)(loopTimer * 1000);
65
+ await sleep(loopTimer * 1000);
72
66
  }
73
67
  };
74
- const pushCommand = async (args) => {
75
- await (0, commandHandler_1.checkTokenScopes)();
68
+ export const pushCommand = async (args) => {
69
+ await checkTokenScopes();
76
70
  const [roundId, pgnPath] = args.slice(0, 2);
77
71
  if (!roundId || !pgnPath) {
78
- (0, commandHandler_1.msgCommonErrorHelp)("Round ID and PGN are required.");
79
- (0, node_process_1.exit)(1);
72
+ msgCommonErrorHelp("Round ID and PGN are required.");
73
+ exit(1);
80
74
  }
81
- const roundInfo = await (0, getInfoBroadcast_1.getBroadcastRound)(roundId);
75
+ const roundInfo = await getBroadcastRound(roundId);
82
76
  if (!roundInfo) {
83
- console.error(colors_1.default.red("Round not found."));
84
- (0, node_process_1.exit)(1);
77
+ console.error(cl.red("Round not found."));
78
+ exit(1);
85
79
  }
86
80
  const loopArgIndex = args.findIndex((arg) => arg === "--loop");
87
81
  let loopTimer = undefined;
@@ -89,13 +83,13 @@ const pushCommand = async (args) => {
89
83
  const loopTimerStr = args[loopArgIndex + 1];
90
84
  loopTimer = parseInt(loopTimerStr, 10);
91
85
  if (isNaN(loopTimer) || loopTimer <= 0) {
92
- console.error(colors_1.default.red("Loop timer must be a positive integer."));
93
- (0, node_process_1.exit)(1);
86
+ console.error(cl.red("Loop timer must be a positive integer."));
87
+ exit(1);
94
88
  }
95
89
  }
96
90
  if (loopTimer) {
97
- console.log(colors_1.default.green(`Starting loop to push PGN every ${colors_1.default.whiteBold(loopTimer.toString())} seconds...`));
98
- console.log(colors_1.default.blue("Press Ctrl+C to stop."));
91
+ console.log(cl.green(`Starting loop to push PGN every ${cl.whiteBold(loopTimer.toString())} seconds...`));
92
+ console.log(cl.blue("Press Ctrl+C to stop."));
99
93
  await loop(roundInfo, pgnPath, loopTimer);
100
94
  }
101
95
  else {
@@ -104,4 +98,3 @@ const pushCommand = async (args) => {
104
98
  await pushPGN(roundInfo, pgnContent);
105
99
  }
106
100
  };
107
- exports.pushCommand = pushCommand;
@@ -1,19 +1,13 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.pushFilterIDCommand = void 0;
7
- const node_process_1 = require("node:process");
8
- const promises_1 = require("node:fs/promises");
9
- const node_path_1 = __importDefault(require("node:path"));
10
- const commandHandler_1 = require("../utils/commandHandler");
11
- const pgn_1 = require("chessops/pgn");
12
- const getInfoBroadcast_1 = require("../utils/getInfoBroadcast");
13
- const colors_1 = __importDefault(require("../utils/colors"));
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";
5
+ import { parsePgn, makePgn } from "chessops/pgn";
6
+ import { getBroadcastRound } from "../utils/getInfoBroadcast.js";
7
+ import cl from "../utils/colors.js";
14
8
  const pushPGN = async (round, pgn) => {
15
9
  try {
16
- const res = await commandHandler_1.client
10
+ const res = await client
17
11
  .POST("/api/broadcast/round/{broadcastRoundId}/push", {
18
12
  params: {
19
13
  path: { broadcastRoundId: round.id },
@@ -22,24 +16,24 @@ const pushPGN = async (round, pgn) => {
22
16
  bodySerializer: (body) => body,
23
17
  })
24
18
  .then((response) => response.data);
25
- console.log(colors_1.default.green(`✓ Successfully pushed PGN for round ${colors_1.default.whiteBold(round.id)}.`));
19
+ console.log(cl.green(`✓ Successfully pushed PGN for round ${cl.whiteBold(round.id)}.`));
26
20
  console.table(res?.games.map((game, i) => {
27
21
  return {
28
22
  "Game #": i + 1,
29
23
  "White Player": game.tags["White"] || "Unknown",
30
24
  "Black Player": game.tags["Black"] || "Unknown",
31
25
  Result: game.tags["Result"] || "Unknown",
32
- "Ply Count": game.moves || "Unknown",
26
+ "Ply Count": game.moves ?? "Unknown",
33
27
  Error: game.error || "None",
34
28
  };
35
29
  }));
36
30
  }
37
31
  catch (error) {
38
- console.error(colors_1.default.red(`Error pushing PGN for round ${colors_1.default.whiteBold(round.id)}:`), error);
32
+ console.error(cl.red(`Error pushing PGN for round ${cl.whiteBold(round.id)}:`), error);
39
33
  }
40
34
  };
41
35
  const filterPgnByIds = (pgn, filterIds) => {
42
- const parsed = (0, pgn_1.parsePgn)(pgn);
36
+ const parsed = parsePgn(pgn);
43
37
  const filteredGames = parsed.filter((game) => {
44
38
  const whiteFideId = game.headers.get("WhiteFideId");
45
39
  const blackFideId = game.headers.get("BlackFideId");
@@ -60,32 +54,32 @@ const filterPgnByIds = (pgn, filterIds) => {
60
54
  filterIds.includes(parseInt(blackFideId || "", 10)));
61
55
  }
62
56
  });
63
- return filteredGames.map((game) => (0, pgn_1.makePgn)(game)).join("\n\n");
57
+ return filteredGames.map((game) => makePgn(game)).join("\n\n");
64
58
  };
65
59
  const readPGNFromURL = async (pgnURL, filterIds) => {
66
60
  if (pgnURL.startsWith("http://") || pgnURL.startsWith("https://")) {
67
61
  const response = await fetch(pgnURL, {
68
62
  method: "GET",
69
63
  headers: {
70
- "User-Agent": commandHandler_1.packageJson.name + "/" + commandHandler_1.packageJson.version,
64
+ "User-Agent": packageJson.name + "/" + packageJson.version,
71
65
  },
72
66
  });
73
67
  if (!response.ok) {
74
- console.error(colors_1.default.red(`Failed to fetch PGN from URL: ${response.statusText}`));
68
+ console.error(cl.red(`Failed to fetch PGN from URL: ${response.statusText}`));
75
69
  return undefined;
76
70
  }
77
71
  const pgnText = await response.text();
78
72
  const filteredPgn = filterPgnByIds(pgnText, filterIds);
79
73
  if (!filteredPgn) {
80
- console.error(colors_1.default.red(`No games found matching the provided filter IDs.`));
74
+ console.error(cl.red(`No games found matching the provided filter IDs.`));
81
75
  return undefined;
82
76
  }
83
77
  return filteredPgn;
84
78
  }
85
79
  else {
86
- const resolvedPath = node_path_1.default.resolve(pgnURL);
87
- const stats = await (0, promises_1.readFile)(resolvedPath, { encoding: "utf-8" }).catch((err) => {
88
- console.error(colors_1.default.red(`Failed to read PGN file: ${err.message}`));
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}`));
89
83
  return undefined;
90
84
  });
91
85
  if (!stats)
@@ -93,29 +87,32 @@ const readPGNFromURL = async (pgnURL, filterIds) => {
93
87
  return stats.toString();
94
88
  }
95
89
  };
90
+ let lastPGN = "";
96
91
  const loop = async (roundInfo, pgnPath, loopTimer, filterIds) => {
97
92
  while (true) {
98
93
  const pgnContent = await readPGNFromURL(pgnPath, filterIds);
99
- if (pgnContent)
94
+ if (pgnContent && pgnContent !== lastPGN) {
100
95
  await pushPGN(roundInfo, pgnContent);
101
- await (0, commandHandler_1.sleep)(loopTimer * 1000);
96
+ lastPGN = pgnContent;
97
+ }
98
+ await sleep(loopTimer * 1000);
102
99
  }
103
100
  };
104
- const pushFilterIDCommand = async (args) => {
105
- await (0, commandHandler_1.checkTokenScopes)();
101
+ export const pushFilterIDCommand = async (args) => {
102
+ await checkTokenScopes();
106
103
  const [roundId, pgnPath] = args.slice(0, 2);
107
104
  const filterIds = args
108
105
  .slice(2)
109
106
  .filter((arg) => !arg.startsWith("--") && Number.isInteger(parseInt(arg)))
110
107
  .map((arg) => parseInt(arg, 10));
111
108
  if (!roundId || !pgnPath || filterIds.length === 0) {
112
- (0, commandHandler_1.msgCommonErrorHelp)("Round ID, PGN, and at least one filter ID are required.");
113
- (0, node_process_1.exit)(1);
109
+ msgCommonErrorHelp("Round ID, PGN, and at least one filter ID are required.");
110
+ exit(1);
114
111
  }
115
- const roundInfo = await (0, getInfoBroadcast_1.getBroadcastRound)(roundId);
112
+ const roundInfo = await getBroadcastRound(roundId);
116
113
  if (!roundInfo) {
117
- console.error(colors_1.default.red("Round not found."));
118
- (0, node_process_1.exit)(1);
114
+ console.error(cl.red("Round not found."));
115
+ exit(1);
119
116
  }
120
117
  const loopArgIndex = args.findIndex((arg) => arg === "--loop");
121
118
  let loopTimer = undefined;
@@ -123,13 +120,13 @@ const pushFilterIDCommand = async (args) => {
123
120
  const loopTimerStr = args[loopArgIndex + 1];
124
121
  loopTimer = parseInt(loopTimerStr, 10);
125
122
  if (isNaN(loopTimer) || loopTimer <= 0) {
126
- console.error(colors_1.default.red("Loop timer must be a positive integer."));
127
- (0, node_process_1.exit)(1);
123
+ console.error(cl.red("Loop timer must be a positive integer."));
124
+ exit(1);
128
125
  }
129
126
  }
130
127
  if (loopTimer) {
131
- console.log(colors_1.default.green(`Starting loop to push PGN every ${colors_1.default.whiteBold(loopTimer.toString())} seconds...`));
132
- console.log(colors_1.default.blue("Press Ctrl+C to stop."));
128
+ console.log(cl.green(`Starting loop to push PGN every ${cl.whiteBold(loopTimer.toString())} seconds...`));
129
+ console.log(cl.blue("Press Ctrl+C to stop."));
133
130
  await loop(roundInfo, pgnPath, loopTimer, filterIds);
134
131
  }
135
132
  else {
@@ -138,4 +135,3 @@ const pushFilterIDCommand = async (args) => {
138
135
  await pushPGN(roundInfo, pgnContent);
139
136
  }
140
137
  };
141
- exports.pushFilterIDCommand = pushFilterIDCommand;