Skip to content

Commit 0f472ae

Browse files
committed
feat(tailwindcss-patch): expose restore report metadata in json
1 parent 275c02d commit 0f472ae

7 files changed

Lines changed: 48 additions & 1 deletion

File tree

.changeset/tender-houses-fly.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"tailwindcss-patch": patch
3+
---
4+
5+
Improve restore JSON observability for migration reports.
6+
7+
- expose `reportKind` and `reportSchemaVersion` in `tw-patch restore --json` output when report metadata is present
8+
- keep compatibility with legacy reports that do not contain schema metadata
9+
- add unit tests for metadata and legacy restore report behavior

packages/tailwindcss-patch/MIGRATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Migration mapping:
9191
- `tw-patch restore` can restore config files from a migration report (`backupFile` entries) with optional `--dry-run`, `--strict`, and `--json`.
9292
- Migration reports now include envelope metadata: `reportKind`, `schemaVersion`, `generatedAt`, and `tool` (`name` / `version`).
9393
- `tw-patch restore` validates report metadata when present, rejects unknown kinds/newer schema versions, and keeps legacy metadata-free reports backward compatible.
94+
- `tw-patch restore --json` now exposes report metadata fields (`reportKind`, `reportSchemaVersion`) when available for easier diagnostics.
9495
- Commands resolve configuration from `tailwindcss-patch.config.ts` via `@tailwindcss-mangle/config`. Existing configuration files continue to work without changes.
9596

9697
## 4. Cache handling

packages/tailwindcss-patch/README-cn.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ CLI 会通过 `@tailwindcss-mangle/config` 加载 `tailwindcss-patch.config.ts`
9090
| `--json` | 输出 JSON 格式的恢复结果。 |
9191

9292
`tw-patch restore` 在报告包含元数据时会执行 schema 校验。若 `reportKind` 不匹配或 `schemaVersion` 高于当前支持版本,会拒绝恢复;不包含该元数据的历史报告仍保持兼容。
93+
使用 `--json` 时,恢复结果会在报告含元数据时附带 `reportKind` / `reportSchemaVersion` 字段。
9394

9495
### `tokens` 常用参数
9596

packages/tailwindcss-patch/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Migration reports now include envelope metadata: `reportKind`, `schemaVersion`,
147147
| `--json` | Print restore summary as JSON. |
148148

149149
`tw-patch restore` validates report schema metadata when available. Reports with unsupported `reportKind` or newer `schemaVersion` are rejected to avoid unsafe restores. Legacy reports without metadata are still supported.
150+
With `--json`, restore output includes `reportKind` / `reportSchemaVersion` when report metadata is present.
150151

151152
### Token report options
152153

packages/tailwindcss-patch/src/cli/migrate-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export interface RestoreConfigFilesOptions {
9696
export interface RestoreConfigFilesResult {
9797
cwd: string
9898
reportFile: string
99+
reportKind?: string
99100
reportSchemaVersion?: number
100101
dryRun: boolean
101102
strict: boolean
@@ -718,6 +719,7 @@ export async function restoreConfigFiles(options: RestoreConfigFilesOptions): Pr
718719
return {
719720
cwd,
720721
reportFile,
722+
...(report.reportKind === undefined ? {} : { reportKind: report.reportKind }),
721723
...(report.schemaVersion === undefined ? {} : { reportSchemaVersion: report.schemaVersion }),
722724
dryRun,
723725
strict,

packages/tailwindcss-patch/test/cli.migrate-config.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ describe('restoreConfigFiles', () => {
296296
await fs.outputFile(target, `export default { registry: { extract: { file: 'x.json' } } }\n`, 'utf8')
297297
await fs.outputFile(backup, `export default { registry: { output: { file: 'x.json' } } }\n`, 'utf8')
298298
await fs.outputJSON(reportPath, {
299+
reportKind: 'tw-patch-migrate-report',
300+
schemaVersion: 1,
299301
entries: [{ file: target, backupFile: backup }],
300302
}, { spaces: 2 })
301303

@@ -304,7 +306,8 @@ describe('restoreConfigFiles', () => {
304306
reportFile: '.tw-patch/migrate-report.json',
305307
})
306308

307-
expect(result.reportSchemaVersion).toBeUndefined()
309+
expect(result.reportKind).toBe('tw-patch-migrate-report')
310+
expect(result.reportSchemaVersion).toBe(1)
308311
expect(result.restoredFiles).toBe(1)
309312
expect(result.missingBackups).toBe(0)
310313
const restored = await fs.readFile(target, 'utf8')
@@ -344,6 +347,32 @@ describe('restoreConfigFiles', () => {
344347
}
345348
})
346349

350+
it('supports legacy restore report without schema metadata', async () => {
351+
const cwd = await fs.mkdtemp(path.join(os.tmpdir(), 'tw-patch-restore-legacy-'))
352+
const target = path.resolve(cwd, 'tailwindcss-patch.config.ts')
353+
const backup = path.resolve(cwd, '.tw-patch/migrate-backups/tailwindcss-patch.config.ts.bak')
354+
const reportPath = path.resolve(cwd, '.tw-patch/migrate-report.json')
355+
try {
356+
await fs.outputFile(target, `export default { registry: { extract: { file: 'x.json' } } }\n`, 'utf8')
357+
await fs.outputFile(backup, `export default { registry: { output: { file: 'x.json' } } }\n`, 'utf8')
358+
await fs.outputJSON(reportPath, {
359+
entries: [{ file: target, backupFile: backup }],
360+
}, { spaces: 2 })
361+
362+
const result = await restoreConfigFiles({
363+
cwd,
364+
reportFile: '.tw-patch/migrate-report.json',
365+
})
366+
367+
expect(result.reportKind).toBeUndefined()
368+
expect(result.reportSchemaVersion).toBeUndefined()
369+
expect(result.restoredFiles).toBe(1)
370+
}
371+
finally {
372+
await fs.remove(cwd)
373+
}
374+
})
375+
347376
it('throws in strict mode when backup files are missing', async () => {
348377
const cwd = await fs.mkdtemp(path.join(os.tmpdir(), 'tw-patch-restore-strict-'))
349378
const target = path.resolve(cwd, 'tailwindcss-patch.config.ts')

packages/tailwindcss-patch/test/cli.mount.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const { migrateConfigFilesMock, restoreConfigFilesMock } = vi.hoisted(() => {
3232
restoreConfigFilesMock: vi.fn(async () => ({
3333
cwd: '/tmp/project',
3434
reportFile: '/tmp/project/.tw-patch/migrate-report.json',
35+
reportKind: 'tw-patch-migrate-report',
36+
reportSchemaVersion: 1,
3537
dryRun: false,
3638
strict: false,
3739
scannedEntries: 1,
@@ -464,6 +466,8 @@ describe('mountTailwindcssPatchCommands', () => {
464466
expect(logger.log).toHaveBeenCalledWith(JSON.stringify({
465467
cwd: '/tmp/project',
466468
reportFile: '/tmp/project/.tw-patch/migrate-report.json',
469+
reportKind: 'tw-patch-migrate-report',
470+
reportSchemaVersion: 1,
467471
dryRun: false,
468472
strict: false,
469473
scannedEntries: 1,

0 commit comments

Comments
 (0)