Skip to content

Commit fda3fa7

Browse files
author
Bruce Hill
committed
Reuse logic from lint command inside the dev command
1 parent cb15abc commit fda3fa7

File tree

2 files changed

+48
-50
lines changed

2 files changed

+48
-50
lines changed

pkg/cmd/dev.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -359,32 +359,12 @@ func runPreview(ctx context.Context, cmd *cli.Command) error {
359359

360360
// Phase 3: Start build and monitor progress in a loop
361361
for {
362-
// Keep checking diagnostics until they're all fixed
363-
for {
364-
diagnostics, err := getDiagnostics(ctx, cmd, cc)
365-
if err != nil {
366-
if errors.Is(err, ErrUserCancelled) {
367-
return nil
368-
}
369-
return err
370-
}
371-
372-
if len(diagnostics) > 0 {
373-
fmt.Println(ViewDiagnosticsPrint(diagnostics, 10))
374-
}
375-
376-
if hasBlockingDiagnostic(diagnostics) {
377-
fmt.Println("\nDiagnostic checks will re-run once you edit your configuration files...")
378-
if err := waitTillConfigChanges(ctx, cmd, cc); err != nil {
379-
if errors.Is(err, ErrUserCancelled) {
380-
return nil
381-
}
382-
return err
383-
}
384-
continue
385-
} else {
386-
break
362+
// Make the user get past linter errors
363+
if err := runLinter(ctx, cmd, true); err != nil {
364+
if errors.Is(err, ErrUserCancelled) {
365+
return nil
387366
}
367+
return err
388368
}
389369

390370
// Start the build process

pkg/cmd/lint.go

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,31 @@ var lintCommand = cli.Command{
3939
Usage: "Watch for files to change and re-run linting",
4040
},
4141
},
42-
Action: runLinter,
42+
Action: func(ctx context.Context, cmd *cli.Command) error {
43+
if cmd.Bool("watch") {
44+
// Clear the screen and move the cursor to the top
45+
fmt.Print("\033[2J\033[H")
46+
os.Stdout.Sync()
47+
}
48+
return runLinter(ctx, cmd, false)
49+
},
4350
}
4451

4552
var helpStyle = lipgloss.NewStyle().
4653
Foreground(lipgloss.Color("241")).
4754
Margin(1, 0, 0, 0)
4855

4956
type lintModel struct {
50-
spinner spinner.Model
51-
diagnostics []stainless.BuildDiagnostic
52-
error error
53-
watching bool
54-
ctx context.Context
55-
cmd *cli.Command
56-
cc *apiCommandContext
57-
stopPolling chan struct{}
58-
help helpModel
57+
spinner spinner.Model
58+
diagnostics []stainless.BuildDiagnostic
59+
error error
60+
watching bool
61+
stopWhenPassing bool
62+
ctx context.Context
63+
cmd *cli.Command
64+
cc *apiCommandContext
65+
stopPolling chan struct{}
66+
help helpModel
5967
}
6068

6169
type helpModel struct {
@@ -64,18 +72,17 @@ type helpModel struct {
6472
}
6573

6674
func (m lintModel) Init() tea.Cmd {
67-
if m.watching {
68-
// Clear the screen and move the cursor to the top
69-
fmt.Print("\033[2J\033[H")
70-
os.Stdout.Sync()
71-
}
7275
return m.spinner.Tick
7376
}
7477

7578
func (m lintModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7679
switch msg := msg.(type) {
7780
case tea.KeyMsg:
7881
if msg.String() == "ctrl+c" {
82+
m.watching = false
83+
m.error = ErrUserCancelled
84+
return m, tea.Quit
85+
} else if msg.String() == "enter" {
7986
m.watching = false
8087
return m, tea.Quit
8188
}
@@ -87,6 +94,11 @@ func (m lintModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8794
m.cmd = msg.cmd
8895
m.cc = msg.cc
8996

97+
if m.stopWhenPassing && !hasBlockingDiagnostic(m.diagnostics) {
98+
m.watching = false
99+
return m, tea.Quit
100+
}
101+
90102
if m.watching {
91103
return m, func() tea.Msg {
92104
if err := waitTillConfigChanges(m.ctx, m.cmd, m.cc); err != nil {
@@ -130,8 +142,13 @@ func (m lintModel) View() string {
130142
}
131143
}
132144

133-
helpText := helpStyle.Render("Press Ctrl+C to exit")
134-
return content + helpText
145+
if m.stopWhenPassing {
146+
content += helpStyle.Render("Press Enter to skip diagnostics, Ctrl+C to exit")
147+
} else {
148+
content += helpStyle.Render("Press Ctrl+C to exit")
149+
}
150+
151+
return content
135152
}
136153

137154
type diagnosticsMsg struct {
@@ -155,21 +172,22 @@ func getDiagnosticsCmd(ctx context.Context, cmd *cli.Command, cc *apiCommandCont
155172
}
156173
}
157174

158-
func runLinter(ctx context.Context, cmd *cli.Command) error {
175+
func runLinter(ctx context.Context, cmd *cli.Command, stopWhenPassing bool) error {
159176
cc := getAPICommandContext(cmd)
160177

161178
s := spinner.New()
162179
s.Spinner = spinner.MiniDot
163180
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("208"))
164181

165182
m := lintModel{
166-
spinner: s,
167-
watching: cmd.Bool("watch"),
168-
ctx: ctx,
169-
cmd: cmd,
170-
cc: cc,
171-
stopPolling: make(chan struct{}),
172-
help: helpModel{},
183+
spinner: s,
184+
watching: cmd.Bool("watch"),
185+
stopWhenPassing: stopWhenPassing,
186+
ctx: ctx,
187+
cmd: cmd,
188+
cc: cc,
189+
stopPolling: make(chan struct{}),
190+
help: helpModel{},
173191
}
174192

175193
p := tea.NewProgram(m, tea.WithContext(ctx))

0 commit comments

Comments
 (0)