@trainheroic-unofficial/cli 3.5.0 → 3.5.1

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.
Files changed (2) hide show
  1. package/dist/cli.mjs +85 -7
  2. package/package.json +3 -3
package/dist/cli.mjs CHANGED
@@ -718,6 +718,12 @@ const exerciseSpecSchema = z.object({
718
718
  instr: z.string().optional(),
719
719
  param_1_type: z.number().optional(),
720
720
  param_2_type: z.number().optional()
721
+ }).superRefine((exercise, ctx) => {
722
+ if (Array.isArray(exercise.reps) && Array.isArray(exercise.weight) && exercise.reps.length !== exercise.weight.length) ctx.addIssue({
723
+ code: "custom",
724
+ message: `Per-set reps and weight arrays must have the same length; received ${exercise.reps.length} reps and ${exercise.weight.length} weights.`,
725
+ path: ["weight"]
726
+ });
721
727
  });
722
728
  /** A block's Red-Zone leaderboard: a unit string/number, or an object with options. */
723
729
  const leaderboardSpecSchema = z.union([
@@ -2822,9 +2828,75 @@ function repsList(ex) {
2822
2828
  const reps = ex.reps;
2823
2829
  if (Array.isArray(reps)) return reps.map((r) => String(r));
2824
2830
  if (reps === void 0 || reps === null) return [];
2825
- const sets = Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
2831
+ const sets = Array.isArray(ex.weight) ? ex.weight.length : Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
2826
2832
  return Array.from({ length: sets }, () => String(reps));
2827
2833
  }
2834
+ /** Number of sets that the encoder will write for an exercise. */
2835
+ function exerciseSetCount(ex) {
2836
+ if (Array.isArray(ex.reps) && ex.reps.length > 0) return ex.reps.length;
2837
+ if (Array.isArray(ex.weight)) return ex.weight.length;
2838
+ if (!Array.isArray(ex.reps) && ex.reps !== void 0 && ex.reps !== null || ex.weight !== void 0 && ex.weight !== null) return Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
2839
+ return 0;
2840
+ }
2841
+ function findSetSplits(blocks) {
2842
+ return blocks.flatMap((block) => block.exercises.flatMap((exercise) => {
2843
+ const setCount = exerciseSetCount(exercise);
2844
+ if (setCount <= 10) return [];
2845
+ return [{
2846
+ blockTitle: block.title,
2847
+ exerciseTitle: exercise.title ?? String(exercise.id),
2848
+ setCount,
2849
+ blockCount: Math.ceil(setCount / 10)
2850
+ }];
2851
+ }));
2852
+ }
2853
+ function setSplitSummary(blocks) {
2854
+ const splits = findSetSplits(blocks);
2855
+ if (splits.length === 0) return null;
2856
+ return `TrainHeroic supports at most 10 sets per exercise block. The following will be split into consecutive same-titled blocks: ${splits.map(({ blockTitle, exerciseTitle, setCount, blockCount }) => `${blockTitle} / ${exerciseTitle}: ${setCount} sets into ${blockCount} blocks`).join("; ")}.`;
2857
+ }
2858
+ function sliceExercise(exercise, setCount, start) {
2859
+ if (start > 0 && setCount <= start) return null;
2860
+ const chunkCount = setCount === 0 ? 0 : Math.min(10, Math.max(0, setCount - start));
2861
+ const chunk = { ...exercise };
2862
+ const hasPerSetReps = Array.isArray(exercise.reps) && exercise.reps.length > 0;
2863
+ const hasScalarReps = !Array.isArray(exercise.reps) && exercise.reps !== void 0 && exercise.reps !== null;
2864
+ if (Array.isArray(exercise.reps) && exercise.reps.length > 0) {
2865
+ chunk.reps = exercise.reps.slice(start, start + 10);
2866
+ delete chunk.sets;
2867
+ } else if (hasScalarReps) chunk.sets = chunkCount;
2868
+ if (Array.isArray(exercise.weight)) {
2869
+ chunk.weight = exercise.weight.slice(start, start + 10);
2870
+ if (!hasPerSetReps && !hasScalarReps) delete chunk.sets;
2871
+ } else if (!hasPerSetReps && exercise.weight !== void 0 && exercise.weight !== null) chunk.sets = chunkCount;
2872
+ return chunk;
2873
+ }
2874
+ /**
2875
+ * Split blocks so no encoded exercise exceeds TrainHeroic's ten parameter slots.
2876
+ * Exercises in a superset stay aligned by set range; shorter exercises appear only in
2877
+ * chunks where they still have sets.
2878
+ */
2879
+ function splitOversizedBlocks(blocks) {
2880
+ return blocks.flatMap((block) => {
2881
+ const setCounts = block.exercises.map(exerciseSetCount);
2882
+ const largestSetCount = Math.max(0, ...setCounts);
2883
+ const chunkCount = Math.max(1, Math.ceil(largestSetCount / 10));
2884
+ if (chunkCount === 1) return [block];
2885
+ return Array.from({ length: chunkCount }, (_, index) => {
2886
+ const start = index * 10;
2887
+ const exercises = block.exercises.map((exercise, exerciseIndex) => sliceExercise(exercise, setCounts[exerciseIndex] ?? 0, start)).filter((exercise) => exercise !== null);
2888
+ if (index === 0) return {
2889
+ ...block,
2890
+ exercises
2891
+ };
2892
+ return {
2893
+ title: block.title,
2894
+ ...block.type === void 0 ? {} : { type: block.type },
2895
+ exercises
2896
+ };
2897
+ });
2898
+ });
2899
+ }
2828
2900
  /** Build one saveWorkoutSetExercises payload entry with all ten param slots filled. */
2829
2901
  function makeExercise(ex, workoutSetId, order, key) {
2830
2902
  const reps = repsList(ex);
@@ -2832,8 +2904,7 @@ function makeExercise(ex, workoutSetId, order, key) {
2832
2904
  if (instruction === "" && ex.rpe !== void 0 && ex.rpe !== null) instruction = `RPE ${ex.rpe}`;
2833
2905
  const hasWeight = ex.weight !== void 0 && ex.weight !== null;
2834
2906
  const weightArr = Array.isArray(ex.weight) ? ex.weight : null;
2835
- let count = reps.length;
2836
- if (count === 0 && hasWeight) count = weightArr ? weightArr.length : Math.max(1, Math.trunc(Number(ex.sets ?? 1)) || 1);
2907
+ const count = exerciseSetCount(ex);
2837
2908
  let param2Type;
2838
2909
  let param2Values;
2839
2910
  if (hasWeight) {
@@ -2864,8 +2935,8 @@ function makeExercise(ex, workoutSetId, order, key) {
2864
2935
  eType: "e",
2865
2936
  use_count: 0
2866
2937
  };
2867
- const p1 = slots(reps);
2868
- const p2 = slots(param2Values);
2938
+ const p1 = slots(reps, 10);
2939
+ const p2 = slots(param2Values, 10);
2869
2940
  for (let i = 0; i < 10; i += 1) {
2870
2941
  entry[`param_1_data_${i + 1}`] = p1[i] ?? "";
2871
2942
  entry[`param_2_data_${i + 1}`] = p2[i] ?? "";
@@ -2983,7 +3054,10 @@ function createPath(opts) {
2983
3054
  return `/2.0/coach/calendar/workout/createWorkoutForDay/${opts.programId}/${y}/${m}/${d}/0`;
2984
3055
  }
2985
3056
  async function buildSession(client, opts) {
2986
- const blocks = z.array(blockSpecSchema).parse(opts.blocks);
3057
+ const validatedBlocks = z.array(blockSpecSchema).parse(opts.blocks);
3058
+ const splitSummary = setSplitSummary(validatedBlocks);
3059
+ if (splitSummary !== null && opts.confirmSetSplit !== true) throw new Error(`${splitSummary} Set confirmSetSplit:true to allow this change.`);
3060
+ const blocks = splitSummary === null ? validatedBlocks : splitOversizedBlocks(validatedBlocks);
2987
3061
  const sess = await req(client, "POST", createPath(opts), {});
2988
3062
  checkResponse(sessionCreateResponseSchema, sess, "session create");
2989
3063
  const workoutId = Number(sess.workout_id);
@@ -4178,7 +4252,8 @@ Coach — manage a roster (needs a coach account):
4178
4252
  coach exercise stats
4179
4253
 
4180
4254
  workouts (spec: {"blocks":[{"title","exercises":[{"id","sets"?,"reps"?,"weight"?,"rpe"?}]}],"instruction"?};
4181
- reps/weight may be a scalar or per-set array; omit --publish to leave a draft):
4255
+ reps/weight may be a scalar or per-set array; >10 sets split into consecutive blocks
4256
+ and require --yes; omit --publish to leave a draft):
4182
4257
  coach workout build (--program <id> | --athlete <id>) (--date Y-M-D | --timeline-day <n>) [--publish --yes] <spec.json>|--file f
4183
4258
  coach workout read --program <id> --date Y-M-D --pw <id>
4184
4259
  coach workout publish --pw <id> --yes
@@ -4373,12 +4448,15 @@ async function cmdWorkoutBuild(client, a) {
4373
4448
  const parsed = await jsonInput(positionals[0], values.file);
4374
4449
  const spec = validate(workoutSpecSchema, Array.isArray(parsed) ? { blocks: parsed } : parsed, "workout spec", "run 'trainheroic skill' for the workout spec format and copy-paste examples.");
4375
4450
  const publish = values.publish === true;
4451
+ const splitSummary = setSplitSummary(spec.blocks);
4452
+ if (splitSummary !== null && values.yes !== true) fail(`${splitSummary} Add --yes to confirm.`);
4376
4453
  if (publish && values.yes !== true) fail("publishing is athlete-facing; add --yes to build and publish.");
4377
4454
  const opts = {
4378
4455
  programId,
4379
4456
  blocks: spec.blocks,
4380
4457
  publish
4381
4458
  };
4459
+ if (splitSummary !== null) opts.confirmSetSplit = true;
4382
4460
  if (values.date !== void 0) opts.date = parseWorkoutDate(values.date);
4383
4461
  if (values["timeline-day"] !== void 0) opts.timelineDay = toInt(values["timeline-day"], "--timeline-day");
4384
4462
  if (spec.instruction !== void 0) opts.instruction = spec.instruction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trainheroic-unofficial/cli",
3
- "version": "3.5.0",
3
+ "version": "3.5.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,8 +21,8 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "zod": "^4.4.3",
24
- "@trainheroic-unofficial/dto": "3.5.0",
25
- "@trainheroic-unofficial/js": "3.5.0"
24
+ "@trainheroic-unofficial/dto": "3.5.1",
25
+ "@trainheroic-unofficial/js": "3.5.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/node": "^26.3.0",