From 106c0d68410a1c017eb3334fb7e8689b5d008b0c Mon Sep 17 00:00:00 2001 From: Mozafar Haider Date: Thu, 15 Oct 2026 14:51:57 +0100 Subject: [PATCH] feat(skill-calendar-upgrade): add calendar-upgrade skill Moves DHIS2 apps onto @dhis2/multi-calendar-dates 3.2.0+ and direct @js-temporal/polyfill usage onto 0.5.1, as a behavioural migration: before/after regression snapshots, migrating hand-rolled date code onto formatCalendarDate/addToCalendarDate/getDaysInMonth, the Ethiopic era patch for CLDR 48, and a source-map check of the Temporal copies in the build. Checks @dhis2/ui is 10.19.3+ first, since older versions bring their own multi-calendar-dates 2.x. Split out of the modernise-apps skill, which now points to it. Co-Authored-By: Claude Opus 5.5 --- README.md | 24 +- src/skills/calendar-upgrade/SKILL.md | 136 +++++++ src/skills/calendar-upgrade/evals/evals.json | 17 + src/skills/calendar-upgrade/package.json | 12 + .../references/bundle-temporal-check.md | 97 +++++ .../references/multi-calendar-dates-bump.md | 348 ++++++++++++++++++ .../references/temporal-polyfill-upgrade.md | 139 +++++++ src/skills/modernise-apps/SKILL.md | 26 +- .../references/platform-library-bumps.md | 2 +- 9 files changed, 786 insertions(+), 15 deletions(-) create mode 100644 src/skills/calendar-upgrade/SKILL.md create mode 100644 src/skills/calendar-upgrade/evals/evals.json create mode 100644 src/skills/calendar-upgrade/package.json create mode 100644 src/skills/calendar-upgrade/references/bundle-temporal-check.md create mode 100644 src/skills/calendar-upgrade/references/multi-calendar-dates-bump.md create mode 100644 src/skills/calendar-upgrade/references/temporal-polyfill-upgrade.md diff --git a/README.md b/README.md index 680a14a..9b8e056 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ app can need any combination of, or none: `@dhis2/d2-i18n` — non-breaking (current major) by default for each, with an explicit choice offered whenever `latest` would cross a major version. Upgrading `@dhis2/multi-calendar-dates`/`@js-temporal/polyfill` is left to the separate - `multi-calendar-dates-upgrade` skill. + `calendar-upgrade` skill. - **README refresh.** A pnpm badge alongside existing badges, an app description pulled from the App Hub when the app is published there, and the scaffold's verbose "Available Scripts" section collapsed into a concise "Get Started". @@ -106,6 +106,28 @@ still renders after logging in. npx skills add dhis2/ai-devtools --skill modernise-apps ``` +## `@dhis2/skill-calendar-upgrade` + +An AI skill for moving an existing DHIS2 app onto `@dhis2/multi-calendar-dates` `3.x` and +`@js-temporal/polyfill` `0.5.1`. `3.x` removed Temporal types from the library's public API, +so this is treated as a behavioural migration rather than a routine bump: + +- **Library bump.** A before/after regression snapshot test of every direct import + (`getNowInCalendar`, the period helpers, `useDatePicker`) across the `gregory`, + `ethiopian` and `nepali` calendars, captured before bumping and re-run unmodified after. +- **Helper migration.** Replaces the app's own calendar date formatting and arithmetic with + the library's `formatCalendarDate`, `addToCalendarDate` and `getDaysInMonth`. +- **Direct polyfill upgrade.** Moves apps that import `@js-temporal/polyfill` from `0.4.3` to + `0.5.1`, with snapshot tests and a revertable patch for CLDR 48's Ethiopic era codes. +- **Bundle check.** Lists the Temporal copies that end up in the production build from its + source maps, and works to get them down to one. + +### Install + +```sh +npx skills add dhis2/ai-devtools --skill calendar-upgrade +``` + --- ## Contributing diff --git a/src/skills/calendar-upgrade/SKILL.md b/src/skills/calendar-upgrade/SKILL.md new file mode 100644 index 0000000..38cce49 --- /dev/null +++ b/src/skills/calendar-upgrade/SKILL.md @@ -0,0 +1,136 @@ +--- +name: calendar-upgrade +description: > + Guide for upgrading an existing DHIS2 app to @dhis2/multi-calendar-dates 3.x (3.2.0+) and + @js-temporal/polyfill 0.5.1. Use this skill whenever the user wants to bump or update + @dhis2/multi-calendar-dates or @js-temporal/polyfill, fix code broken by + multi-calendar-dates 3.x (getNowInCalendar or convertFromIso8601/convertToIso8601 no longer + returning Temporal objects), replace an app's own calendar date formatting/arithmetic with + multi-calendar-dates' formatCalendarDate, addToCalendarDate or getDaysInMonth helpers, + remove an @js-temporal/polyfill 0.4.3 resolutions pin, fix "Era am ... was not matched by + any era" errors for Ethiopian dates, or check which Temporal copies end up in an app's + bundle. For general tooling modernisation (pnpm, lint configs, CI, @dhis2/ui/app-runtime + bumps), use the modernise-apps skill instead. +--- + +# Upgrading DHIS2 Calendar Libraries + +You are helping a developer move an existing DHIS2 app onto `@dhis2/multi-calendar-dates` +`3.x` (target `3.2.0`+) and, where the app imports it directly, `@js-temporal/polyfill` +`0.5.1`. Unlike a routine dependency bump, this is a **behavioural migration**: +[`dhis2/multi-calendar-dates#102`](https://github.com/dhis2/multi-calendar-dates/pull/102) +removed Temporal types from the library's public API, so wrong results compile and run fine. +Every step is guarded by regression snapshots captured _before_ the change. + +It covers four tasks, in this order — an app may need only some of them: + +1. **Bump `@dhis2/multi-calendar-dates` to `3.2.0`+**, with a before/after regression test + covering every direct import from the library. +2. **Migrate the app's own calendar date code onto the library's helpers** + (`formatCalendarDate`, `addToCalendarDate`, `getDaysInMonth`), deleting the app's copies. +3. **Upgrade direct `@js-temporal/polyfill` usage from `0.4.3` to `0.5.1`**, and patch + `0.5.1`'s Ethiopic era handling (applied by default, offered for revert). +4. **Check which Temporal copies end up in the production build**, and reduce them to the + library's vendored copy plus `0.5.1`. + +Nothing here fixes `multi-calendar-dates` `2.x` or the polyfill's `0.4.3`. Patching those is +only a temporary, uncommitted step to capture a test baseline before bumping. + +## First: check `@dhis2/ui` + +`@dhis2/ui` versions before `10.19.3` bring their own `multi-calendar-dates` `2.x` (and the +`0.4.3` polyfill), so the app can't end up with a single `3.x`/`0.5.1` copy until `@dhis2/ui` +is at least `10.19.3` — directly, or through the app shell (`@dhis2/cli-app-scripts`). Check: + +```bash +node -p "require('./package.json').dependencies?.['@dhis2/ui']" +npm view @dhis2/ui@10 version +``` + +- **Already `10.19.3`+**: nothing to do. +- **On `10.x` below `10.19.3`**: bump `@dhis2/ui` to the latest `10.x` first, in its own + commit. It's a same-major bump, so it should be safe; run `lint`/`test` after it. If the + `modernise-apps` skill is installed, follow its platform library bump step + (`references/platform-library-bumps.md`, Step 1). +- **On `9.x` or older**: that's a breaking upgrade of its own. Don't fold it into this + migration — ask the user whether to do it first as a separate task, or to go ahead without + it and accept the extra copies (reported in the summary, see + `references/bundle-temporal-check.md`). + +## Which tasks apply? + +| Check | How | Result | +| --------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | --------------------------------------------------------------- | +| `@dhis2/multi-calendar-dates` is in `package.json` and below `3.2.0` | Read `package.json` + `npm view @dhis2/multi-calendar-dates dist-tags` | **Library behind** — `references/multi-calendar-dates-bump.md` | +| `src` formats calendar dates (`eraYear`, `padStart`) or does month/day arithmetic around `multi-calendar-dates` calls | See the grep in `references/multi-calendar-dates-bump.md` | **Hand-rolled date code** — migrate it to the `3.2.0` helpers | +| `src` imports `@js-temporal/polyfill` and it's pinned below `0.5.1` (deps or `resolutions`) | `grep -rln "@js-temporal/polyfill" src` + read `package.json` | **Polyfill behind** — `references/temporal-polyfill-upgrade.md` | +| Any of the above was done | — | **Check the bundle** — `references/bundle-temporal-check.md` | +| None of the packages is used | — | **Nothing to do** — this skill doesn't apply | + +## What does the user need? + +| Scenario | References (read in order) | +| --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | +| Bump `@dhis2/multi-calendar-dates` to the latest version | `references/multi-calendar-dates-bump.md`, then `references/bundle-temporal-check.md` | +| Replace the app's own calendar date formatting/arithmetic with `multi-calendar-dates` helpers | `references/multi-calendar-dates-bump.md` ("Replace hand-rolled calendar date code") | +| Upgrade direct `@js-temporal/polyfill` usage from `0.4.3` to `0.5.1` | `references/temporal-polyfill-upgrade.md`, then `references/bundle-temporal-check.md` | +| Check which Temporal copies end up in the build, and reduce them to `0.5.1` | `references/bundle-temporal-check.md` | + +## Rules + +- **`@dhis2/multi-calendar-dates`'s `3.x` is a real breaking change — don't treat it like a + routine bump.** `getNowInCalendar` no longer returns anything Temporal-shaped (calling + `.add()` etc. on it throws). Target `3.2.0`+. +- **Migrate hand-rolled calendar date code to the library's helpers — don't re-implement it.** + Every place the app formats a calendar date (`eraYear ?? year` + padding), adds months/ + years/days to one (13-month calendar lists, ISO round-trips, `validateDateString` clamping + loops) or works out days in a month should use `formatCalendarDate`, `addToCalendarDate` or + `getDaysInMonth` from `3.2.0`, and the app's own helper should be deleted. Do it in its own + commit after the bump; the regression snapshots should stay unchanged, and any change must + be explained. See `references/multi-calendar-dates-bump.md`. +- **Before bumping `@dhis2/multi-calendar-dates`, write a before/after regression test + covering every direct import from the library** — `getNowInCalendar`, the + period-calculation helpers (`getFixedPeriodByDate`, `getAdjacentFixedPeriods`, etc. — + `#102` rewrote these too, not just `getNowInCalendar`), and `useDatePicker` if the app + uses it directly. The exclusion is `@dhis2/ui`'s own bundled `Calendar`/`DatePicker` + components (a separate internal copy this app doesn't control), not this library's own + hook — check the import, not the visible UI. Cover `gregory`, `ethiopian`, and `nepali` + for each; run the test against the currently-installed version first to confirm it's a + real baseline, then bump and re-run it unmodified. Add it as a real committed test if the + app has a test runner; otherwise use a temporary standalone script (a headless-hook test + via `renderHook` for `useDatePicker` if used) and delete it once confirmed safe. Don't rely + on a `lint`/generic `test` pass alone as proof this is safe. See + `references/multi-calendar-dates-bump.md`. +- **Before upgrading a direct `@js-temporal/polyfill` dependency to `0.5.1`, snapshot-test + every function in the app that calls it**, against `gregory`, `ethiopian` and `nepali` + (those three matter, the rest don't), with "now" pinned. Capture the snapshots on `0.4.3` + first, then re-run them unmodified with `--ci` on `0.5.1`. Declare `0.5.1` as a real + dependency rather than a `resolutions` pin. On CLDR 48 runtimes, Ethiopic throws in **both** + versions ([LIBS-830](https://dhis2.atlassian.net/browse/LIBS-830)). Patch `0.4.3` only + temporarily, to capture the baseline, and **never commit that patch**. **Apply the `0.5.1` + patch by default**, in its own commit. In the summary, **offer to revert it**: say that the patch + file is ~516 KB because the dist is minified onto one line, and that the alternative is to + wait for the upstream fix. If reverted, leave `ethiopian` out of these tests and note the + known issue in the PR description. See `references/temporal-polyfill-upgrade.md`. +- **After bumping `@dhis2/multi-calendar-dates` or `@js-temporal/polyfill`, check the + production build's source maps for Temporal copies.** The only one should be + `multi-calendar-dates`' vendored copy (plus a direct `@js-temporal/polyfill@0.5.1` if the + app imports it). Flag multiple versions in the lockfile, and any extra copy in the bundle. + Try to get the bundle down to `0.5.1` only: bump the package that brings the old copy in, + or else force `0.5.1` with `resolutions`/`pnpm.overrides` after snapshot-testing that code, + plus the Ethiopic patch if Ethiopic matters for the app. See + `references/bundle-temporal-check.md`. + +## Troubleshooting + +| Symptom | Fix | +| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| The app has its own date formatting, month/day arithmetic or days-in-month code around `multi-calendar-dates` calls | Migrate it to `formatCalendarDate`/`addToCalendarDate`/`getDaysInMonth` and delete the app's copy. See "Replace hand-rolled calendar date code with the library's helpers" in `references/multi-calendar-dates-bump.md`. | +| A regression snapshot changes when moving onto the `3.2.0` helpers | Explain it before updating: the `ethiopic` id getting the era year, a nonexistent date (e.g. month 13 in a 12-month calendar) now throwing, or Nepali month arithmetic no longer drifting are expected. Anything else is a regression. See `references/multi-calendar-dates-bump.md`. | +| `RangeError: Era am (ISO year ...) was not matched by any era` on the `ethiopian` calendar while capturing the baseline | Unrelated `@js-temporal/polyfill@0.4.3` bug with CLDR 48's new Ethiopic era codes on newer Node. Temporarily patch the polyfill to capture the baseline, then revert it and never commit it (`3.x` vendors the fix). See "If `ethiopian` fails before you've bumped anything" in `references/multi-calendar-dates-bump.md`. | +| The before/after regression test fails on the `nepali` calendar specifically, but `gregory`/`ethiopian` pass | Expected to be the highest-risk case — `#102` rewrote Nepali support from scratch (it's the one calendar this library implements itself rather than delegating to CLDR). Don't assume it's a test bug; diff the actual before/after values and check the library's own changelog/tests for that release before concluding it's safe. | +| `warning Resolution field "@js-temporal/polyfill@0.4.3" is incompatible with requested version "@js-temporal/polyfill@0.5.1"` | The app pins the polyfill in `resolutions`, forcing `multi-calendar-dates`/`@dhis2/ui` onto `0.4.3`. Follow `references/temporal-polyfill-upgrade.md`: snapshot-test direct usages, then move to `0.5.1` as a real dependency. | +| `RangeError: Era am/aa … was not matched by any era` from app code (not just while capturing a baseline) | The app calls the polyfill directly for Ethiopic dates, and CLDR 48 era codes break it. After the upgrade, apply the `0.5.1` patch from `references/temporal-polyfill-upgrade.md` by default, then offer to revert it in the summary (~516 KB), noting the known issue in the PR if reverted. Don't fix it on `0.4.3`: that patch is only a temporary baseline aid. Upstream fix: [LIBS-830](https://dhis2.atlassian.net/browse/LIBS-830). | +| The `references/bundle-temporal-check.md` check shows an `@js-temporal/polyfill` entry other than a direct `0.5.1`, or a `multi-calendar-dates` below `3.x` (e.g. `1.3.2` under `@dhis2/analytics`) | An older `multi-calendar-dates` is bundled with its own polyfill (the polyfill may be hoisted to the top level, so check the versions, not the paths). Bump the package that brings it in; otherwise force `0.5.1` with `resolutions`/`pnpm.overrides`, snapshot-test that code, and add the Ethiopic patch from `references/temporal-polyfill-upgrade.md` if Ethiopic matters. See `references/bundle-temporal-check.md`. | +| No test runner is set up and it's unclear whether to add one just for this | Don't — that's a bigger decision than a dependency bump warrants. Use the standalone verify script instead, and delete it once the bump is confirmed safe. | +| Unsure whether `useDatePicker` is in scope for this app's regression test, since the app also uses `@dhis2/ui`'s `Calendar`/`DatePicker` | Check the import, not the visible UI — `import { useDatePicker } from '@dhis2/multi-calendar-dates'` is in scope (direct usage of the version this app controls); `@dhis2/ui`'s bundled `Calendar`/`DatePicker` components are not (they resolve their own separate internal copy). | diff --git a/src/skills/calendar-upgrade/evals/evals.json b/src/skills/calendar-upgrade/evals/evals.json new file mode 100644 index 0000000..03013ea --- /dev/null +++ b/src/skills/calendar-upgrade/evals/evals.json @@ -0,0 +1,17 @@ +{ + "skill_name": "calendar-upgrade", + "evals": [ + { + "id": 0, + "name": "multi-calendar-dates-bump-aggregate-data-entry-app", + "prompt": "Can you update @dhis2/multi-calendar-dates to the latest version in this app?", + "expected_output": "A real snapshot of dhis2/aggregate-data-entry-app on @dhis2/multi-calendar-dates ^2.1.2. The target is 3.2.0 or later, a genuine breaking change (dhis2/multi-calendar-dates#102): getNowInCalendar returns a plain { year, month, day, eraYear?, hour, minute, second } object instead of a Temporal.ZonedDateTime, and the library provides formatCalendarDate, addToCalendarDate and getDaysInMonth. This app's src/shared/date/get-now-in-calendar.js wraps it in getNowInCalendarString, whose stringifyDate helper builds the date string by hand (padding, eraYear only for ethiopian/ethiopic, and .hour/.minute/.second when `long: true` is passed; used by src/shared/date/date-utils.js and src/shared/locked-status/use-check-lock-status.js), and date-utils.js hand-builds date strings and adds days via ISO round-trips. The app's own pre-existing jest suite (src/shared/date/get-now-in-calendar.test.js) hard-asserts exact HH:MM:SS strings, including a timezone-correction case for Africa/Kigali (UTC+2) -- this is the actual acceptance test. The correct fix bumps to 3.2.0 or later, replaces stringifyDate with formatCalendarDate(now, { includeTime: long }), moves the hand-built date strings and day arithmetic in date-utils.js to formatCalendarDate/addToCalendarDate, and deletes the app's own helpers.", + "files": ["fixtures/real-aggregate-data-entry-app/"], + "assertions": [ + "@dhis2/multi-calendar-dates is bumped to 3.2.0 or later", + "stringifyDate and other hand-built calendar date strings are replaced with formatCalendarDate, with the app's own helpers deleted", + "src/shared/date/get-now-in-calendar.test.js still passes in full, including the Africa/Kigali timezone-correction case -- not just that eslint/tsc are silent" + ] + } + ] +} diff --git a/src/skills/calendar-upgrade/package.json b/src/skills/calendar-upgrade/package.json new file mode 100644 index 0000000..5d60c02 --- /dev/null +++ b/src/skills/calendar-upgrade/package.json @@ -0,0 +1,12 @@ +{ + "name": "@dhis2/skill-calendar-upgrade", + "version": "0.0.1", + "description": "AI skill for upgrading DHIS2 apps to @dhis2/multi-calendar-dates 3.x and @js-temporal/polyfill 0.5.1", + "license": "BSD-3-Clause", + "publishConfig": { + "access": "public" + }, + "exports": { + ".": "./SKILL.md" + } +} diff --git a/src/skills/calendar-upgrade/references/bundle-temporal-check.md b/src/skills/calendar-upgrade/references/bundle-temporal-check.md new file mode 100644 index 0000000..89a3c5b --- /dev/null +++ b/src/skills/calendar-upgrade/references/bundle-temporal-check.md @@ -0,0 +1,97 @@ +# Checking which Temporal copies end up in the build + +After bumping `@dhis2/multi-calendar-dates` (`references/multi-calendar-dates-bump.md`) or a direct +`@js-temporal/polyfill` (`references/temporal-polyfill-upgrade.md`), check what the **production build** contains. The only Temporal implementation in +the bundle should be the copy that `multi-calendar-dates` `3.x` vendors +(`@dhis2/multi-calendar-dates/build/es/vendor/temporal`, which already has the Ethiopic era +fix), plus `@js-temporal/polyfill@0.5.1` only if the app imports the polyfill itself. +`3.x` lists `@js-temporal/polyfill` as a dependency but doesn't import it at runtime. + +The lockfile alone doesn't answer this. It often has more copies than the bundle does: for +example, `@dhis2/analytics` still depends on `multi-calendar-dates` `1.x` with +`@js-temporal/polyfill@0.4.3`, but if the app only imports components that don't use +calendars (such as `OrgUnitDimension`), that code is tree-shaken out of the build. + +1. **List the copies in the lockfile**, and flag in the summary if there's more than one + version of either package: + + ```bash + yarn why @js-temporal/polyfill # or: pnpm why @js-temporal/polyfill + yarn why @dhis2/multi-calendar-dates + ``` + +2. **Build, then list the `multi-calendar-dates` and `@js-temporal/polyfill` copies in the + bundle**, from the source maps the platform build writes next to the bundles in + `build/app`. Run this from the app's root after the build: + + ```bash + node -e ' + const fs = require("fs") + const path = require("path") + const maps = [] + const walk = (dir) => + fs.readdirSync(dir, { withFileTypes: true }).forEach((e) => { + const p = path.join(dir, e.name) + if (e.isDirectory()) walk(p) + else if (p.endsWith(".js.map")) maps.push(p) + }) + walk("build/app") + const copies = {} + for (const map of maps) { + for (const source of JSON.parse(fs.readFileSync(map, "utf8")).sources) { + const match = source.match( + /node_modules\/(?:.*\/)?(?:@js-temporal\/polyfill|@dhis2\/multi-calendar-dates)(?=\/)/ + ) + if (match && !copies[match[0]]) { + const { version } = require(path.resolve(match[0], "package.json")) + copies[match[0]] = version + } + } + } + console.log(maps.length ? copies : "no source maps found in build/app") + ' + ``` + + **Expected:** only `multi-calendar-dates` `3.x` entries (its Temporal is the vendored + copy, which these source maps show under `build/es/vendor/temporal`), and no + `@js-temporal/polyfill` entry, unless the app imports the polyfill itself, in + which case it must be `0.5.1`. **Flag anything else.** Look at versions, not paths: an old + copy can be hoisted to the top-level `node_modules/@js-temporal/polyfill` even though only + a nested package uses it. For example, maps-app (which uses analytics' period selectors) + bundles: + + ```text + { 'node_modules/@dhis2/multi-calendar-dates': '3.2.0', + 'node_modules/@js-temporal/polyfill': '0.4.3', + 'node_modules/@dhis2/analytics/node_modules/@dhis2/multi-calendar-dates': '1.3.2' } + ``` + + Here analytics' period selectors run on `multi-calendar-dates` `1.3.2` with the `0.4.3` + polyfill, which has no Ethiopic era fix (and adds to the bundle's size). climate-app, + which only imports `OrgUnitDimension` from analytics, bundles just + `{ 'node_modules/@dhis2/multi-calendar-dates': '3.2.0' }`, although its lockfile has the + same extra copies. + +3. **If there are extra copies, try to get the bundle down to `0.5.1` only:** + - First bump whatever pulls the old copy in, using `yarn why`/`pnpm why`: `@dhis2/ui` + `10.19.3`+ (older versions bring `multi-calendar-dates` `2.x`), the app shell (the + `@dhis2/cli-app-scripts`/app-platform release that has `@dhis2/ui` `10.19.3`+), or + `@dhis2/analytics` once it's on `multi-calendar-dates` `3.x`. + - If that's not possible, force the remaining copies to `0.5.1` with `resolutions` (yarn 1) + or `pnpm.overrides` (pnpm): `"@js-temporal/polyfill": "0.5.1"`. Those packages weren't + released against `0.5.1`, so before keeping the override, snapshot-test the app code that + goes through them (the same approach as the other two references, with `gregory`, `ethiopian` and `nepali`). + This is the only case where a `resolutions` pin is right here: `references/temporal-polyfill-upgrade.md` declares `0.5.1` as + a direct dependency instead, because there the app imports it itself. + - **If Ethiopic matters for the app** (it's used on Ethiopian-calendar instances, and the + code that goes through the forced copy handles server-calendar dates, such as analytics' + period selectors), also apply the `0.5.1` Ethiopic era patch from `references/temporal-polyfill-upgrade.md`. Unpatched, + `0.5.1` still throws `Era am … was not matched by any era` on CLDR 48 runtimes. Say in the + summary that the patch was applied, and offer to revert it, as there. + - If neither works (e.g. the old copy is pinned exactly and overriding it breaks the + snapshot tests), leave it and report it in the summary and the PR description, naming + which package brings it in. + +4. **Re-run the build and the source-map check**, and put the result in the PR description: + which copies the lockfile has, which ones the bundle has, and why any extra one is still + there. diff --git a/src/skills/calendar-upgrade/references/multi-calendar-dates-bump.md b/src/skills/calendar-upgrade/references/multi-calendar-dates-bump.md new file mode 100644 index 0000000..e8786f4 --- /dev/null +++ b/src/skills/calendar-upgrade/references/multi-calendar-dates-bump.md @@ -0,0 +1,348 @@ +# Bumping `@dhis2/multi-calendar-dates` to `3.2.0`+ + +Check for `@dhis2/multi-calendar-dates` in `package.json`. Its `3.x` line is now `latest`. +Target **`3.2.0` or later**, which has the date helpers this step migrates the app onto (see +"Replace hand-rolled calendar date code with the library's helpers" below). Confirm the +current version: + +```bash +npm view @dhis2/multi-calendar-dates dist-tags +``` + +This is a real breaking change, not a routine bump, regardless of which dist-tag it ships +under — [`dhis2/multi-calendar-dates#102`](https://github.com/dhis2/multi-calendar-dates/pull/102) +removed Temporal types from the public API entirely. Specifically, **`getNowInCalendar` now +returns a plain `{ year, month, day, eraYear?, hour, minute, second }` object instead of a +`Temporal.ZonedDateTime`**, with the time of day in the `timezone` it's given. +`convertFromIso8601`/`convertToIso8601` return the same plain `{ year, month, day, eraYear? }` +shape. Grep for `getNowInCalendar` (the app may wrap it, like +`aggregate-data-entry-app`'s `getNowInCalendarString` does) and check every call site for: + +- **Calling a Temporal method on the result** (`.add()`, `.withCalendar()`, `.startOfDay()`, + etc.) — these don't exist on the plain object anymore, and the call throws. Don't rebuild + the arithmetic by hand: use `addToCalendarDate` (see the helpers section below). +- **Building a date-time string by hand** from `.hour`/`.minute`/`.second` — use + `formatCalendarDate(now, { includeTime: true })` instead (see the helpers section below). +- **Reading `.year` for an Ethiopic date** — `year` is the Amete Alem year (e.g. `7516`); + the year DHIS2 shows is `eraYear` (`2016`). Islamic has no `eraYear`. Always read + `eraYear ?? year` (`formatCalendarDate` does this for you), never + `calendar === 'ethiopian' ? eraYear : year`, which misses the `ethiopic` id. +- Plain reads of `.month`/`.day`/`.eraYear` need no changes — that part of the shape is + unchanged. + +## Write a before/after regression test for direct library usage + +Don't just eyeball the call sites above — this is a real behavioral change that can silently +produce wrong output (not a compile error, not a thrown exception), so pin down the exact +current output with a test, confirm it passes against the version installed _right now_ +(before bumping), then bump and re-run the identical test unmodified. If it still passes, +behavior is provably unchanged; if it fails, you've caught a real regression before it ships, +not after. + +**Test every direct import from `@dhis2/multi-calendar-dates` itself, including +`useDatePicker` — the exclusion is `@dhis2/ui`'s own bundled `Calendar`/`DatePicker` +components, not this library's own hook.** It's easy to conflate the two, but they're not +the same thing: + +- **In scope** — anything the app imports directly from `@dhis2/multi-calendar-dates`: + `getNowInCalendar`, `convertFromIso8601`, `convertToIso8601`, the period-calculation + helpers (`getFixedPeriodByDate`, `getAdjacentFixedPeriods`, `generateFixedPeriods`, + `createFixedPeriodFromPeriodId`), `useDatePicker`, or a wrapper the app built on any of + these. **`#102` touched the period-calculation internals extensively** (daily/monthly/ + weekly/yearly period generation, adjacent-period lookup, period-from-id construction) and + rewrote `useDatePicker` to use the new plain-date shape — these are not lower-risk than + `getNowInCalendar` just because they weren't named in the PR's headline breaking-change + note. +- **Out of scope** — `@dhis2/ui`'s `Calendar`/`DatePicker` React components. Those bundle + their _own_ internal `multi-calendar-dates` dependency (a separate resolved version this + app doesn't control — see the Troubleshooting entry on this in `SKILL.md`), have their own test coverage + upstream, and testing them here wouldn't exercise the version this app is actually bumping. + +Cover at least these three calendars for **every** function/hook under test above — they're +the most behaviorally distinct: `gregory` (the default, CLDR-backed), `ethiopian` +(CLDR-backed but uses `eraYear` instead of `year`), and `nepali` (the one calendar this +library implements itself rather than delegating to CLDR — per `#102`'s own description, +this is the part that got rewritten most substantially, so it's the highest-risk case for a +subtle regression, and it's exactly the kind of calendar a period-calculation helper or +`useDatePicker` could be driving just as easily as `getNowInCalendar`). + +### If `ethiopian` fails before you've bumped anything (Node 24 / CLDR 48) + +On newer Node (anything after 20.19 — e.g. Node 24, which ships CLDR 48), the `ethiopian` +calendar can throw `RangeError: Era am (ISO year 2016) was not matched by any era` against +the **currently-installed** `2.x` — so the baseline can't be captured at all. This is an +unrelated upstream bug, not something the bump caused: CLDR 48 changed the Ethiopic era codes +`Intl` reports (to `am`/`aa`), and the `@js-temporal/polyfill@0.4.3` that `2.x` depends on +doesn't recognise them +([`js-temporal/temporal-polyfill#357`](https://github.com/js-temporal/temporal-polyfill/pull/357)). +**`3.x` doesn't have this problem** — it vendors the polyfill's calendar code with the era fix +applied — so this only blocks the _before_ run. Confirm the cause first: + +```bash +# resolves the polyfill copy multi-calendar-dates actually uses (it isn't a direct app dependency) +node -p "const {Temporal}=require(require.resolve('@js-temporal/polyfill',{paths:[require.resolve('@dhis2/multi-calendar-dates')]})); Temporal.PlainDate.from('2024-06-15').withCalendar('ethiopic').eraYear" +``` + +To capture the baseline, temporarily patch `@js-temporal/polyfill@0.4.3` (confirm the version +with `pnpm why`/`yarn why @js-temporal/polyfill`). **This patch is a local baseline aid only. +Never commit it, and revert it once the baseline is captured.** It doesn't fix `2.x`, and +nothing here should. Edit **both** `dist/index.cjs` (what jest loads) and `dist/index.esm.js` +(what bundlers load). #357's diff doesn't apply to `0.4.x` as-is (eras here are named +`era0`/`era1`, with no alias list), so add a `reviseIntlEra` to `EthiopicHelper` that maps the +new codes onto them, right after its constructor. The built code is minified, so search for +this exact string: + +```diff + super("ethiopic",[{name:"era0",isoEpoch:{year:-5492,month:7,day:17}},{name:"era1",isoEpoch:{year:8,month:8,day:27},anchorEpoch:{year:5501}}])} ++reviseIntlEra(e){let{era:t,eraYear:r}=e;return"am"===t&&(t="era1"),"aa"===t&&(t="era0"),{era:t,eraYear:r}} +``` + +- **yarn 1:** edit the files in `node_modules/@js-temporal/polyfill/dist/` directly. No + `patch-package` is needed, and the next `yarn install` undoes it. +- **pnpm:** don't edit `node_modules` in place. Its files are hard-linked to the global + store, so the edit would leak into every other project. Use `pnpm patch +@js-temporal/polyfill@0.4.3`, edit the files in the directory it prints, then + `pnpm patch-commit `. To revert, delete the `patches/` file and the + `patchedDependencies` entry, then reinstall. + +Re-run the one-liner above. It should print `2016`. Capture the baseline snapshots with the +patch in place, then **revert the patch before committing** the tests and snapshots. With the +patch gone, the `ethiopian` cases fail on `2.x` until the bump lands. That's expected, since +the next commit is the bump. `3.x` vendors the fix, so it needs no patch. +**Exception:** if the app also imports `@js-temporal/polyfill` itself, it needs a lasting +`0.5.1` patch after the upgrade. See `references/temporal-polyfill-upgrade.md`. + +**If the app already has a test runner** (jest/vitest — check for an existing +`*.test.ts`/`*.test.js` alongside the file that calls these functions, or a +`jest.config.js`/`vitest.config.ts`), add real, committed **snapshot tests**, following +whatever conventions already exist there. Use `toMatchSnapshot()` rather than hand-writing +expected values — the snapshot taken against the currently-installed version _is_ the +baseline, so there's no guessing or copy-pasting of "before" values, and it captures the full +output shape (every field of a period object, every cell of a date-picker grid) rather than +only the fields you thought to assert on. Pin "now" for date-dependent functions like +`getNowInCalendar` (e.g. `jest.useFakeTimers()` + `jest.setSystemTime()`), otherwise the +snapshot changes daily: + +```javascript +// Testing a wrapper around getNowInCalendar directly -- not through a UI component +beforeAll(() => { + jest.useFakeTimers() + jest.setSystemTime(new Date('2024-06-15T10:30:00Z')) +}) +afterAll(() => jest.useRealTimers()) + +describe.each(['gregory', 'ethiopian', 'nepali'])('%s calendar', (calendar) => { + it('returns the expected date', () => { + expect(getNowInCalendarString({ calendar })).toMatchSnapshot() + }) + + it('finds the expected adjacent period', () => { + const period = getFixedPeriodByDate({ + periodType: 'Monthly', + date: '2024-06-15', + calendar, + }) + const [next] = getAdjacentFixedPeriods({ period, calendar, steps: 1 }) + expect(next).toMatchSnapshot() + }) +}) +``` + +If the app uses `useDatePicker` directly (check for `import { useDatePicker } from +'@dhis2/multi-calendar-dates'`), test it the same way via `@testing-library/react`'s +`renderHook` — it's a headless hook, no `@dhis2/ui` component needed to exercise it: + +```javascript +import { renderHook } from '@testing-library/react' +import { useDatePicker } from '@dhis2/multi-calendar-dates' + +describe.each(['gregory', 'ethiopian', 'nepali'])('%s calendar', (calendar) => { + it('builds the expected calendar grid', () => { + // pin the system time first, same as above -- isToday/isInCurrentMonth depend on it + const { result } = renderHook(() => + useDatePicker({ + date: /* fixed date string */, + options: { calendar }, + onDateSelect: () => {}, + }) + ) + expect(result.current.calendarWeekDays).toMatchSnapshot() + }) +}) +``` + +**Snapshot what the app actually consumes, not raw `getNowInCalendar` return values.** +`getNowInCalendar`'s return type changes by design in `3.x` (Temporal object → plain object), +so a snapshot of the raw value is guaranteed to differ and tells you nothing. Snapshot the +app's own wrapper output (like `getNowInCalendarString` above) or the fields the app reads — +that's where a real regression would show up. + +The workflow: + +1. **Before bumping**, run the tests against the currently-installed version. The first run + writes the `__snapshots__/*.snap` files — open them and sanity-check the values look + right (it's a baseline, not proof of correctness). Commit the tests and snapshots on their + own, before the bump, so the bump's diff is cleanly separate. +2. **Bump**, reinstall, and run the identical tests again, unmodified, with `--ci` + (`pnpm test --ci`, or `jest --ci`) — in CI mode jest fails on a mismatch, and on a missing + snapshot, instead of silently writing a new one. +3. **If a snapshot fails, don't update it (`-u`) to make it pass** — the diff _is_ the + finding. Investigate each changed value: either it's a real regression to fix in the app + code, or it's an expected change (confirm against `#102`/the changelog) and only then + update that one snapshot deliberately, saying why in the commit message. + +Keep the tests and snapshots in the codebase afterward — they're real regression coverage +for this library going forward, not a throwaway migration check. (Vitest supports the same +`toMatchSnapshot()` API, and fails on mismatch when run under `CI=true`.) + +**If there's no test runner set up at all**, don't introduce one just for this — that's a +bigger decision than a dependency bump warrants. Instead, do the same before/after +comparison as a temporary standalone script (plain Node, no browser needed): + +```javascript +// verify-multi-calendar-dates.mjs -- run once before bumping, once after; diff the two outputs +import { + getNowInCalendar, + convertFromIso8601, + convertToIso8601, + getFixedPeriodByDate, + getAdjacentFixedPeriods, +} from '@dhis2/multi-calendar-dates' + +const CALENDARS = ['gregory', 'ethiopian', 'nepali'] +const FIXED_ISO_DATE = '2024-06-15' // any date currently exercised by the app's real usage + +for (const calendar of CALENDARS) { + const period = getFixedPeriodByDate({ + periodType: 'Monthly', + date: FIXED_ISO_DATE, + calendar, + }) + console.log(calendar, { + now: getNowInCalendar(calendar, 'Etc/UTC'), + fromIso: convertFromIso8601(FIXED_ISO_DATE, calendar), + toIso: convertToIso8601(FIXED_ISO_DATE, calendar), + period, + adjacentPeriods: getAdjacentFixedPeriods({ + period, + calendar, + steps: 1, + }), + // add whichever other functions/period types this app actually calls directly + }) +} +``` + +Redirect the output to a file before bumping (`node verify-multi-calendar-dates.mjs > +/tmp/before.json`), bump the dependency, run it again (`> /tmp/after.json`), and `diff +/tmp/before.json /tmp/after.json`. Anything beyond `getNowInCalendar`'s return shape +(expected — already covered above) is a real behavioral difference to investigate before +shipping. Delete the script afterward — it's a verification aid for this migration, +not something to leave behind in the app's repo. + +**`useDatePicker` doesn't fit this plain-Node script** — it's a React hook, not a pure +function, so exercising it needs at least `@testing-library/react`'s `renderHook`. If the +app uses `useDatePicker` directly but has no test runner at all, that's a real gap worth +raising with the user rather than silently skipping it — a plain-object diff script can't +cover it, only a real (even minimal, temporary) React test can. + +## Bump the dependency + +Bump the dependency to whatever version `npm view ... dist-tags` showed above resolves +`latest` to (`3.2.0` as of this writing, and at least `3.2.0`) and verify: + +```json +"dependencies": { + "@dhis2/multi-calendar-dates": "^3.2.0" +} +``` + +Also check that the lockfile ends up with a single `3.x` copy: older `@dhis2/ui` versions +(before `10.19.3`) bring their own `multi-calendar-dates`. Bump `@dhis2/ui` alongside if +needed (see "First: check `@dhis2/ui`" in `SKILL.md`), and deduplicate (`npx yarn-deduplicate --packages +@dhis2/multi-calendar-dates --packages @dhis2/ui yarn.lock` on yarn 1). A nested `1.x` copy +under `@dhis2/analytics` is expected and can't be removed from the app. + +```bash +pnpm install +pnpm lint +pnpm test +``` + +Since this is a real behavioral change (not just a type change), a passing `tsc`/`eslint` +pass doesn't guarantee correctness here on its own — a wrong result (e.g. Nepali month arithmetic +done in ISO terms, or the wrong Ethiopic year) compiles and runs fine, it just silently +produces wrong output. The +before/after regression test (or standalone script) above is what actually closes that gap; +don't skip it and rely on `tsc`/`eslint`/a generic `pnpm test` pass alone as proof this is +safe. + +## Replace hand-rolled calendar date code with the library's helpers (`3.2.0`+) + +The library has three helpers that cover what DHIS2 apps have historically written for +themselves. **Every instance of the patterns below should be migrated to the library**, +and the app's own helper deleted — not kept alongside, and not re-implemented when fixing a +`3.x` breakage (e.g. a `getNowInCalendar(calendar).add(...)` call that now throws): + +| Helper | What it does | +| ------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `getDaysInMonth({ year, month }, calendar)` | Days in a month: 29 for February 2024, 5 or 6 for the Ethiopic 13th month, up to 32 for Nepali | +| `addToCalendarDate(date, { years, months, days }, calendar)` | Adds (or, with negative values, subtracts) years and months in the calendar's own months, clamps the day to the end of the month, then adds days. Returns a plain `{ year, month, day, eraYear? }` | +| `formatCalendarDate(date, { format, includeTime })` | `yyyy-MM-dd` (or `format: 'DD-MM-YYYY'`), with `includeTime: true` appending `Thh:mm:ss` from `getNowInCalendar`'s result (it throws if the date has no time) | + +All three read the year as `eraYear ?? year`, so `getNowInCalendar`/`convertFromIso8601` +results can be passed straight in. They accept DHIS2 calendar ids (`ethiopian`, `iso8601`, +`gregorian`, …) as well as Intl ids, and throw for a date that doesn't exist (e.g. month 13 in +a 12-month calendar) or a Nepali year outside 1970–2100. + +**Find the patterns to migrate** — grep `src` around every `@dhis2/multi-calendar-dates` +import, including the app's own date utils (`date.js`, `time.js`, `dates.js`, `date-utils.js` +are common names): + +```bash +grep -rnE "eraYear|padStart|padWithZero|THIRTEEN_MONTH|monthsInYear|daysInMonth|validateDateString|\.add\(" src +``` + +| Hand-rolled pattern | Replace with | +| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | +| Building `yyyy-MM-dd`/`DD-MM-YYYY` from a calendar date with `padStart`/`padWithZeros` and `eraYear ?? year` (or `calendar === 'ethiopian' ? eraYear : year`) | `formatCalendarDate(date)` / `formatCalendarDate(date, { format: 'DD-MM-YYYY' })` | +| The same, plus `T${hour}:${minute}:${second}` from `getNowInCalendar` | `formatCalendarDate(now, { includeTime: true })` | +| Padding the result of `convertToIso8601`/`convertFromIso8601` into a string | `formatCalendarDate(convertToIso8601(...))` | +| Adding/subtracting months or years with a `THIRTEEN_MONTH_CALENDARS`-style list, `year * monthsInYear + month` index maths, or a `validateDateString` loop to clamp the day | `addToCalendarDate(date, { months }, calendar)` | +| Adding days by converting to ISO, using `Date`/milliseconds, and converting back | `addToCalendarDate(date, { days }, calendar)` (keep any time part of the string as it was) | +| "One year ago" as `year - 1` with no clamping (breaks on 29 February and Ethiopic Pagume) | `addToCalendarDate(now, { years: -1 }, calendar)` | +| Days in a month via native `Date` (`new Date(y, m, 0)`), a `validateDateString` loop from 32 down, or an app copy of the Nepali month table | `getDaysInMonth({ year, month }, calendar)` | +| `getNowInCalendar(calendar).add(period)` (throws on `3.x`) | `formatCalendarDate(addToCalendarDate(getNowInCalendar(calendar), period, calendar))` | + +What the library does **not** cover, so keep the app's own code for it: months in a year (no +helper — keep a 13-month calendar list if the app needs one), the `julian` calendar (not +supported; apps that map it to `gregory` need their own 29 February rule for century years), +and formatting Temporal objects the app creates itself with a direct `@js-temporal/polyfill` +import (`references/temporal-polyfill-upgrade.md`'s scope, not this one). + +How to migrate: + +1. Do it in its **own commit after the bump**, so the bump's snapshot diff stays separate from + the refactor's. The regression snapshots from above should come through **unchanged**. Any + snapshot that does change must be explained in the commit message — typically one of: + - the `ethiopic` id now gives the era year (the old code only special-cased + `ethiopian`) — a fix; + - a nonexistent date (month 13 in a 12-month calendar) now throws instead of returning a + made-up value — check that no caller can pass one; + - Nepali month arithmetic keeps the day of the month instead of drifting by 1–5 days + (it was done in ISO terms) — a fix. +2. Delete the app's helpers, constants and tests that are no longer used anywhere + (`grep -rn src` before deleting an export). Keep shared utilities such as a + `padWithZeroes` that other, unrelated code still imports. +3. If a test mocks `@dhis2/multi-calendar-dates` with `jest.mock`, forward the new functions + lazily inside the factory (`getDaysInMonth: (...args) => +jest.requireActual('@dhis2/multi-calendar-dates').getDaysInMonth(...args)`); spreading + `jest.requireActual` at the top of the factory can fail. +4. Re-enable tests the app had skipped or commented out because `2.x` threw for Ethiopic + dates, and run them — they usually pass on `3.2.0`. + +Examples from real migrations: climate-app's `getCalendarDate` (a `THIRTEEN_MONTH_CALENDARS` +list, `addMonths`, `addDays` and a local `formatCalendarDate`) became one +`formatCalendarDate(addToCalendarDate(getNowInCalendar(calendar), { years, months, days }, +calendar))`; aggregate-data-entry-app's `stringifyDate` became `formatCalendarDate(now, +{ includeTime: long })`; maps-app's `getMaxDaysInMonth` became a `getDaysInMonth` call. diff --git a/src/skills/calendar-upgrade/references/temporal-polyfill-upgrade.md b/src/skills/calendar-upgrade/references/temporal-polyfill-upgrade.md new file mode 100644 index 0000000..395954b --- /dev/null +++ b/src/skills/calendar-upgrade/references/temporal-polyfill-upgrade.md @@ -0,0 +1,139 @@ +# Upgrading direct `@js-temporal/polyfill` usage to `0.5.1` + +`@dhis2/multi-calendar-dates@3.x` and `@dhis2/ui@10.19+` both ask for +`@js-temporal/polyfill@0.5.1`. Apps that import the polyfill themselves often pin it to +`0.4.3`, either as a direct dependency or (more commonly, on yarn 1) as a `resolutions` +entry. A pinned resolution silently forces every library onto the old version too. On yarn, +it shows up as `warning Resolution field "@js-temporal/polyfill@0.4.3" is incompatible with +requested version "@js-temporal/polyfill@0.5.1"`. + +## Find every direct usage + +```bash +grep -rln "@js-temporal/polyfill" src # every file that imports it +grep -n "js-temporal" package.json # dependencies? or only resolutions? +``` + +An app that imports the polyfill but only has it in `resolutions` is relying on yarn 1 +hoisting a transitive copy. Declare it as a real dependency as part of this step. + +For each importing file, list the Temporal API it calls. `0.5` removed `Temporal.Calendar`/ +`Temporal.TimeZone` objects (calendars are strings only), `getISOFields()`, `getCalendar()`, +`Temporal.Now.plainDate(calendar)` (only the `…ISO()` variants remain), and the `calendar` +property (now `calendarId`). It also renamed `era` codes, e.g. Ethiopic `era0`/`era1` became +`ethioaa`/`ethiopic`, and some calendars now report `era: undefined`. Grep for those: + +```bash +grep -rnE "Temporal\.(Calendar|TimeZone)|getISOFields|getCalendar|Now\.plainDate\(|\.calendar\b|\.era\b" +``` + +Typical DHIS2 app usage (`PlainDate.from`/`compare`, `PlainDateTime` with `'iso8601'`, +`Now.plainDateISO().withCalendar()`, `.since({ largestUnit, smallestUnit })`, `.subtract()`, +`.with({ year })`, `.eraYear`) is unaffected. Prove it with the snapshot test below, don't +assume it. + +## Write snapshot tests for every usage _before_ upgrading + +Same approach as `references/multi-calendar-dates-bump.md`: committed `toMatchSnapshot()` tests, captured on `0.4.3`, then re-run +unmodified with `--ci` on `0.5.1`. Cover **every** function that calls the polyfill, through +the app's own exported helpers or components, not raw polyfill calls. Run each one against +**`gregory`, `ethiopian` (Temporal `ethiopic`) and `nepali`**. These three are the ones that +matter, so don't spend effort on the others. + +- **Pin "now"** (`jest.useFakeTimers()` + `jest.setSystemTime(...)`) for anything calling + `Temporal.Now.*`, such as an age field computing years/months/days from today. The + polyfill reads `Date.now()`, so fake timers control it. +- **Nepali isn't a Temporal calendar.** Apps usually guard it with an `isCalendarSupported` + check and fall back to ISO arithmetic via `multi-calendar-dates`' `convertToIso8601`/ + `convertFromIso8601`. Test that fallback path explicitly. +- **Ethiopic**: Temporal's `year` here is counted from the Amete Alem era (e.g. `7519`), and + `eraYear` is the Amete Mihret year DHIS2 users see (e.g. `2019`). Apps work around this + with `date.with({ year: date.eraYear })`. Both versions keep the same semantics, but + snapshot the result anyway. +- If a helper/function is module-private (e.g. a `getCalculatedValues` inside a component + file), export it for testing rather than snapshotting through rendered UI. + +### The Ethiopic era patch (applied by default, revertable) + +On newer Node / browsers with CLDR 48 (Node 24, and Node builds using system ICU 78), `Intl` +reports Ethiopic eras as `am`/`aa`. Neither `0.4.3` nor `0.5.1` recognises those codes, so +**any** Ethiopic date throws `RangeError: Era am (ISO year …) was not matched by any era` +([`js-temporal/temporal-polyfill#357`](https://github.com/js-temporal/temporal-polyfill/pull/357)). +Unlike the `multi-calendar-dates` bump, the app calls the polyfill directly here, so this isn't just a baseline problem: +it breaks the app in production for Ethiopian users. The upstream fix is tracked in +[LIBS-830](https://dhis2.atlassian.net/browse/LIBS-830). + +There are two patches here, with different lifetimes: + +- **`0.4.3`: temporary, never committed.** It exists only so the `ethiopian` snapshot cases + can run before the upgrade. Apply and revert it as in `references/multi-calendar-dates-bump.md`. +- **`0.5.1`: kept, in its own commit.** This is the actual fix for the app's own Ethiopic + dates after the upgrade. It's only needed because the app imports the polyfill itself. + Apps that only go through `multi-calendar-dates` 3.x need no patch. + +**Apply the `0.5.1` patch by default, then offer to revert it in the summary.** The patch is +big. The polyfill ships its `dist` files minified onto a single line, so +`patch-package`/`pnpm patch` store the whole line, not just the change. The patch file is +**~516 KB**, even though the actual edit is a few characters. Some teams won't want that in +their repo, so: + +- **Default: patch.** Keep the `0.5.1` patch and the `ethiopian` snapshot cases in their own + commit, separate from the upgrade, so they can be dropped cleanly. Remove the patch once + LIBS-830 lands upstream. +- **In the final summary, always offer the revert**, stating the patch size and linking + LIBS-830. For example: "The Ethiopic patch adds a ~516 KB file to `patches/` because the + polyfill's dist is minified onto one line. If you'd rather not carry it, I can drop that + commit and wait for the upstream fix (LIBS-830)." +- **If the user chooses to revert:** drop that commit (or move it to its own branch if they + want to keep it around), and leave `ethiopian` out of the polyfill snapshot tests, since it + throws on CLDR 48 runtimes either way. Then record the known issue in the PR description. + Explain that both `0.4.3` and `0.5.1` are affected, link #357 and LIBS-830, and say which + app code calls the polyfill directly for Ethiopic dates (e.g. an age field). + `multi-calendar-dates@3.x` bundles its own copy of the fix, so conversions through it are + unaffected and its own Ethiopian tests still pass. + +Patch **both** `dist/index.cjs` (jest) and `dist/index.esm.js` (bundlers). The built code is +minified, so search for the exact strings below. + +**`0.4.3`** (temporary, to capture the baseline only; see `references/multi-calendar-dates-bump.md` for applying and reverting +it): eras are named `era0`/`era1` with no alias list, so add a `reviseIntlEra` right after +`EthiopicHelper`'s constructor: + +```diff + super("ethiopic",[{name:"era0",isoEpoch:{year:-5492,month:7,day:17}},{name:"era1",isoEpoch:{year:8,month:8,day:27},anchorEpoch:{year:5501}}])} ++reviseIntlEra(e){let{era:t,eraYear:r}=e;return"am"===t&&(t="era1"),"aa"===t&&(t="era0"),{era:t,eraYear:r}} +``` + +**`0.5.1`** (keep this one after the upgrade, until LIBS-830 is fixed upstream): eras have `code` + `names` alias lists, so +#357's fix applies directly. Add `"aa"` and `"am"` as aliases: + +```diff +-super("ethiopic",[{code:"ethioaa",names:["ethiopic-amete-alem","mundi"],isoEpoch:{year:-5492,month:7,day:17}},{code:"ethiopic",names:["incar"],isoEpoch:{year:8,month:8,day:27},anchorEpoch:{year:5501}}])} ++super("ethiopic",[{code:"ethioaa",names:["ethiopic-amete-alem","mundi","aa"],isoEpoch:{year:-5492,month:7,day:17}},{code:"ethiopic",names:["incar","am"],isoEpoch:{year:8,month:8,day:27},anchorEpoch:{year:5501}}])} +``` + +Apply the `0.5.1` patch with `pnpm patch @js-temporal/polyfill@0.5.1` → edit → +`pnpm patch-commit ` on pnpm. On yarn 1, edit `node_modules/@js-temporal/polyfill/dist/*` +and run `npx patch-package @js-temporal/polyfill`, which needs `patch-package` in +`postinstall`. Verify with the following, which should print `2016` once patched (on either +version): + +```bash +node -p "require('@js-temporal/polyfill').Temporal.PlainDate.from('2024-06-15').withCalendar('ethiopic').eraYear" +``` + +## The workflow + +1. Temporarily apply the `0.4.3` patch so the `ethiopian` cases can run, write the snapshot + tests, and run them to create the snapshots. Sanity-check the `.snap` values + (Ethiopic/Nepali dates should look like real local dates). Revert the patch, then commit + the tests and snapshots on their own. **Never commit the `0.4.3` patch.** +2. Upgrade: remove the `0.4.3` `resolutions`/`pnpm.overrides` pin, add + `"@js-temporal/polyfill": "0.5.1"` to `dependencies`, reinstall, and confirm a single copy + resolves (`yarn why`/`pnpm why @js-temporal/polyfill`). Commit the upgrade without any + patch. +3. Add the `0.5.1` patch in its own commit. +4. Re-run the identical tests with `--ci`, plus `tsc` and lint. Don't `-u` a failing + snapshot. The diff is the finding (see `references/multi-calendar-dates-bump.md`). +5. In the summary, offer to revert the patch commit, stating its size and linking LIBS-830 + (see above). diff --git a/src/skills/modernise-apps/SKILL.md b/src/skills/modernise-apps/SKILL.md index 21df418..46c65a0 100644 --- a/src/skills/modernise-apps/SKILL.md +++ b/src/skills/modernise-apps/SKILL.md @@ -19,7 +19,7 @@ description: > d2-app-scripts publish, semantic-release, or the App Hub API key/app ID. This skill is for modernising an already-existing app — for scaffolding a brand new app, use the dhis2-apps skill instead, and for upgrading @dhis2/multi-calendar-dates or - @js-temporal/polyfill, use the multi-calendar-dates-upgrade skill. + @js-temporal/polyfill, use the calendar-upgrade skill. --- # Modernising DHIS2 Apps @@ -43,7 +43,7 @@ covers five independent tasks — an app can need any combination of them, or no (non-breaking by default, with an explicit choice offered if `latest` would cross a major version for any of them). Upgrading `@dhis2/multi-calendar-dates` and `@js-temporal/polyfill` is a separate, behavioural migration with its own skill, - `multi-calendar-dates-upgrade` — point the user to it if the app uses either. + `calendar-upgrade` — point the user to it if the app uses either. 4. **Modernising the CI pipeline** — replacing bespoke GitHub Actions workflows with the shared `dhis2/workflows-platform` reusable workflows wherever one exists, and bumping any outdated action version or Node version left in whatever stays custom. @@ -56,17 +56,17 @@ covers five independent tasks — an app can need any combination of them, or no ## First: confirm this is an existing DHIS2 app, and which task(s) apply -| Check | How | Result | -| ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | -| `d2.config.js` exists and `@dhis2/cli-app-scripts` is in `package.json` | Glob + read `package.json` | **DHIS2 app** — continue | -| Neither found | — | **Not a DHIS2 app** — this skill doesn't apply; see `dhis2-apps` if the user wants to scaffold one | -| `yarn.lock` exists and `pnpm-lock.yaml` does not | `ls` | **Not yet migrated to pnpm** — proceed with `references/pnpm-migration.md` if that's what's needed | -| `pnpm-lock.yaml` already exists | `ls` | **Already migrated to pnpm** — nothing to do for that task | -| `@dhis2/cli-style` is in `package.json` | Read `package.json` | **Still on `d2-style`** — proceed with `references/style-configs-migration.md` if that's needed | -| `@dhis2/ui`/`app-runtime`/`d2-i18n` is present and behind `latest` | Read `package.json` + `npm view dist-tags` | **Platform library behind** — proceed with `references/platform-library-bumps.md` if that's needed | -| `@dhis2/multi-calendar-dates` is below `3.2.0`, or `src` imports `@js-temporal/polyfill` pinned below `0.5.1` | Read `package.json` + `grep -rln "@js-temporal/polyfill" src` | **Calendar libraries behind** — out of scope here; suggest the `multi-calendar-dates-upgrade` skill | -| `.github/workflows/*.yml` has bespoke jobs, not just `uses: dhis2/workflows-platform/...` | Read `.github/workflows/*.yml` | **CI not fully modernised** — proceed with `references/ci-migration.md` if that's needed | -| No workflow calls `.../release.yml` with App Hub publish on, and `d2.config.js`'s `type` isn't `'lib'` | Read `.github/workflows/*.yml` + `d2.config.js` | **No App Hub CD** — offer `references/apphub-release.md`; don't set it up unprompted (see Rules) | +| Check | How | Result | +| ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | +| `d2.config.js` exists and `@dhis2/cli-app-scripts` is in `package.json` | Glob + read `package.json` | **DHIS2 app** — continue | +| Neither found | — | **Not a DHIS2 app** — this skill doesn't apply; see `dhis2-apps` if the user wants to scaffold one | +| `yarn.lock` exists and `pnpm-lock.yaml` does not | `ls` | **Not yet migrated to pnpm** — proceed with `references/pnpm-migration.md` if that's what's needed | +| `pnpm-lock.yaml` already exists | `ls` | **Already migrated to pnpm** — nothing to do for that task | +| `@dhis2/cli-style` is in `package.json` | Read `package.json` | **Still on `d2-style`** — proceed with `references/style-configs-migration.md` if that's needed | +| `@dhis2/ui`/`app-runtime`/`d2-i18n` is present and behind `latest` | Read `package.json` + `npm view dist-tags` | **Platform library behind** — proceed with `references/platform-library-bumps.md` if that's needed | +| `@dhis2/multi-calendar-dates` is below `3.2.0`, or `src` imports `@js-temporal/polyfill` pinned below `0.5.1` | Read `package.json` + `grep -rln "@js-temporal/polyfill" src` | **Calendar libraries behind** — out of scope here; suggest the `calendar-upgrade` skill | +| `.github/workflows/*.yml` has bespoke jobs, not just `uses: dhis2/workflows-platform/...` | Read `.github/workflows/*.yml` | **CI not fully modernised** — proceed with `references/ci-migration.md` if that's needed | +| No workflow calls `.../release.yml` with App Hub publish on, and `d2.config.js`'s `type` isn't `'lib'` | Read `.github/workflows/*.yml` + `d2.config.js` | **No App Hub CD** — offer `references/apphub-release.md`; don't set it up unprompted (see Rules) | ## What does the user need? diff --git a/src/skills/modernise-apps/references/platform-library-bumps.md b/src/skills/modernise-apps/references/platform-library-bumps.md index 9a7e990..c3c48e4 100644 --- a/src/skills/modernise-apps/references/platform-library-bumps.md +++ b/src/skills/modernise-apps/references/platform-library-bumps.md @@ -61,7 +61,7 @@ revert to avoid the work unless the user explicitly wants to defer it. Not part of this skill. If the app depends on `@dhis2/multi-calendar-dates` or imports `@js-temporal/polyfill` directly, upgrading them is a separate, behavioural migration (`multi-calendar-dates` `3.x` is a real breaking change) — use the -`multi-calendar-dates-upgrade` skill for it. It needs `@dhis2/ui` `10.19.3`+ to end up with a +`calendar-upgrade` skill for it. It needs `@dhis2/ui` `10.19.3`+ to end up with a single `multi-calendar-dates` `3.x` and `@js-temporal/polyfill@0.5.1`, so if this step bumped `@dhis2/ui` past that, mention in the summary that the calendar upgrade is now unblocked.