@thebriangao/totem 1.4.4
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/.dockerignore +23 -0
- package/.env.example +66 -0
- package/CHANGELOG.md +283 -0
- package/CONTRIBUTING.md +149 -0
- package/Dockerfile +53 -0
- package/LICENSE +37 -0
- package/README.md +587 -0
- package/SECURITY.md +79 -0
- package/TOOLS.md +429 -0
- package/WHOOP.md +2673 -0
- package/assets/banner.svg +38 -0
- package/dist/cli/index.js +589 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/setup.js +1127 -0
- package/dist/cli/setup.js.map +1 -0
- package/dist/cli/ui.js +512 -0
- package/dist/cli/ui.js.map +1 -0
- package/dist/cli/update.js +465 -0
- package/dist/cli/update.js.map +1 -0
- package/dist/data/behaviors.js +2777 -0
- package/dist/data/behaviors.js.map +1 -0
- package/dist/data/endpoints.js +320 -0
- package/dist/data/endpoints.js.map +1 -0
- package/dist/data/exercises.js +4863 -0
- package/dist/data/exercises.js.map +1 -0
- package/dist/data/sports.js +212 -0
- package/dist/data/sports.js.map +1 -0
- package/dist/lib/concurrency.js +27 -0
- package/dist/lib/concurrency.js.map +1 -0
- package/dist/lib/dates.js +29 -0
- package/dist/lib/dates.js.map +1 -0
- package/dist/lib/format.js +22 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/stats.js +32 -0
- package/dist/lib/stats.js.map +1 -0
- package/dist/lib/timezone.js +249 -0
- package/dist/lib/timezone.js.map +1 -0
- package/dist/lib/walk.js +396 -0
- package/dist/lib/walk.js.map +1 -0
- package/dist/projections/behavior_impact.js +96 -0
- package/dist/projections/behavior_impact.js.map +1 -0
- package/dist/projections/calendar.js +33 -0
- package/dist/projections/calendar.js.map +1 -0
- package/dist/projections/communities.js +105 -0
- package/dist/projections/communities.js.map +1 -0
- package/dist/projections/cycle.js +81 -0
- package/dist/projections/cycle.js.map +1 -0
- package/dist/projections/hr_zones.js +35 -0
- package/dist/projections/hr_zones.js.map +1 -0
- package/dist/projections/journal.js +56 -0
- package/dist/projections/journal.js.map +1 -0
- package/dist/projections/leaderboard.js +37 -0
- package/dist/projections/leaderboard.js.map +1 -0
- package/dist/projections/lift_exercise.js +22 -0
- package/dist/projections/lift_exercise.js.map +1 -0
- package/dist/projections/lift_history.js +133 -0
- package/dist/projections/lift_history.js.map +1 -0
- package/dist/projections/lift_library.js +71 -0
- package/dist/projections/lift_library.js.map +1 -0
- package/dist/projections/lift_progression.js +138 -0
- package/dist/projections/lift_progression.js.map +1 -0
- package/dist/projections/lift_prs.js +20 -0
- package/dist/projections/lift_prs.js.map +1 -0
- package/dist/projections/live_hr.js +19 -0
- package/dist/projections/live_hr.js.map +1 -0
- package/dist/projections/live_state.js +23 -0
- package/dist/projections/live_state.js.map +1 -0
- package/dist/projections/live_stress.js +11 -0
- package/dist/projections/live_stress.js.map +1 -0
- package/dist/projections/performance_assessment.js +15 -0
- package/dist/projections/performance_assessment.js.map +1 -0
- package/dist/projections/profile.js +59 -0
- package/dist/projections/profile.js.map +1 -0
- package/dist/projections/recovery.js +131 -0
- package/dist/projections/recovery.js.map +1 -0
- package/dist/projections/sleep.js +256 -0
- package/dist/projections/sleep.js.map +1 -0
- package/dist/projections/sleep_need.js +32 -0
- package/dist/projections/sleep_need.js.map +1 -0
- package/dist/projections/smart_alarm.js +45 -0
- package/dist/projections/smart_alarm.js.map +1 -0
- package/dist/projections/strain.js +128 -0
- package/dist/projections/strain.js.map +1 -0
- package/dist/projections/stress.js +86 -0
- package/dist/projections/stress.js.map +1 -0
- package/dist/projections/today.js +129 -0
- package/dist/projections/today.js.map +1 -0
- package/dist/projections/trend.js +149 -0
- package/dist/projections/trend.js.map +1 -0
- package/dist/projections/workout.js +161 -0
- package/dist/projections/workout.js.map +1 -0
- package/dist/projections/workouts.js +37 -0
- package/dist/projections/workouts.js.map +1 -0
- package/dist/schemas/calendar.js +12 -0
- package/dist/schemas/calendar.js.map +1 -0
- package/dist/schemas/coach.js +11 -0
- package/dist/schemas/coach.js.map +1 -0
- package/dist/schemas/communities.js +51 -0
- package/dist/schemas/communities.js.map +1 -0
- package/dist/schemas/compare.js +15 -0
- package/dist/schemas/compare.js.map +1 -0
- package/dist/schemas/escape.js +18 -0
- package/dist/schemas/escape.js.map +1 -0
- package/dist/schemas/journal.js +69 -0
- package/dist/schemas/journal.js.map +1 -0
- package/dist/schemas/leaderboard.js +25 -0
- package/dist/schemas/leaderboard.js.map +1 -0
- package/dist/schemas/live.js +26 -0
- package/dist/schemas/live.js.map +1 -0
- package/dist/schemas/performance.js +12 -0
- package/dist/schemas/performance.js.map +1 -0
- package/dist/schemas/primitives.js +29 -0
- package/dist/schemas/primitives.js.map +1 -0
- package/dist/schemas/profile.js +39 -0
- package/dist/schemas/profile.js.map +1 -0
- package/dist/schemas/recovery.js +27 -0
- package/dist/schemas/recovery.js.map +1 -0
- package/dist/schemas/settings.js +26 -0
- package/dist/schemas/settings.js.map +1 -0
- package/dist/schemas/sleep.js +38 -0
- package/dist/schemas/sleep.js.map +1 -0
- package/dist/schemas/sleep_need.js +15 -0
- package/dist/schemas/sleep_need.js.map +1 -0
- package/dist/schemas/smart_alarm.js +36 -0
- package/dist/schemas/smart_alarm.js.map +1 -0
- package/dist/schemas/strain.js +24 -0
- package/dist/schemas/strain.js.map +1 -0
- package/dist/schemas/strength.js +170 -0
- package/dist/schemas/strength.js.map +1 -0
- package/dist/schemas/stress.js +16 -0
- package/dist/schemas/stress.js.map +1 -0
- package/dist/schemas/today.js +38 -0
- package/dist/schemas/today.js.map +1 -0
- package/dist/schemas/trend.js +50 -0
- package/dist/schemas/trend.js.map +1 -0
- package/dist/schemas/womens_health.js +23 -0
- package/dist/schemas/womens_health.js.map +1 -0
- package/dist/schemas/workouts.js +50 -0
- package/dist/schemas/workouts.js.map +1 -0
- package/dist/scripts/cognito_bootstrap.js +278 -0
- package/dist/scripts/cognito_bootstrap.js.map +1 -0
- package/dist/server-http.js +221 -0
- package/dist/server-http.js.map +1 -0
- package/dist/server.js +74 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/register.js +117 -0
- package/dist/tools/register.js.map +1 -0
- package/dist/tools/v2/activity_create.js +64 -0
- package/dist/tools/v2/activity_create.js.map +1 -0
- package/dist/tools/v2/activity_delete.js +26 -0
- package/dist/tools/v2/activity_delete.js.map +1 -0
- package/dist/tools/v2/behavior_impact.js +36 -0
- package/dist/tools/v2/behavior_impact.js.map +1 -0
- package/dist/tools/v2/calendar.js +28 -0
- package/dist/tools/v2/calendar.js.map +1 -0
- package/dist/tools/v2/coach_ask.js +117 -0
- package/dist/tools/v2/coach_ask.js.map +1 -0
- package/dist/tools/v2/communities.js +60 -0
- package/dist/tools/v2/communities.js.map +1 -0
- package/dist/tools/v2/compare.js +67 -0
- package/dist/tools/v2/compare.js.map +1 -0
- package/dist/tools/v2/cycle.js +23 -0
- package/dist/tools/v2/cycle.js.map +1 -0
- package/dist/tools/v2/cycle_log.js +43 -0
- package/dist/tools/v2/cycle_log.js.map +1 -0
- package/dist/tools/v2/day.js +32 -0
- package/dist/tools/v2/day.js.map +1 -0
- package/dist/tools/v2/endpoints.js +28 -0
- package/dist/tools/v2/endpoints.js.map +1 -0
- package/dist/tools/v2/hidden_metric.js +28 -0
- package/dist/tools/v2/hidden_metric.js.map +1 -0
- package/dist/tools/v2/hr_zones.js +24 -0
- package/dist/tools/v2/hr_zones.js.map +1 -0
- package/dist/tools/v2/hr_zones_set.js +51 -0
- package/dist/tools/v2/hr_zones_set.js.map +1 -0
- package/dist/tools/v2/journal.js +26 -0
- package/dist/tools/v2/journal.js.map +1 -0
- package/dist/tools/v2/journal_autopop.js +19 -0
- package/dist/tools/v2/journal_autopop.js.map +1 -0
- package/dist/tools/v2/journal_catalog.js +44 -0
- package/dist/tools/v2/journal_catalog.js.map +1 -0
- package/dist/tools/v2/journal_log.js +78 -0
- package/dist/tools/v2/journal_log.js.map +1 -0
- package/dist/tools/v2/leaderboard.js +79 -0
- package/dist/tools/v2/leaderboard.js.map +1 -0
- package/dist/tools/v2/lift_catalog.js +47 -0
- package/dist/tools/v2/lift_catalog.js.map +1 -0
- package/dist/tools/v2/lift_custom_exercise.js +108 -0
- package/dist/tools/v2/lift_custom_exercise.js.map +1 -0
- package/dist/tools/v2/lift_exercise.js +31 -0
- package/dist/tools/v2/lift_exercise.js.map +1 -0
- package/dist/tools/v2/lift_history.js +39 -0
- package/dist/tools/v2/lift_history.js.map +1 -0
- package/dist/tools/v2/lift_library.js +22 -0
- package/dist/tools/v2/lift_library.js.map +1 -0
- package/dist/tools/v2/lift_log.js +95 -0
- package/dist/tools/v2/lift_log.js.map +1 -0
- package/dist/tools/v2/lift_progression.js +30 -0
- package/dist/tools/v2/lift_progression.js.map +1 -0
- package/dist/tools/v2/lift_prs.js +21 -0
- package/dist/tools/v2/lift_prs.js.map +1 -0
- package/dist/tools/v2/lift_template_save.js +83 -0
- package/dist/tools/v2/lift_template_save.js.map +1 -0
- package/dist/tools/v2/live_hr.js +21 -0
- package/dist/tools/v2/live_hr.js.map +1 -0
- package/dist/tools/v2/live_state.js +21 -0
- package/dist/tools/v2/live_state.js.map +1 -0
- package/dist/tools/v2/live_stress.js +22 -0
- package/dist/tools/v2/live_stress.js.map +1 -0
- package/dist/tools/v2/performance_assessment.js +38 -0
- package/dist/tools/v2/performance_assessment.js.map +1 -0
- package/dist/tools/v2/profile.js +28 -0
- package/dist/tools/v2/profile.js.map +1 -0
- package/dist/tools/v2/profile_update.js +66 -0
- package/dist/tools/v2/profile_update.js.map +1 -0
- package/dist/tools/v2/raw.js +43 -0
- package/dist/tools/v2/raw.js.map +1 -0
- package/dist/tools/v2/recovery.js +31 -0
- package/dist/tools/v2/recovery.js.map +1 -0
- package/dist/tools/v2/sleep.js +23 -0
- package/dist/tools/v2/sleep.js.map +1 -0
- package/dist/tools/v2/sleep_need.js +21 -0
- package/dist/tools/v2/sleep_need.js.map +1 -0
- package/dist/tools/v2/smart_alarm.js +24 -0
- package/dist/tools/v2/smart_alarm.js.map +1 -0
- package/dist/tools/v2/smart_alarm_set.js +94 -0
- package/dist/tools/v2/smart_alarm_set.js.map +1 -0
- package/dist/tools/v2/sports_catalog.js +26 -0
- package/dist/tools/v2/sports_catalog.js.map +1 -0
- package/dist/tools/v2/strain.js +23 -0
- package/dist/tools/v2/strain.js.map +1 -0
- package/dist/tools/v2/stress.js +23 -0
- package/dist/tools/v2/stress.js.map +1 -0
- package/dist/tools/v2/symptom_log.js +72 -0
- package/dist/tools/v2/symptom_log.js.map +1 -0
- package/dist/tools/v2/today.js +30 -0
- package/dist/tools/v2/today.js.map +1 -0
- package/dist/tools/v2/trend.js +26 -0
- package/dist/tools/v2/trend.js.map +1 -0
- package/dist/tools/v2/workout.js +23 -0
- package/dist/tools/v2/workout.js.map +1 -0
- package/dist/tools/v2/workouts.js +32 -0
- package/dist/tools/v2/workouts.js.map +1 -0
- package/dist/whoop/build_lift_body.js +67 -0
- package/dist/whoop/build_lift_body.js.map +1 -0
- package/dist/whoop/client.js +84 -0
- package/dist/whoop/client.js.map +1 -0
- package/dist/whoop/cognito.js +110 -0
- package/dist/whoop/cognito.js.map +1 -0
- package/dist/whoop/constants.js +4 -0
- package/dist/whoop/constants.js.map +1 -0
- package/dist/whoop/device.js +103 -0
- package/dist/whoop/device.js.map +1 -0
- package/dist/whoop/errors.js +42 -0
- package/dist/whoop/errors.js.map +1 -0
- package/dist/whoop/init_timezone.js +62 -0
- package/dist/whoop/init_timezone.js.map +1 -0
- package/dist/whoop/installation.js +62 -0
- package/dist/whoop/installation.js.map +1 -0
- package/dist/whoop/json_out.js +9 -0
- package/dist/whoop/json_out.js.map +1 -0
- package/dist/whoop/oauth_provider.js +297 -0
- package/dist/whoop/oauth_provider.js.map +1 -0
- package/dist/whoop/session_state.js +34 -0
- package/dist/whoop/session_state.js.map +1 -0
- package/dist/whoop/token_manager.js +60 -0
- package/dist/whoop/token_manager.js.map +1 -0
- package/dist/whoop/token_store.js +47 -0
- package/dist/whoop/token_store.js.map +1 -0
- package/dist/whoop/types.js +82 -0
- package/dist/whoop/types.js.map +1 -0
- package/dist/whoop/write_safety.js +23 -0
- package/dist/whoop/write_safety.js.map +1 -0
- package/package.json +66 -0
- package/src/cli/index.ts +597 -0
- package/src/cli/setup.ts +1106 -0
- package/src/cli/ui.ts +505 -0
- package/src/cli/update.ts +442 -0
- package/src/data/behaviors.ts +2784 -0
- package/src/data/endpoints.ts +319 -0
- package/src/data/exercises.ts +4868 -0
- package/src/data/sports.ts +218 -0
- package/src/lib/concurrency.ts +28 -0
- package/src/lib/dates.ts +35 -0
- package/src/lib/format.ts +27 -0
- package/src/lib/stats.ts +35 -0
- package/src/lib/timezone.ts +269 -0
- package/src/lib/walk.ts +418 -0
- package/src/projections/behavior_impact.ts +98 -0
- package/src/projections/calendar.ts +46 -0
- package/src/projections/communities.ts +135 -0
- package/src/projections/cycle.ts +85 -0
- package/src/projections/hr_zones.ts +43 -0
- package/src/projections/journal.ts +55 -0
- package/src/projections/leaderboard.ts +49 -0
- package/src/projections/lift_exercise.ts +29 -0
- package/src/projections/lift_history.ts +140 -0
- package/src/projections/lift_library.ts +70 -0
- package/src/projections/lift_progression.ts +138 -0
- package/src/projections/lift_prs.ts +23 -0
- package/src/projections/live_hr.ts +21 -0
- package/src/projections/live_state.ts +28 -0
- package/src/projections/live_stress.ts +13 -0
- package/src/projections/performance_assessment.ts +19 -0
- package/src/projections/profile.ts +71 -0
- package/src/projections/recovery.ts +133 -0
- package/src/projections/sleep.ts +276 -0
- package/src/projections/sleep_need.ts +36 -0
- package/src/projections/smart_alarm.ts +70 -0
- package/src/projections/strain.ts +131 -0
- package/src/projections/stress.ts +95 -0
- package/src/projections/today.ts +166 -0
- package/src/projections/trend.ts +149 -0
- package/src/projections/workout.ts +168 -0
- package/src/projections/workouts.ts +36 -0
- package/src/schemas/calendar.ts +13 -0
- package/src/schemas/coach.ts +11 -0
- package/src/schemas/communities.ts +56 -0
- package/src/schemas/compare.ts +17 -0
- package/src/schemas/escape.ts +21 -0
- package/src/schemas/journal.ts +80 -0
- package/src/schemas/leaderboard.ts +26 -0
- package/src/schemas/live.ts +31 -0
- package/src/schemas/performance.ts +13 -0
- package/src/schemas/primitives.ts +35 -0
- package/src/schemas/profile.ts +40 -0
- package/src/schemas/recovery.ts +28 -0
- package/src/schemas/settings.ts +30 -0
- package/src/schemas/sleep.ts +40 -0
- package/src/schemas/sleep_need.ts +16 -0
- package/src/schemas/smart_alarm.ts +40 -0
- package/src/schemas/strain.ts +25 -0
- package/src/schemas/strength.ts +189 -0
- package/src/schemas/stress.ts +17 -0
- package/src/schemas/today.ts +39 -0
- package/src/schemas/trend.ts +53 -0
- package/src/schemas/womens_health.ts +26 -0
- package/src/schemas/workouts.ts +55 -0
- package/src/scripts/cognito_bootstrap.ts +249 -0
- package/src/server-http.ts +241 -0
- package/src/server.ts +85 -0
- package/src/tools/register.ts +120 -0
- package/src/tools/v2/activity_create.ts +74 -0
- package/src/tools/v2/activity_delete.ts +33 -0
- package/src/tools/v2/behavior_impact.ts +39 -0
- package/src/tools/v2/calendar.ts +33 -0
- package/src/tools/v2/coach_ask.ts +131 -0
- package/src/tools/v2/communities.ts +65 -0
- package/src/tools/v2/compare.ts +83 -0
- package/src/tools/v2/cycle.ts +28 -0
- package/src/tools/v2/cycle_log.ts +51 -0
- package/src/tools/v2/day.ts +37 -0
- package/src/tools/v2/endpoints.ts +32 -0
- package/src/tools/v2/hidden_metric.ts +33 -0
- package/src/tools/v2/hr_zones.ts +29 -0
- package/src/tools/v2/hr_zones_set.ts +57 -0
- package/src/tools/v2/journal.ts +31 -0
- package/src/tools/v2/journal_autopop.ts +26 -0
- package/src/tools/v2/journal_catalog.ts +48 -0
- package/src/tools/v2/journal_log.ts +84 -0
- package/src/tools/v2/leaderboard.ts +92 -0
- package/src/tools/v2/lift_catalog.ts +48 -0
- package/src/tools/v2/lift_custom_exercise.ts +117 -0
- package/src/tools/v2/lift_exercise.ts +35 -0
- package/src/tools/v2/lift_history.ts +52 -0
- package/src/tools/v2/lift_library.ts +28 -0
- package/src/tools/v2/lift_log.ts +102 -0
- package/src/tools/v2/lift_progression.ts +34 -0
- package/src/tools/v2/lift_prs.ts +26 -0
- package/src/tools/v2/lift_template_save.ts +89 -0
- package/src/tools/v2/live_hr.ts +26 -0
- package/src/tools/v2/live_state.ts +26 -0
- package/src/tools/v2/live_stress.ts +27 -0
- package/src/tools/v2/performance_assessment.ts +44 -0
- package/src/tools/v2/profile.ts +33 -0
- package/src/tools/v2/profile_update.ts +76 -0
- package/src/tools/v2/raw.ts +50 -0
- package/src/tools/v2/recovery.ts +36 -0
- package/src/tools/v2/sleep.ts +28 -0
- package/src/tools/v2/sleep_need.ts +26 -0
- package/src/tools/v2/smart_alarm.ts +29 -0
- package/src/tools/v2/smart_alarm_set.ts +103 -0
- package/src/tools/v2/sports_catalog.ts +32 -0
- package/src/tools/v2/strain.ts +28 -0
- package/src/tools/v2/stress.ts +28 -0
- package/src/tools/v2/symptom_log.ts +78 -0
- package/src/tools/v2/today.ts +35 -0
- package/src/tools/v2/trend.ts +31 -0
- package/src/tools/v2/workout.ts +28 -0
- package/src/tools/v2/workouts.ts +37 -0
- package/src/whoop/build_lift_body.ts +84 -0
- package/src/whoop/client.ts +118 -0
- package/src/whoop/cognito.ts +150 -0
- package/src/whoop/constants.ts +3 -0
- package/src/whoop/device.ts +106 -0
- package/src/whoop/errors.ts +41 -0
- package/src/whoop/init_timezone.ts +64 -0
- package/src/whoop/installation.ts +58 -0
- package/src/whoop/json_out.ts +9 -0
- package/src/whoop/oauth_provider.ts +377 -0
- package/src/whoop/session_state.ts +41 -0
- package/src/whoop/token_manager.ts +76 -0
- package/src/whoop/token_store.ts +45 -0
- package/src/whoop/types.ts +89 -0
- package/src/whoop/write_safety.ts +30 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// `totem update` — the interactive updater.
|
|
2
|
+
//
|
|
3
|
+
// Three entry points, all dispatched from runUpdate():
|
|
4
|
+
// totem update → interactive: pick a version (newest default, older
|
|
5
|
+
// warns), apply with an animated build, then offer to
|
|
6
|
+
// turn on auto-update.
|
|
7
|
+
// totem update --auto on → install a background job that auto-pulls new releases
|
|
8
|
+
// totem update --auto off → remove it
|
|
9
|
+
// totem update --check → non-interactive: print installed-vs-latest + auto state
|
|
10
|
+
// totem update --auto-run → internal: the scheduled job's non-interactive apply
|
|
11
|
+
//
|
|
12
|
+
// Version index: every release is a git tag (git installs) + an npm version (npm
|
|
13
|
+
// installs); GitHub Releases supplies the notes/dates shown in the picker. No
|
|
14
|
+
// separate hosting — the tags / registry / releases ARE the index.
|
|
15
|
+
import { existsSync, readFileSync, writeFileSync, rmSync, mkdirSync } from "node:fs";
|
|
16
|
+
import { resolve } from "node:path";
|
|
17
|
+
import {
|
|
18
|
+
c, capture, captureAsync, spin, withSpinner, select, promptYesNo, ping, httpGet, commandExists,
|
|
19
|
+
} from "./ui.js";
|
|
20
|
+
|
|
21
|
+
// What index.ts hands us so this module stays free of the command registry.
|
|
22
|
+
export interface UpdateCtx {
|
|
23
|
+
root: string;
|
|
24
|
+
version: string; // current version (PKG.version)
|
|
25
|
+
pkgPath: string; // path to package.json
|
|
26
|
+
pkgName: string; // @thebriangao/totem
|
|
27
|
+
cliEntry: string; // absolute path to the running dist/cli/index.js (for the scheduler)
|
|
28
|
+
isGit: boolean; // git checkout vs npm-global install
|
|
29
|
+
detectDeploy: () => DeployInfo | null;
|
|
30
|
+
redeploy: () => Promise<number>; // = `totem deploy`
|
|
31
|
+
}
|
|
32
|
+
interface DeployInfo { platform: string; app?: string; url?: string; region?: string; project?: string; }
|
|
33
|
+
|
|
34
|
+
interface VersionInfo {
|
|
35
|
+
version: string; // "1.4.2"
|
|
36
|
+
tag: string; // "v1.4.2"
|
|
37
|
+
date?: string | undefined; // "2026-06-07"
|
|
38
|
+
notes?: string | undefined; // release name / first body line
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const AUTO_LABEL = "com.totem.autoupdate";
|
|
42
|
+
const AUTO_INTERVAL_SEC = 6 * 60 * 60; // poll every 6h — there's no push channel to a user's machine
|
|
43
|
+
const cleanUrl = (u: string): string => u.replace(/\/+$/, "");
|
|
44
|
+
const home = (): string => process.env.HOME ?? process.env.USERPROFILE ?? "~";
|
|
45
|
+
const autoLogPath = (root: string): string => resolve(root, ".totem-autoupdate.log");
|
|
46
|
+
|
|
47
|
+
// ── semver ────────────────────────────────────────────────────────────────
|
|
48
|
+
// Compare two x.y.z strings (leading "v" ignored). -1 / 0 / 1.
|
|
49
|
+
export function compareSemver(a: string, b: string): number {
|
|
50
|
+
const pa = a.replace(/^v/, "").split(".").map((n) => parseInt(n, 10) || 0);
|
|
51
|
+
const pb = b.replace(/^v/, "").split(".").map((n) => parseInt(n, 10) || 0);
|
|
52
|
+
for (let i = 0; i < 3; i++) {
|
|
53
|
+
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
54
|
+
if (d !== 0) return d < 0 ? -1 : 1;
|
|
55
|
+
}
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
export function isNewer(candidate: string, current: string): boolean {
|
|
59
|
+
return compareSemver(candidate, current) > 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// The last meaningful line of captured output, for surfacing a failure reason.
|
|
63
|
+
function lastLine(r: { stdout: string; stderr: string }): string {
|
|
64
|
+
const lines = (r.stderr + "\n" + r.stdout).split("\n").map((l) => l.trim()).filter(Boolean);
|
|
65
|
+
return lines.length ? lines[lines.length - 1]! : "";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// owner/repo for the GitHub Releases API, parsed from package.json repository.url.
|
|
69
|
+
function repoSlug(pkgPath: string): string | null {
|
|
70
|
+
try {
|
|
71
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8")) as { repository?: { url?: string } | string };
|
|
72
|
+
const url = typeof pkg.repository === "string" ? pkg.repository : pkg.repository?.url ?? "";
|
|
73
|
+
const m = url.match(/github\.com[/:]([^/]+\/[^/.]+)/);
|
|
74
|
+
return m ? m[1]! : null;
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Best-effort notes/date index from the GitHub Releases API (works for both
|
|
81
|
+
// install types; failure just means the picker shows versions without notes).
|
|
82
|
+
export function parseReleases(json: string): Map<string, { date?: string | undefined; notes?: string | undefined }> {
|
|
83
|
+
const out = new Map<string, { date?: string | undefined; notes?: string | undefined }>();
|
|
84
|
+
try {
|
|
85
|
+
const arr = JSON.parse(json) as Array<{ tag_name?: string; name?: string; body?: string; published_at?: string; draft?: boolean }>;
|
|
86
|
+
for (const r of arr) {
|
|
87
|
+
if (r.draft || !r.tag_name) continue;
|
|
88
|
+
const v = r.tag_name.replace(/^v/, "");
|
|
89
|
+
const firstLine = (r.body ?? "").split("\n").map((l) => l.trim()).find(Boolean) ?? "";
|
|
90
|
+
const notes = (r.name && r.name !== r.tag_name && r.name !== v ? r.name : firstLine).replace(/[*_`#>[\]()]/g, "").trim();
|
|
91
|
+
out.set(v, { date: r.published_at ? r.published_at.slice(0, 10) : undefined, notes: notes || undefined });
|
|
92
|
+
}
|
|
93
|
+
} catch { /* leave empty */ }
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function fetchReleaseNotes(ctx: UpdateCtx): Promise<Map<string, { date?: string | undefined; notes?: string | undefined }>> {
|
|
98
|
+
const slug = repoSlug(ctx.pkgPath);
|
|
99
|
+
if (!slug) return new Map();
|
|
100
|
+
// GitHub's API requires a User-Agent — httpGet sends one ("totem-setup").
|
|
101
|
+
const r = await httpGet(`https://api.github.com/repos/${slug}/releases?per_page=100`, 8000);
|
|
102
|
+
return r.status === 200 ? parseReleases(r.body) : new Map();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// The installable versions for THIS install type, newest first, enriched with notes.
|
|
106
|
+
async function fetchVersions(ctx: UpdateCtx): Promise<VersionInfo[]> {
|
|
107
|
+
const notes = await fetchReleaseNotes(ctx);
|
|
108
|
+
let versions: string[] = [];
|
|
109
|
+
const commitDates = new Map<string, string>();
|
|
110
|
+
if (ctx.isGit) {
|
|
111
|
+
await captureAsync("git", ["fetch", "--tags", "--quiet", "origin"], { cwd: ctx.root });
|
|
112
|
+
const out = capture("git", ["tag", "--sort=-v:refname"], { cwd: ctx.root }).stdout;
|
|
113
|
+
versions = out.split("\n").map((s) => s.trim()).filter((t) => /^v\d+\.\d+\.\d+$/.test(t)).map((t) => t.replace(/^v/, ""));
|
|
114
|
+
// Date a version by when its CODE was committed, not the GitHub release's
|
|
115
|
+
// published_at — backfilled tags (e.g. v1.1.0, tagged days after v1.2.0) would
|
|
116
|
+
// otherwise show the wrong, out-of-order date. `*committerdate` dereferences an
|
|
117
|
+
// annotated tag to its commit; the trailing `committerdate` covers a lightweight
|
|
118
|
+
// tag. The regex just grabs the first YYYY-MM-DD after the ref, which is the
|
|
119
|
+
// commit date in both shapes.
|
|
120
|
+
const dates = capture("git", ["for-each-ref", "--format=%(refname:short) %(*committerdate:short) %(committerdate:short)", "refs/tags"], { cwd: ctx.root }).stdout;
|
|
121
|
+
for (const line of dates.split("\n")) {
|
|
122
|
+
const m = line.trim().match(/^v(\d+\.\d+\.\d+)\s+(\d{4}-\d{2}-\d{2})/);
|
|
123
|
+
if (m) commitDates.set(m[1]!, m[2]!);
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
const out = capture("npm", ["view", ctx.pkgName, "versions", "--json"]).stdout.trim();
|
|
127
|
+
try {
|
|
128
|
+
const parsed = JSON.parse(out) as string[] | string;
|
|
129
|
+
versions = Array.isArray(parsed) ? parsed : [parsed];
|
|
130
|
+
} catch { versions = []; }
|
|
131
|
+
}
|
|
132
|
+
versions = [...new Set(versions)].sort((a, b) => compareSemver(b, a));
|
|
133
|
+
return versions.map((v) => ({ version: v, tag: `v${v}`, date: commitDates.get(v) ?? notes.get(v)?.date, notes: notes.get(v)?.notes }));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ── animated apply ──────────────────────────────────────────────────────────
|
|
137
|
+
// Run one captured step under a spinner; ✓ on success, ✗ + reason on failure.
|
|
138
|
+
async function staged(label: string, fn: () => Promise<{ code: number; stdout: string; stderr: string }>): Promise<boolean> {
|
|
139
|
+
const s = spin(label);
|
|
140
|
+
const r = await fn();
|
|
141
|
+
if (r.code === 0) { s.stop(` ${c.green("✓")} ${label}`); return true; }
|
|
142
|
+
s.stop(` ${c.red("✗")} ${label}`);
|
|
143
|
+
const why = lastLine(r);
|
|
144
|
+
if (why) console.log(c.gray(` ${why}`));
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Pull/install `target`, rebuild, and (if deployed) redeploy + health-check.
|
|
149
|
+
// Returns 0 on success. `latest` is used only to decide latest-vs-pinned git path.
|
|
150
|
+
async function applyVersion(ctx: UpdateCtx, target: VersionInfo, latest: VersionInfo): Promise<number> {
|
|
151
|
+
console.log(c.bold(`\n Updating to v${target.version}`) + c.gray(" ────────────────────────────"));
|
|
152
|
+
|
|
153
|
+
if (ctx.isGit) {
|
|
154
|
+
// Never clobber tracked local edits (a dev checkout). Untracked files (.env,
|
|
155
|
+
// dist/, fly.toml) don't block a checkout, so we ignore them.
|
|
156
|
+
const dirty = capture("git", ["status", "--porcelain", "--untracked-files=no"], { cwd: ctx.root }).stdout.trim();
|
|
157
|
+
if (dirty) {
|
|
158
|
+
console.log(c.red("\n Local changes to tracked files present — commit or stash them first:"));
|
|
159
|
+
console.log(c.gray(" " + dirty.split("\n").slice(0, 5).join("\n ")));
|
|
160
|
+
return 1;
|
|
161
|
+
}
|
|
162
|
+
if (!(await staged("fetching releases from GitHub", () => captureAsync("git", ["fetch", "--tags", "--quiet", "origin"], { cwd: ctx.root })))) return 1;
|
|
163
|
+
if (target.version === latest.version) {
|
|
164
|
+
// Latest lives on main; make sure we're on the branch, then fast-forward.
|
|
165
|
+
const branch = capture("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd: ctx.root }).stdout.trim();
|
|
166
|
+
if (branch !== "main" && !(await staged("switching to main", () => captureAsync("git", ["checkout", "main"], { cwd: ctx.root })))) return 1;
|
|
167
|
+
if (!(await staged("pulling latest code", () => captureAsync("git", ["pull", "--ff-only", "origin", "main"], { cwd: ctx.root })))) return 1;
|
|
168
|
+
} else {
|
|
169
|
+
// A pinned older/newer version is a tag → detached checkout.
|
|
170
|
+
if (!(await staged(`checking out ${target.tag}`, () => captureAsync("git", ["checkout", "--quiet", target.tag], { cwd: ctx.root })))) return 1;
|
|
171
|
+
}
|
|
172
|
+
if (!(await staged("installing dependencies", () => captureAsync("npm", ["install"], { cwd: ctx.root })))) return 1;
|
|
173
|
+
const tsc = resolve(ctx.root, "node_modules", ".bin", "tsc");
|
|
174
|
+
if (!(await staged("building", () => captureAsync(process.execPath, [tsc], { cwd: ctx.root })))) return 1;
|
|
175
|
+
} else {
|
|
176
|
+
if (!(await staged(`installing ${ctx.pkgName}@${target.version}`, () => captureAsync("npm", ["install", "-g", `${ctx.pkgName}@${target.version}`])))) return 1;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Propagate the new code to wherever the server actually runs. What that takes
|
|
180
|
+
// depends entirely on the user's RECORDED deploy state — branch on it, never
|
|
181
|
+
// assume:
|
|
182
|
+
// • no record → nothing remote exists; local code is already updated.
|
|
183
|
+
// • local (stdio) → just restart the MCP client to pick up the new build.
|
|
184
|
+
// • fly/railway/cloudrun → rebuild-from-source on deploy; fully automatic.
|
|
185
|
+
// • custom / self-host → a container on a box we can't reach; we must NOT fake
|
|
186
|
+
// a deploy or health-check (it'd pass against the OLD container) — print the
|
|
187
|
+
// exact rebuild/restart steps instead.
|
|
188
|
+
const d = ctx.detectDeploy();
|
|
189
|
+
const AUTO_DEPLOY = new Set(["fly", "railway", "cloudrun"]);
|
|
190
|
+
if (!d || d.platform === "local") {
|
|
191
|
+
console.log(c.gray(d?.platform === "local"
|
|
192
|
+
? "\n Local install — restart your MCP client to load the new build."
|
|
193
|
+
: "\n No deployment recorded — local code is updated (run `totem cloud` to deploy a server)."));
|
|
194
|
+
} else if (AUTO_DEPLOY.has(d.platform)) {
|
|
195
|
+
// The platform CLI must be present to push from here. If it's gone (a fresh
|
|
196
|
+
// machine, or you ran update from a different box than you deployed from),
|
|
197
|
+
// don't surface a cryptic `spawn fly ENOENT` + "server unchanged" — the local
|
|
198
|
+
// code IS updated; say plainly the redeploy needs the CLI.
|
|
199
|
+
const cliMissing = d.platform === "fly" ? !(commandExists("fly") || commandExists("flyctl"))
|
|
200
|
+
: d.platform === "railway" ? !commandExists("railway")
|
|
201
|
+
: !commandExists("gcloud");
|
|
202
|
+
if (cliMissing) {
|
|
203
|
+
const cli = d.platform === "fly" ? "flyctl" : d.platform === "railway" ? "railway" : "gcloud";
|
|
204
|
+
console.log(c.yellow(`\n Local code updated — but the ${d.platform} CLI (${cli}) isn't installed here, so the deployment wasn't pushed.`));
|
|
205
|
+
console.log(c.gray(` Install ${cli} and run \`totem deploy\`, or re-run \`totem cloud\`.`));
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
console.log(c.bold(`\n Redeploying to ${d.platform}${d.app ? ` (${d.app})` : ""}…`));
|
|
209
|
+
if ((await ctx.redeploy()) !== 0) { console.log(c.red(" ✗ deploy failed — your live server is unchanged.")); return 1; }
|
|
210
|
+
if (d.url) {
|
|
211
|
+
const url = `${cleanUrl(d.url)}/health`;
|
|
212
|
+
console.log(c.gray(` health check → ${url}`));
|
|
213
|
+
if ((await ping(url)) !== 0) {
|
|
214
|
+
console.log(c.red(" ⚠ /health didn't return 200 — the server may be unhealthy."));
|
|
215
|
+
console.log(c.gray(d.platform === "fly" ? ` Roll back: fly releases -a ${d.app} → redeploy the prior release.` : " Roll back to the previous version on your host."));
|
|
216
|
+
return 1;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// A redeploy restarts the server. This is a code-only update — secrets persist
|
|
220
|
+
// on the host, so tokens carry over. But /health passing doesn't prove Whoop
|
|
221
|
+
// auth survived the restart (cloud uses the in-memory token store, which can
|
|
222
|
+
// strand a rotated refresh token), so point at the fix if calls start failing.
|
|
223
|
+
console.log(c.gray(" Tokens persist across the redeploy. If Whoop calls start failing afterward, run `totem auth` to refresh + re-push them."));
|
|
224
|
+
} else {
|
|
225
|
+
printSelfHostUpdate(ctx, d);
|
|
226
|
+
}
|
|
227
|
+
return 0;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Self-hosted (Docker on a VPS / Render / a home box / …) — totem can't reach the
|
|
231
|
+
// container, and the run command varies per user (compose, ports, restart policy,
|
|
232
|
+
// remote host), so we never hardcode it: print the canonical rebuild/restart recipe
|
|
233
|
+
// + the env file we wrote at deploy time, and let the user adapt it to their host.
|
|
234
|
+
function printSelfHostUpdate(ctx: UpdateCtx, d: DeployInfo): void {
|
|
235
|
+
const hasEnv = existsSync(resolve(ctx.root, ".env.deploy"));
|
|
236
|
+
console.log(c.yellow(`\n Self-hosted (${d.platform}) — finish the update where your container runs (totem can't reach it):`));
|
|
237
|
+
console.log(` ${c.violet("1")} Get this updated code onto that host ${c.gray("(git pull your checkout there, or copy the build over)")}`);
|
|
238
|
+
console.log(` ${c.violet("2")} Rebuild: ${c.cyan("docker build -t totem .")}`);
|
|
239
|
+
console.log(` ${c.violet("3")} Restart: ${c.cyan(`docker run -d --restart unless-stopped -p 3000:3000 --env-file ${hasEnv ? ".env.deploy" : "<your-env-file>"} totem`)}`);
|
|
240
|
+
console.log(` ${c.gray("or: docker compose up -d --build · or your host's redeploy (Render/Coolify/etc.)")}`);
|
|
241
|
+
if (d.url) console.log(` ${c.violet("4")} Verify: ${c.gray(`curl ${cleanUrl(d.url)}/health`)}`);
|
|
242
|
+
console.log(c.gray("\n Your local code + build are already on the new version — only the remote container is left."));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function printDone(version: string): void {
|
|
246
|
+
console.log("");
|
|
247
|
+
console.log(` ${c.green("✓")} ${c.bold(`Now on v${version}`)}`);
|
|
248
|
+
console.log(c.brandDim(" ▁▁▂▂▆▂▁▁▁▁▂▂▆▂▁▁▁▁▂▂▆▂▁▁▁▁▂▂▆▂▁▁"));
|
|
249
|
+
console.log("");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ── auto-update scheduler (launchd on macOS, cron on Linux) ──────────────────
|
|
253
|
+
function plistPath(): string { return resolve(home(), "Library", "LaunchAgents", `${AUTO_LABEL}.plist`); }
|
|
254
|
+
|
|
255
|
+
function xmlEscape(s: string): string {
|
|
256
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// A launchd LaunchAgent that runs `totem update --auto-run` every intervalSec.
|
|
260
|
+
export function launchdPlist(programArgs: string[], intervalSec: number, workingDir: string, logPath: string): string {
|
|
261
|
+
const args = programArgs.map((a) => ` <string>${xmlEscape(a)}</string>`).join("\n");
|
|
262
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
263
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
264
|
+
<plist version="1.0">
|
|
265
|
+
<dict>
|
|
266
|
+
<key>Label</key>
|
|
267
|
+
<string>${AUTO_LABEL}</string>
|
|
268
|
+
<key>ProgramArguments</key>
|
|
269
|
+
<array>
|
|
270
|
+
${args}
|
|
271
|
+
</array>
|
|
272
|
+
<key>StartInterval</key>
|
|
273
|
+
<integer>${intervalSec}</integer>
|
|
274
|
+
<key>RunAtLoad</key>
|
|
275
|
+
<false/>
|
|
276
|
+
<key>WorkingDirectory</key>
|
|
277
|
+
<string>${xmlEscape(workingDir)}</string>
|
|
278
|
+
<key>StandardOutPath</key>
|
|
279
|
+
<string>${xmlEscape(logPath)}</string>
|
|
280
|
+
<key>StandardErrorPath</key>
|
|
281
|
+
<string>${xmlEscape(logPath)}</string>
|
|
282
|
+
</dict>
|
|
283
|
+
</plist>
|
|
284
|
+
`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// One cron line, tagged so we can find + remove exactly ours.
|
|
288
|
+
export function cronLine(programArgs: string[], root: string, logPath: string): string {
|
|
289
|
+
const cmd = programArgs.map((a) => (/\s/.test(a) ? `"${a}"` : a)).join(" ");
|
|
290
|
+
return `0 */6 * * * cd ${root} && ${cmd} >> ${logPath} 2>&1 # ${AUTO_LABEL}`;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function isAutoOn(): boolean {
|
|
294
|
+
if (process.platform === "darwin") return existsSync(plistPath());
|
|
295
|
+
if (process.platform === "linux") {
|
|
296
|
+
const r = capture("crontab", ["-l"]);
|
|
297
|
+
return r.code === 0 && r.stdout.includes(AUTO_LABEL);
|
|
298
|
+
}
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function autoRunArgs(ctx: UpdateCtx): string[] {
|
|
303
|
+
return [process.execPath, ctx.cliEntry, "update", "--auto-run"];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async function enableAuto(ctx: UpdateCtx): Promise<boolean> {
|
|
307
|
+
const args = autoRunArgs(ctx);
|
|
308
|
+
const log = autoLogPath(ctx.root);
|
|
309
|
+
if (process.platform === "darwin") {
|
|
310
|
+
const p = plistPath();
|
|
311
|
+
mkdirSync(resolve(home(), "Library", "LaunchAgents"), { recursive: true });
|
|
312
|
+
writeFileSync(p, launchdPlist(args, AUTO_INTERVAL_SEC, ctx.root, log));
|
|
313
|
+
await captureAsync("launchctl", ["unload", p]); // ignore if not loaded
|
|
314
|
+
const load = await captureAsync("launchctl", ["load", p]);
|
|
315
|
+
if (load.code !== 0) { console.log(c.red(` ✗ couldn't load the launchd agent: ${lastLine(load)}`)); return false; }
|
|
316
|
+
console.log(c.green(" ✓ auto-update on") + c.gray(` — checks every 6h via launchd (${p})`));
|
|
317
|
+
} else if (process.platform === "linux") {
|
|
318
|
+
const existing = capture("crontab", ["-l"]).stdout.split("\n").filter((l) => l && !l.includes(AUTO_LABEL));
|
|
319
|
+
existing.push(cronLine(args, ctx.root, log));
|
|
320
|
+
const set = await captureAsync("crontab", ["-"], { input: existing.join("\n") + "\n" });
|
|
321
|
+
if (set.code !== 0) { console.log(c.red(` ✗ couldn't write crontab: ${lastLine(set)}`)); return false; }
|
|
322
|
+
console.log(c.green(" ✓ auto-update on") + c.gray(" — checks every 6h via cron"));
|
|
323
|
+
} else {
|
|
324
|
+
console.log(c.yellow(" Auto-update isn't wired for this OS. Schedule this command every 6h yourself:"));
|
|
325
|
+
console.log(c.gray(` ${args.join(" ")}`));
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
console.log(c.gray(` Log: ${log} · turn off: totem update --auto off`));
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async function disableAuto(ctx: UpdateCtx): Promise<boolean> {
|
|
333
|
+
if (process.platform === "darwin") {
|
|
334
|
+
const p = plistPath();
|
|
335
|
+
if (existsSync(p)) { await captureAsync("launchctl", ["unload", p]); rmSync(p, { force: true }); }
|
|
336
|
+
console.log(c.green(" ✓ auto-update off"));
|
|
337
|
+
} else if (process.platform === "linux") {
|
|
338
|
+
const kept = capture("crontab", ["-l"]).stdout.split("\n").filter((l) => l && !l.includes(AUTO_LABEL));
|
|
339
|
+
await captureAsync("crontab", ["-"], { input: kept.length ? kept.join("\n") + "\n" : "\n" });
|
|
340
|
+
console.log(c.green(" ✓ auto-update off"));
|
|
341
|
+
} else {
|
|
342
|
+
console.log(c.yellow(" Nothing scheduled by us on this OS — remove any manual schedule yourself."));
|
|
343
|
+
}
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// ── flows ────────────────────────────────────────────────────────────────────
|
|
348
|
+
async function runInteractive(ctx: UpdateCtx): Promise<number> {
|
|
349
|
+
console.log(c.bold("\ntotem update") + c.gray(` — you're on v${ctx.version}\n`));
|
|
350
|
+
const versions = await withSpinner("checking available versions", () => fetchVersions(ctx));
|
|
351
|
+
if (!versions.length) {
|
|
352
|
+
console.log(c.red(" Couldn't list versions (offline, or no releases found)."));
|
|
353
|
+
return 1;
|
|
354
|
+
}
|
|
355
|
+
const latest = versions[0]!;
|
|
356
|
+
const choices = versions.map((v) => {
|
|
357
|
+
const tags: string[] = [];
|
|
358
|
+
if (v.version === latest.version) tags.push(c.green("latest"));
|
|
359
|
+
if (v.version === ctx.version) tags.push(c.cyan("current"));
|
|
360
|
+
if (v.date) tags.push(c.dim(v.date));
|
|
361
|
+
const note = v.notes ? c.dim("· " + (v.notes.length > 44 ? v.notes.slice(0, 43) + "…" : v.notes)) : "";
|
|
362
|
+
return { label: `v${v.version}`, hint: [tags.join(" "), note].filter(Boolean).join(" ") };
|
|
363
|
+
});
|
|
364
|
+
const idx = await select("Update to which version? (newest is default)", choices, { defaultIndex: 0 });
|
|
365
|
+
const target = versions[idx]!;
|
|
366
|
+
const onLatest = target.version === latest.version;
|
|
367
|
+
|
|
368
|
+
if (!onLatest) {
|
|
369
|
+
const ahead = versions.filter((v) => compareSemver(v.version, target.version) > 0).map((v) => "v" + v.version);
|
|
370
|
+
console.log(c.yellow(`\n ⚠ v${target.version} is NOT the newest — you'd be behind: ${ahead.join(", ")}.`));
|
|
371
|
+
if (!(await promptYesNo(" Install this older version anyway?", false))) { console.log(c.gray(" Cancelled.")); return 0; }
|
|
372
|
+
} else if (target.version === ctx.version) {
|
|
373
|
+
if (!(await promptYesNo(`\n You're already on v${ctx.version} (the latest). Re-apply anyway?`, false))) { console.log(c.gray(" Nothing to do.")); return 0; }
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const code = await applyVersion(ctx, target, latest);
|
|
377
|
+
if (code !== 0) return code;
|
|
378
|
+
|
|
379
|
+
// After a successful apply, manage the auto-update toggle.
|
|
380
|
+
const autoOn = isAutoOn();
|
|
381
|
+
if (onLatest) {
|
|
382
|
+
if (!autoOn) {
|
|
383
|
+
console.log("");
|
|
384
|
+
if (await promptYesNo(" Turn on auto-update? (auto-pulls + sets up every new release)", false)) await enableAuto(ctx);
|
|
385
|
+
} else {
|
|
386
|
+
console.log(c.gray("\n Auto-update is on — new releases install themselves."));
|
|
387
|
+
}
|
|
388
|
+
} else if (autoOn) {
|
|
389
|
+
console.log(c.yellow("\n ⚠ Auto-update is ON — it'll move you back to the latest on the next check."));
|
|
390
|
+
if (await promptYesNo(" Turn auto-update OFF to stay pinned to this version?", true)) await disableAuto(ctx);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
printDone(target.version);
|
|
394
|
+
return 0;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
async function runCheck(ctx: UpdateCtx): Promise<number> {
|
|
398
|
+
console.log(c.bold("\ntotem update --check") + c.gray(` — installed v${ctx.version}\n`));
|
|
399
|
+
const versions = await withSpinner("checking available versions", () => fetchVersions(ctx));
|
|
400
|
+
if (!versions.length) { console.log(c.red(" Couldn't list versions (offline?).")); return 1; }
|
|
401
|
+
const latest = versions[0]!;
|
|
402
|
+
console.log(` installed: v${ctx.version}`);
|
|
403
|
+
console.log(` latest: v${latest.version}${latest.date ? c.gray(" " + latest.date) : ""}`);
|
|
404
|
+
console.log(isNewer(latest.version, ctx.version)
|
|
405
|
+
? c.green(` ▲ update available — run \`totem update\``)
|
|
406
|
+
: c.green(` ✓ up to date`));
|
|
407
|
+
console.log(c.gray(` auto-update: ${isAutoOn() ? "on" : "off"} · ${versions.length} versions available`));
|
|
408
|
+
return 0;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// The scheduled job: non-interactive, pull-to-latest-if-newer, log everything.
|
|
412
|
+
async function runAuto(ctx: UpdateCtx): Promise<number> {
|
|
413
|
+
const stamp = new Date().toISOString();
|
|
414
|
+
const versions = await fetchVersions(ctx);
|
|
415
|
+
const latest = versions[0];
|
|
416
|
+
if (!latest) { console.log(`[totem auto-update ${stamp}] no version list (offline?) — skipping`); return 0; }
|
|
417
|
+
if (!isNewer(latest.version, ctx.version)) { console.log(`[totem auto-update ${stamp}] up to date on v${ctx.version}`); return 0; }
|
|
418
|
+
console.log(`[totem auto-update ${stamp}] new release v${latest.version} (was v${ctx.version}) — applying`);
|
|
419
|
+
const code = await applyVersion(ctx, latest, latest);
|
|
420
|
+
console.log(`[totem auto-update ${stamp}] ${code === 0 ? `done → v${latest.version}` : `FAILED (exit ${code})`}`);
|
|
421
|
+
return code;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export async function runUpdate(args: string[], ctx: UpdateCtx): Promise<number> {
|
|
425
|
+
const autoIdx = args.indexOf("--auto");
|
|
426
|
+
if (autoIdx !== -1) {
|
|
427
|
+
const val = (args[autoIdx + 1] ?? "").toLowerCase();
|
|
428
|
+
if (val === "on") {
|
|
429
|
+
console.log(c.bold("\ntotem update --auto on\n"));
|
|
430
|
+
if (!(await enableAuto(ctx))) return 1;
|
|
431
|
+
// Enabling implies "I want newest" — do one check/pull right now.
|
|
432
|
+
console.log("");
|
|
433
|
+
return runAuto(ctx);
|
|
434
|
+
}
|
|
435
|
+
if (val === "off") { console.log(c.bold("\ntotem update --auto off\n")); return (await disableAuto(ctx)) ? 0 : 1; }
|
|
436
|
+
console.log(c.red("Usage: totem update --auto <on|off>"));
|
|
437
|
+
return 1;
|
|
438
|
+
}
|
|
439
|
+
if (args.includes("--auto-run")) return runAuto(ctx);
|
|
440
|
+
if (args.includes("--check")) return runCheck(ctx);
|
|
441
|
+
return runInteractive(ctx);
|
|
442
|
+
}
|