|
| 1 | +--- |
| 2 | +"@aws-blocks/blocks": minor |
| 3 | +"@aws-blocks/create-blocks-app": patch |
| 4 | +--- |
| 5 | + |
| 6 | +feat(blocks): add `cdk-blocks migrate` |
| 7 | + |
| 8 | +`npx --no -- cdk-blocks migrate` moves an app that create-blocks-app scaffolded with `aws-blocks/scripts/*.ts` wrappers to the `cdk-blocks` CLI. It rewrites each `package.json` script whose value is exactly a generated command (for example `"deploy": "tsx aws-blocks/scripts/deploy.ts"` becomes `"deploy": "cdk-blocks deploy"`), moves the port and frontend settings of a generated `server.ts` to `package.json#blocks.dev` (`"frontend": false` for a backend-only one), and deletes the generated wrappers that nothing uses any more. A wrapper that a script or any app file other than Markdown docs still uses is kept: it reads TypeScript and JavaScript sources (including nested and parent-relative imports such as `import '../server.js'`), shell scripts, Dockerfiles, and dot directories such as `.github` and `.vscode`. In a git repository the app files are the tracked files and the untracked files that are not gitignored, in any directory, so gitignored build output and logs do not count, plus the files of a submodule or an untracked git repository inside the app; outside git, or where git does not track the app (a parent repository ignores its directory), they are the files under the app directory other than `node_modules`, `.git`, build output (`build-temp` included) and tool caches. Markdown docs and editor rules (`*.md`, `*.mdx`, `*.mdc`), binary files, files over 1 MiB and symbolic links never count. It does not read outside the app directory, so check the scripts of a parent workspace yourself. It recognises every wrapper version that create-blocks-app has shipped, ignoring whitespace. A wrapper you changed is never deleted, only reported. If you changed `server.ts`, the `sandbox` scripts are kept too, because `sandbox.ts` starts that server, and a `sandbox.ts` that stays keeps its `server.ts`; in the Amplify overlay, `blocks:stop` and `stop.ts` are kept as well, because that `stop.ts` stops the server's own port. If `.blocks/config.json` is missing or has no `stackId`, as in apps scaffolded before create-blocks-app wrote it, the `deploy` and `destroy` scripts and wrappers are kept, because `cdk-blocks deploy` and `cdk-blocks destroy` need it; the report says how to add it. Every other script is left alone. |
| 9 | + |
| 10 | +Run `npx --no -- cdk-blocks migrate --dry-run` first to see the plan. Unless you pass `--force`, it refuses an app outside git (or where git is not installed), a git working tree with uncommitted changes, an app that git does not track (so git could not undo the changes), and a repository that git fails to read, such as one with "dubious ownership", quoting git's error. `--output json` prints `{changed, removed, kept, scriptsRewritten, devConfig}`, where `devConfig` is the `blocks.dev` config that migrate adds to `package.json` (or, with `--dry-run`, would add), or `null`. It exits 0 when the app is fully migrated and 1 when kept items remain for you to finish by hand; exit 1 is a report, not a crash, and running it again is safe. |
| 11 | + |
| 12 | +It recognises the wrappers of every public create-blocks-app release up to 0.4.0, including the `sandbox.ts` that runs `npm run sandbox:preview`, whose `"sandbox:preview": "tsx aws-blocks/scripts/sandbox.ts --preview"` becomes `"sandbox:preview": "cdk-blocks sandbox preview"`. |
| 13 | + |
| 14 | +Fixes from CLI testing: |
| 15 | + |
| 16 | +- Two `cdk-blocks migrate` runs at the same time no longer crash with `ENOENT` when one deletes a wrapper that the other is reading or planned to delete. |
| 17 | +- `cdk-blocks migrate --dry-run` no longer says "everything else was migrated" above the items it would keep. |
| 18 | +- The hint for an app without `stackId` now says that `<stackId>-prod` must equal the production stack name in `aws-blocks/index.cdk.ts`: an app that hard-codes `my-blocks-stack-prod` uses `{"stackId": "my-blocks-stack"}`. |
| 19 | +- A wrapper that matches no known version is reported as an "unrecognised version (only public create-blocks-app releases are recognised)", not as "customized": it may be an unmodified file from a release that migrate does not know. The README and `cdk-blocks migrate -h` say that only the public releases are recognised. |
| 20 | +- A generated script that still runs a kept, unrecognised wrapper (for example `dev` and `dev:server` with a changed `server.ts`) is now listed in `kept` with the reason, so `--output json` names every script that still uses `aws-blocks/scripts`. |
| 21 | +- `cdk-blocks migrate` writes `blocks.dev.frontendCommand` only when it differs from the command `cdk-blocks dev` detects from the `vite` or `next` dependency; the ports still move. A migrated default app gets `{"frontendPort": 3100}`, as new apps do, so `cdk-blocks dev --frontend-port` keeps working. |
| 22 | +- With `--output json`, a refusal (uncommitted changes, an app git does not track, a git failure) prints `{"error": {"message", "hint"}}` on stdout, so a script can tell it from kept items, which also exit 1. `cdk-blocks migrate -h` and the README list the exit codes: 0 fully migrated, 1 kept items remain or refused, 2 usage error. |
| 23 | +- `cdk-blocks migrate` keeps a wrapper that a CI workflow, shell script, Dockerfile or script runs from another working directory: a bare `scripts/deploy.ts` or `deploy.ts` there now counts as a use (`working-directory: aws-blocks` with `npx tsx scripts/deploy.ts`, `cd aws-blocks/scripts && npx tsx typegen.ts`). In a monorepo it also reads every `package.json` and every CI, shell, Make and Docker file that git tracks outside the app, at any depth (`.github/workflows/*`, `scripts/deploy-web.sh`, `ci/Makefile`, `tools/release/package.json`; not under `node_modules`, nor over 1 MiB), and, when the app is in a git submodule, those files of the superproject, so it no longer deletes a wrapper that a root workflow, a monorepo script or a sibling workspace still runs. |
| 24 | +- `cdk-blocks migrate --query <path>` checks the path against the result before anything runs, as the other commands do: `kept.0.reason`, `scriptsRewritten.<script>.to` and `devConfig.frontendPort` are accepted, and a typo exits 2 with the fields it could be. |
| 25 | +- `cdk-blocks migrate --query <path>` prints a refusal only as the error on stderr, with nothing on stdout, also under `--output json`: `--query` prints one field of the result, and a refusal has no result. The `{"error"}` document is for `--output json` without `--query`. |
| 26 | +- An app `package.json` that is not valid JSON stops `cdk-blocks migrate` with `cannot read <app>/package.json: <the parse error>`, as the other commands say, instead of only `Unexpected end of JSON input`. |
| 27 | +- In a monorepo, a file above the app counts as a use of a wrapper only when it names this app: `apps/web/aws-blocks/scripts/deploy.ts` (relative to the repository top or to the file), or a bare `aws-blocks/scripts/deploy.ts`, `scripts/deploy.ts` or `deploy.ts` in a file that changes into the app (`working-directory: apps/web`, `cd apps/web`). Before, `apps/api`'s workflow, with `working-directory: apps/api` or `apps/api/aws-blocks/scripts/typegen.ts`, kept `apps/web`'s wrappers and made its migration exit 1. |
| 28 | +- An app `package.json` that is valid JSON but not an object (`null`, an array, a string) stops `cdk-blocks migrate` with `<app>/package.json must be a JSON object, not null.` and changes nothing, instead of `Cannot read properties of null (reading 'scripts')`, or `Nothing to migrate` for an array or a string. |
| 29 | +- A path written with backslashes counts as a use of a wrapper: `npx tsx aws-blocks\scripts\deploy.ts` in a `.cmd`, `.bat` or `.ps1` file (or a `package.json` script) keeps `deploy.ts`, which `cdk-blocks migrate` deleted before. |
| 30 | +- On Windows, two `cdk-blocks migrate` runs at the same time no longer crash with `EPERM: operation not permitted, rmdir '…\aws-blocks\scripts'`: a directory that the other run holds or is deleting is tried again, then left to it. |
| 31 | +- Outside git, or where git is not installed, `cdk-blocks migrate` now refuses unless you pass `--force`, as it does for an app that git does not track: git could not undo the rewritten scripts or the deleted wrappers. The hint points to `--dry-run` first, and `--output json` prints the `{"error"}` document. |
| 32 | +- In a monorepo, a quoted `cd` or `working-directory` into the app now counts when it holds an escaped quote (`cd "apps/we\"b"`) or a command substitution: `cd "$(git rev-parse --show-toplevel)/apps/web" && npx tsx aws-blocks/scripts/deploy.ts`, or `cd "$(dirname "$0")/../apps/web"` from the script's own directory. Before, `cdk-blocks migrate` deleted `deploy.ts` there and exited 0 while the root script still ran it. |
| 33 | +- A `.blocks/config.json` that is not valid JSON is no longer reported as missing: the kept `deploy` and `destroy` items say `.blocks/config.json is not valid JSON (<the parse error>): fix <path>, then run cdk-blocks migrate again`, as core's deploy and `cdk-blocks whoami` do, instead of telling you to create the file. |
| 34 | +- A `package.json` edit made while `cdk-blocks migrate` scanned the app is no longer overwritten: migrate now stops with `package.json changed while cdk-blocks migrate ran; nothing was written.` before it writes or deletes anything, also with `--force`, and `--output json` prints the `{"error"}` document. Before, it wrote the scripts it had planned over the edit, and could delete a wrapper that a newly added script ran. |
| 35 | +- A wrapper that a field of the app's `package.json` other than `scripts` runs is no longer deleted: a `lint-staged`, `simple-git-hooks` or `nodemonConfig` block with `tsx aws-blocks/scripts/typegen.ts` (or a bare `scripts/deploy.ts` after `cd aws-blocks`) keeps that wrapper, reported as `generated, but still used by package.json#lint-staged`. Before, `cdk-blocks migrate` read only the `scripts` of the app's own `package.json`, deleted the wrapper and exited 0. |
0 commit comments