Skip to content

Commit a6abe81

Browse files
committed
feat: verify build mode
1 parent 21bb75c commit a6abe81

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

examples/plugin-git-ui/src/node/plugin.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { exec } from 'tinyexec'
44
import { getGitState, git } from './git'
55
import { buildSpec } from './spec'
66

7-
async function refreshUi(ctx: { cwd: string, docks: any }, ui: JsonRenderer) {
7+
async function refreshUi(ctx: { cwd: string, docks: any }, ui: JsonRenderer, interactive: boolean) {
88
const gitState = await getGitState(ctx.cwd)
9-
await ui.updateSpec(buildSpec(gitState))
9+
await ui.updateSpec(buildSpec(gitState, { interactive }))
1010
const total = gitState.staged.length + gitState.unstaged.length
1111
ctx.docks.update({
1212
id: 'git-ui',
@@ -18,14 +18,14 @@ async function refreshUi(ctx: { cwd: string, docks: any }, ui: JsonRenderer) {
1818
})
1919
}
2020

21-
function createRpcFunctions(gitRoot: string, getUi: () => JsonRenderer) {
21+
function createRpcFunctions(gitRoot: string, getUi: () => JsonRenderer, interactive: boolean) {
2222
return [
2323
defineRpcFunction({
2424
name: 'git-ui:refresh',
2525
type: 'action',
2626
setup: ctx => ({
2727
handler: async () => {
28-
await refreshUi(ctx, getUi())
28+
await refreshUi(ctx, getUi(), interactive)
2929
},
3030
}),
3131
}),
@@ -44,7 +44,7 @@ function createRpcFunctions(gitRoot: string, getUi: () => JsonRenderer) {
4444
else {
4545
ctx.logs.add({ message: `Stage failed: ${result.stderr}`, level: 'error', category: 'git-ui', notify: true })
4646
}
47-
await refreshUi(ctx, getUi())
47+
await refreshUi(ctx, getUi(), interactive)
4848
},
4949
}),
5050
}),
@@ -63,7 +63,7 @@ function createRpcFunctions(gitRoot: string, getUi: () => JsonRenderer) {
6363
else {
6464
ctx.logs.add({ message: `Unstage failed: ${result.stderr}`, level: 'error', category: 'git-ui', notify: true })
6565
}
66-
await refreshUi(ctx, getUi())
66+
await refreshUi(ctx, getUi(), interactive)
6767
},
6868
}),
6969
}),
@@ -80,7 +80,7 @@ function createRpcFunctions(gitRoot: string, getUi: () => JsonRenderer) {
8080
else {
8181
ctx.logs.add({ message: `Stage all failed: ${result.stderr}`, level: 'error', category: 'git-ui', notify: true })
8282
}
83-
await refreshUi(ctx, getUi())
83+
await refreshUi(ctx, getUi(), interactive)
8484
},
8585
}),
8686
}),
@@ -104,7 +104,7 @@ function createRpcFunctions(gitRoot: string, getUi: () => JsonRenderer) {
104104
ctx.logs.add({ message: `Commit failed: ${result.stderr}`, level: 'error', category: 'git-ui', notify: true })
105105
}
106106

