create-wxl 0.0.0-stage → 3.1.0

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.
@@ -0,0 +1,134 @@
1
+ // Step 10 (traceability.md "Step order" row 10, "Report"): prints R1-R5, one per line, each
2
+ // starting with its own id, in the run's language (`t2`). R3 renders each attention item through the
3
+ // language table's own UP-A* text. R2 lists every path M6 deleted after the counts (D16). R4
4
+ // (traceability.md "Report" R4) is a fixed body of next-steps advice plus three conditional bullets:
5
+ // D16's `|||||||` line only when R3 holds UP-A1, `pnpm install` only when the run wrote
6
+ // `package.json` or `pnpm-lock.yaml`, and the deploy-tag push only for a range with
7
+ // `.github/workflows/deploy.yml`.
8
+
9
+ import { jsonValue, posixCommand } from './text.mjs'
10
+
11
+ /** D14 (T-0063): the restore commands, in order -- `checkout` first, so a missing or changed
12
+ * ignore file is back before `clean` runs, and `clean` never deletes a file `HEAD`'s rules ignore.
13
+ * R5 and UP-E09 print them, and U70 runs them, from this one list. */
14
+ export const RESTORE_COMMANDS = Object.freeze([
15
+ Object.freeze(['git', 'checkout', '--', '.']),
16
+ Object.freeze(['git', 'clean', '-fd']),
17
+ ])
18
+
19
+ /** D23 (T-0066): the restore commands as the one line R5 and UP-E09 print, joined with `&&`, so a
20
+ * `git checkout -- .` that fails never lets `git clean -fd` run (a shell without `&&`, such as
21
+ * Windows PowerShell 5.1, refuses the whole line, and nothing runs). */
22
+ export function restoreCommandLine() {
23
+ return RESTORE_COMMANDS.map((command) => posixCommand(command)).join(' && ')
24
+ }
25
+
26
+ /** `ops` carrying a `report` code (M10, or an M12 conflict/UP-A11 outcome) are written, but not
27
+ * counted here (traceability.md "Report" R2's own "M10 is not counted as updated"). */
28
+ function countsFromOps(ops) {
29
+ const counts = { updated: 0, added: 0, deleted: 0 }
30
+ for (const op of ops) {
31
+ if (op.report !== undefined) continue
32
+ if (op.action === 'delete') counts.deleted++
33
+ else if (op.action === 'write') counts[op.existedBefore ? 'updated' : 'added']++
34
+ }
35
+ return counts
36
+ }
37
+
38
+ /** UP-A12 sorts last among attention lines (traceability.md "Report" R3); every other code sorts
39
+ * by path. */
40
+ function sortAttention(attention) {
41
+ const rest = attention.filter((item) => item.code !== 'UP-A12').sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0))
42
+ const anchor = attention.filter((item) => item.code === 'UP-A12')
43
+ return [...rest, ...anchor]
44
+ }
45
+
46
+ /** T-0027 increment 21 (U26), traceability.md "Report" R2's own "M10 is not counted as updated":
47
+ * unlike `countsFromOps`, above, this does not look at `op.report` at all -- a written
48
+ * `package.json`/`pnpm-lock.yaml` still needs `pnpm install` advice whether or not it was an M10
49
+ * lockfile-conflict write or a plain M5/M12 write. */
50
+ function wroteDependencyFile(ops) {
51
+ return ops.some((op) => op.action === 'write' && (op.path === 'package.json' || op.path === 'pnpm-lock.yaml'))
52
+ }
53
+
54
+ /** R4 (traceability.md "Report"): a fixed body of next-steps advice, plus three conditional
55
+ * bullets (D16's `|||||||` line, `pnpm install`, the `v*` tag), each its own `R4:` line (mirrors
56
+ * R3's own one-line-per-item convention). */
57
+ function buildR4Lines(lang2, { ops, attention, hasDeployWorkflow }) {
58
+ const zh = lang2 === 'zh-TW'
59
+ const lines = [
60
+ zh ? 'R4: 執行 git status 與 git diff' : 'R4: run git status and git diff',
61
+ // Named generically here (never the literal code "UP-A1"), so this fixed bullet never
62
+ // collides with a path-specific assertion elsewhere checking a particular attention code's
63
+ // ABSENCE from the whole run's stdout (e.g. U56, increment 17's own `not.toContain('UP-A1')`).
64
+ zh
65
+ ? 'R4: 處理上面的衝突標記(搜尋 <<<<<<<)與其他注意項目'
66
+ : 'R4: resolve the conflict markers above (search for <<<<<<<) and the other attention items',
67
+ ]
68
+ // D16 (T-0063): when R3 holds UP-A1, say whose text the `|||||||` section of a conflict block is.
69
+ // Like the bullet above, it never names the code itself.
70
+ if (attention.some((item) => item.code === 'UP-A1')) {
71
+ lines.push(
72
+ zh
73
+ ? 'R4: 衝突區塊裡 ||||||| 之後的段落是 R1 所列 base 版本的內容;如果這個 base 不是靶場原本所依據的版本,請先執行 R5 的指令'
74
+ : 'R4: in a conflict block, the section after ||||||| is the text of the base version in R1; ' +
75
+ 'if that base is not the version the range came from, run the R5 commands first',
76
+ )
77
+ }
78
+ if (wroteDependencyFile(ops)) lines.push(zh ? 'R4: 執行 pnpm install' : 'R4: run pnpm install')
79
+ lines.push(zh ? 'R4: build 並測試' : 'R4: build and test')
80
+ lines.push('R4: commit')
81
+ if (hasDeployWorkflow) lines.push(zh ? 'R4: 推送新的 v* tag 以觸發部署' : 'R4: push a new v* tag to deploy')
82
+ return lines
83
+ }
84
+
85
+ export function buildReport({ lang2, toolVersion, baseVersion, baseCommit, targetVersion, targetCommit, ops, attention, hasDeployWorkflow, t2, baseUnchecked = false }) {
86
+ const zh = lang2 === 'zh-TW'
87
+ const counts = countsFromOps(ops)
88
+ const lines = []
89
+ // D17 (traceability.md "Report" R1; T-0061): a `--from <commit>` base is whatever that commit
90
+ // holds -- R1 says it was not checked against an upstream tag.
91
+ const uncheckedNote = baseUnchecked
92
+ ? zh
93
+ ? ';base 由 --from <commit> 指定,沒有比對上游的 tag'
94
+ : '; the base was given by --from <commit> and not checked against an upstream tag'
95
+ : ''
96
+ lines.push(
97
+ zh
98
+ ? `R1: create-wxl ${jsonValue(toolVersion)};base 版本 ${jsonValue(baseVersion)}(commit ${jsonValue(baseCommit)});` +
99
+ `target 版本 ${jsonValue(targetVersion)}(commit ${jsonValue(targetCommit)})` +
100
+ uncheckedNote
101
+ // Names the commit the same way the zh-TW text above already does.
102
+ : `R1: create-wxl ${jsonValue(toolVersion)}; base ${jsonValue(baseVersion)} (commit ${jsonValue(baseCommit)}); ` +
103
+ `target ${jsonValue(targetVersion)} (commit ${jsonValue(targetCommit)})` +
104
+ uncheckedNote,
105
+ )
106
+ lines.push(
107
+ zh
108
+ ? `R2: 更新 ${counts.updated} 新增 ${counts.added} 刪除 ${counts.deleted}`
109
+ : `R2: updated ${counts.updated} added ${counts.added} deleted ${counts.deleted}`,
110
+ )
111
+ // D16 (T-0063): then every path M6 deleted (the only deleting row), sorted by UTF-16 code units,
112
+ // without a code.
113
+ const deletedPaths = ops.filter((op) => op.action === 'delete').map((op) => op.path)
114
+ for (const path of deletedPaths.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))) lines.push(`R2: ${jsonValue(path)}`)
115
+ // T-0027 verification follow-up (item 1/2): each attention line is rendered through `t2`
116
+ // (traceability.md:206 "in the run's language"), the same en/zh-TW UP-A* message table every
117
+ // other code already uses -- UP-A12 (T-0027 increment 20, U59) is not tied to any one path
118
+ // (traceability.md "Fork:init-derived content"), so it prints without one, unlike every other
119
+ // UP-A* line.
120
+ for (const item of sortAttention(attention)) {
121
+ lines.push(item.code === 'UP-A12' ? `R3: ${t2[item.code]()}` : `R3: ${t2[item.code]()} ${jsonValue(item.path)}`)
122
+ }
123
+ lines.push(...buildR4Lines(lang2, { ops, attention, hasDeployWorkflow }))
124
+ // traceability.md "Report" R5; spec.en.md B4: the restore commands undo
125
+ // the WHOLE upgrade, so they are only safe BEFORE any other change -- the fixed bare commands
126
+ // alone, without that warning, let a teacher who edits first lose that edit too.
127
+ // The same one line UP-E09 prints (messages.mjs, D23).
128
+ lines.push(
129
+ zh
130
+ ? `R5: 如果要撤銷整個升級,請在做其他變更之前執行 ${restoreCommandLine()}`
131
+ : `R5: to undo the whole upgrade before making other changes, run ${restoreCommandLine()}`,
132
+ )
133
+ return lines.join('\n') + '\n'
134
+ }