107-
await refreshUi(ctx, getUi())
107+
await refreshUi(ctx, getUi(), interactive)
108108
},
109109
}),
110110
}),
@@ -116,9 +116,10 @@ export function GitUIPlugin(): PluginWithDevTools {
116116
name: 'plugin-git-ui',
117117
devtools: {
118118
async setup(ctx) {
119+
const interactive = ctx.mode === 'dev'
119120
const gitRoot = (await exec('git', ['rev-parse', '--show-toplevel'], { nodeOptions: { cwd: ctx.cwd }, throwOnError: true })).stdout.trim() || ctx.cwd
120121
const gitState = await getGitState(gitRoot)
121-
const ui = ctx.createJsonRenderer(buildSpec(gitState))
122+
const ui = ctx.createJsonRenderer(buildSpec(gitState, { interactive }))
122123

123124
const total = gitState.staged.length + gitState.unstaged.length
124125
ctx.docks.register({
@@ -131,9 +132,11 @@ export function GitUIPlugin(): PluginWithDevTools {
131132
badge: total > 0 ? String(total) : undefined,
132133
})
133134

134-
const rpcFunctions = createRpcFunctions(gitRoot, () => ui)
135-
for (const fn of rpcFunctions) {
136-
ctx.rpc.register(fn)
135+
if (interactive) {
136+
const rpcFunctions = createRpcFunctions(gitRoot, () => ui, interactive)
137+
for (const fn of rpcFunctions) {
138+
ctx.rpc.register(fn)
139+
}
137140
}
138141
},
139142
},

examples/plugin-git-ui/src/node/spec.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function buildFileRows(
2020
prefix: string,
2121
actionName: string,
2222
actionIcon: string,
23+
interactive: boolean,
2324
): { children: string[], elements: Record<string, JsonRenderElement> } {
2425
const children: string[] = []
2526
const elements: Record<string, JsonRenderElement> = {}
@@ -30,14 +31,29 @@ function buildFileRows(
3031
const rowId = `${prefix}-row-${i}`
3132
const statusId = `${prefix}-status-${i}`
3233
const fileId = `${prefix}-file-${i}`
33-
const spacerId = `${prefix}-spacer-${i}`
34-
const btnId = `${prefix}-btn-${i}`
3534

3635
children.push(rowId)
36+
const rowChildren = [statusId, fileId]
37+
38+
if (interactive) {
39+
const spacerId = `${prefix}-spacer-${i}`
40+
const btnId = `${prefix}-btn-${i}`
41+
rowChildren.push(spacerId, btnId)
42+
elements[spacerId] = {
43+
type: 'Stack',
44+
props: { direction: 'horizontal', flex: 1 },
45+
}
46+
elements[btnId] = {
47+
type: 'Button',
48+
props: { icon: actionIcon, variant: 'ghost' },
49+
on: { press: { action: actionName, params: { file } } },
50+
}
51+
}
52+
3753
elements[rowId] = {
3854
type: 'Stack',
3955
props: { direction: 'horizontal', gap: 8, align: 'center' },
40-
children: [statusId, fileId, spacerId, btnId],
56+
children: rowChildren,
4157
}
4258
elements[statusId] = {
4359
type: 'Badge',
@@ -52,23 +68,20 @@ function buildFileRows(
5268
type: 'Text',
5369
props: { content: file, variant: 'code' },
5470
}
55-
elements[spacerId] = {
56-
type: 'Stack',
57-
props: { direction: 'horizontal', flex: 1 },
58-
}
59-
elements[btnId] = {
60-
type: 'Button',
61-
props: { icon: actionIcon, variant: 'ghost' },
62-
on: { press: { action: actionName, params: { file } } },
63-
}
6471
}
6572

6673
return { children, elements }
6774
}
6875

69-
export function buildSpec(gitState: GitState): JsonRenderSpec {
70-
const stagedRows = buildFileRows(gitState.staged, 'staged', 'git-ui:unstage', 'ph:minus-circle')
71-
const unstagedRows = buildFileRows(gitState.unstaged, 'unstaged', 'git-ui:stage', 'ph:plus-circle')
76+
export function buildSpec(gitState: GitState, options?: { interactive?: boolean }): JsonRenderSpec {
77+
const interactive = options?.interactive ?? true
78+
const stagedRows = buildFileRows(gitState.staged, 'staged', 'git-ui:unstage', 'ph:minus-circle', interactive)
79+
const unstagedRows = buildFileRows(gitState.unstaged, 'unstaged', 'git-ui:stage', 'ph:plus-circle', interactive)
80+
81+
const rootChildren = ['header', 'branch-info']
82+
if (interactive)
83+
rootChildren.push('commit-section')
84+
rootChildren.push('divider1', 'staged-card', 'unstaged-card', 'commits-card')
7285

7386
return {
7487
root: 'root',
@@ -79,12 +92,12 @@ export function buildSpec(gitState: GitState): JsonRenderSpec {
7992
'root': {
8093
type: 'Stack',
8194
props: { direction: 'vertical', gap: 12, padding: 4 },
82-
children: ['header', 'branch-info', 'commit-section', 'divider1', 'staged-card', 'unstaged-card', 'commits-card'],
95+
children: rootChildren,
8396
},
8497
'header': {
8598
type: 'Stack',
8699
props: { direction: 'horizontal', gap: 8, align: 'center', justify: 'space-between' },
87-
children: ['title', 'refresh-btn'],
100+
children: interactive ? ['title', 'refresh-btn'] : ['title'],
88101
},
89102
'title': {
90103
type: 'Text',
@@ -163,7 +176,9 @@ export function buildSpec(gitState: GitState): JsonRenderSpec {
163176
'unstaged-card': {
164177
type: 'Card',
165178
props: { title: `Unstaged (${gitState.unstaged.length})`, collapsible: true },
166-
children: gitState.unstaged.length > 0 ? ['unstaged-header', 'unstaged-files'] : ['unstaged-empty'],
179+
children: gitState.unstaged.length > 0
180+
? (interactive ? ['unstaged-header', 'unstaged-files'] : ['unstaged-files'])
181+
: ['unstaged-empty'],
167182
},
168183
'unstaged-header': {
169184
type: 'Stack',

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)