@sprucelabs/spruce-cli 29.1.2 → 29.2.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.
@@ -53,10 +53,7 @@ export default class TestReporter {
53
53
  private handleToggleSmartWatch?: () => any
54
54
  private minWidth = 50
55
55
  private isRpTraining: boolean
56
- private trainingTokenPopup?: PopupWidget
57
56
  private shouldStripCwdFromErrors = true
58
- // private orientationWhenErrorLogWasShown: TestReporterOrientation =
59
- // 'landscape'
60
57
 
61
58
  public constructor(options?: TestReporterOptions) {
62
59
  this.cwd = options?.cwd
@@ -92,10 +89,20 @@ export default class TestReporter {
92
89
  }
93
90
 
94
91
  public setIsRpTraining(isRpTraining: boolean) {
95
- this.setLabelStatus('rp', 'Train AI', isRpTraining)
92
+ this.setRpTrainingStatus(isRpTraining ? 'on' : 'off')
96
93
  this.isRpTraining = isRpTraining
97
94
  }
98
95
 
96
+ public setRpTrainingStatus(status: 'off' | 'installing' | 'on') {
97
+ const colors: Record<string, { fg: string; bg: string }> = {
98
+ off: { fg: 'w', bg: 'r' },
99
+ installing: { fg: 'k', bg: 'y' },
100
+ on: { fg: 'k', bg: 'g' },
101
+ }
102
+ const { fg, bg } = colors[status]
103
+ this.menu.setTextForItem('rp', `Train AI ^${fg}^#^${bg} • ^`)
104
+ }
105
+
99
106
  public startCountdownTimer(durationSec: number) {
100
107
  clearInterval(this.countDownTimeInterval)
101
108
  this.countDownTimeInterval = undefined
@@ -370,38 +377,93 @@ export default class TestReporter {
370
377
  }
371
378
  }
372
379
 
373
- public async askForTrainingToken() {
374
- if (this.trainingTokenPopup) {
375
- return
376
- }
380
+ public async askForProjectName(
381
+ defaultName: string
382
+ ): Promise<string | undefined> {
383
+ return new Promise((resolve) => {
384
+ const popup = this.widgets.Widget('popup', {
385
+ parent: this.window,
386
+ top: 5,
387
+ left: 8,
388
+ width: 65,
389
+ height: 15,
390
+ })
377
391
 
378
- this.trainingTokenPopup = this.widgets.Widget('popup', {
379
- parent: this.window,
380
- top: 10,
381
- left: 10,
382
- width: 50,
383
- height: 10,
384
- })
392
+ this.widgets.Widget('text', {
393
+ parent: popup,
394
+ left: 2,
395
+ top: 1,
396
+ height: 1,
397
+ width: popup.getFrame().width - 4,
398
+ text: 'regressionproof.ai',
399
+ })
385
400
 
386
- this.widgets.Widget('text', {
387
- parent: this.trainingTokenPopup,
388
- left: 4,
389
- top: 3,
390
- height: 4,
391
- width: this.trainingTokenPopup.getFrame().width - 2,
392
- text: 'Coming soon...',
393
- })
401
+ this.widgets.Widget('text', {
402
+ parent: popup,
403
+ left: 2,
404
+ top: 3,
405
+ height: 4,
406
+ width: popup.getFrame().width - 4,
407
+ text: "Help Spruce train coding agents on how to do proper TDD.\nIf you contribute to this effort, you'll get a lifetime\nlicense to the agents for free.",
408
+ })
394
409
 
395
- const button = this.widgets.Widget('button', {
396
- parent: this.trainingTokenPopup,
397
- left: 20,
398
- top: 7,
399
- text: ' Ok ',
400
- })
410
+ const input = this.widgets.Widget('input', {
411
+ parent: popup,
412
+ left: 2,
413
+ top: 8,
414
+ label: 'Name',
415
+ width: popup.getFrame().width - 6,
416
+ height: 1,
417
+ value: defaultName,
418
+ })
401
419
 
402
- await button.on('click', async () => {
403
- await this.trainingTokenPopup?.destroy()
404
- delete this.trainingTokenPopup
420
+ const learnMoreButton = this.widgets.Widget('button', {
421
+ parent: popup,
422
+ left: 2,
423
+ top: 11,
424
+ text: ' Learn more ',
425
+ })
426
+
427
+ const cancelButton = this.widgets.Widget('button', {
428
+ parent: popup,
429
+ left: 40,
430
+ top: 11,
431
+ text: ' Cancel ',
432
+ })
433
+
434
+ const enableButton = this.widgets.Widget('button', {
435
+ parent: popup,
436
+ left: 50,
437
+ top: 11,
438
+ text: ' Enable ',
439
+ })
440
+
441
+ void learnMoreButton.on('click', async () => {
442
+ const open = await import('open')
443
+ void open.default('https://regressionproof.ai')
444
+ })
445
+
446
+ void enableButton.on('click', () => {
447
+ const value = input.getValue()
448
+ void popup.destroy()
449
+ resolve(value || undefined)
450
+ })
451
+
452
+ void cancelButton.on('click', () => {
453
+ void popup.destroy()
454
+ resolve(undefined)
455
+ })
456
+
457
+ void input.on('submit', () => {
458
+ const value = input.getValue()
459
+ void popup.destroy()
460
+ resolve(value || undefined)
461
+ })
462
+
463
+ void input.on('cancel', () => {
464
+ void popup.destroy()
465
+ resolve(undefined)
466
+ })
405
467
  })
406
468
  }
407
469
 
@@ -638,7 +700,6 @@ export default class TestReporter {
638
700
  this.testLog.setText(logContent)
639
701
 
640
702
  if (!errorContent) {
641
- // this.errorLog && this.destroyErrorLog()
642
703
  this.errorLog?.setText(' Nothing to report...')
643
704
  } else {
644
705
  !this.errorLog && this.dropInErrorLog()
@@ -678,8 +739,6 @@ export default class TestReporter {
678
739
  }
679
740
 
680
741
  private dropInErrorLog() {
681
- // this.orientationWhenErrorLogWasShown = this.orientation
682
-
683
742
  if (this.bottomLayout.getRows().length === 1) {
684
743
  if (this.orientation === 'portrait') {
685
744
  this.bottomLayout.addRow({
@@ -719,24 +778,7 @@ export default class TestReporter {
719
778
  }
720
779
  }
721
780
 
722
- private destroyErrorLog() {
723
- // if (this.errorLog) {
724
- // void this.errorLog?.destroy()
725
- // this.errorLog = undefined
726
- // if (this.orientationWhenErrorLogWasShown === 'landscape') {
727
- // this.bottomLayout.removeColumn(0, 1)
728
- // this.bottomLayout.setColumnWidth({
729
- // rowIdx: 0,
730
- // columnIdx: 0,
731
- // width: '100%',
732
- // })
733
- // } else {
734
- // this.bottomLayout.removeRow(1)
735
- // this.bottomLayout.setRowHeight(0, '100%')
736
- // }
737
- // this.bottomLayout.updateLayout()
738
- // }
739
- }
781
+ private destroyErrorLog() {}
740
782
 
741
783
  private updateProgressBar(results: SpruceTestResults) {
742
784
  if (results.totalTestFilesComplete ?? 0 > 0) {
@@ -29,6 +29,7 @@ export default class TestRunner extends AbstractEventEmitter<TestRunnerContract>
29
29
  public async run(options?: {
30
30
  pattern?: string | null
31
31
  debugPort?: number | null
32
+ isRpTraining?: boolean
32
33
  }): Promise<SpruceTestResults & { wasKilled: boolean }> {
33
34
  this.wasKilled = false
34
35
 
@@ -45,7 +46,10 @@ export default class TestRunner extends AbstractEventEmitter<TestRunnerContract>
45
46
  '"'
46
47
  )
47
48
  }
48
- const command = `node --experimental-vm-modules --unhandled-rejections=strict ${debugArgs} ${jestPath} --reporters="@sprucelabs/jest-json-reporter" --testRunner="jest-circus/runner" --passWithNoTests ${
49
+ const rpReporter = options?.isRpTraining
50
+ ? ' --reporters="@regressionproof/jest-reporter"'
51
+ : ''
52
+ const command = `node --experimental-vm-modules --unhandled-rejections=strict ${debugArgs} ${jestPath} --reporters="@sprucelabs/jest-json-reporter"${rpReporter} --testRunner="jest-circus/runner" --passWithNoTests ${
49
53
  pattern ? escapeShell(pattern) : ''
50
54
  }`
51
55
 
@@ -50,11 +50,17 @@ export default class TestAction extends AbstractAction<OptionsSchema> {
50
50
  public async execute(
51
51
  options: SchemaValues<OptionsSchema>
52
52
  ): Promise<FeatureActionResponse> {
53
+ const settings = this.Service('settings')
54
+
53
55
  if (!options.watchMode) {
54
- const settings = this.Service('settings')
55
56
  options.watchMode = settings.get('test.watchMode') ?? 'off'
56
57
  }
57
58
 
59
+ const rpSettings = settings.get('regressionproof') as
60
+ | { enabled: boolean; projectName: string }
61
+ | undefined
62
+ this.isRpTraining = rpSettings?.enabled ?? false
63
+
58
64
  const normalizedOptions = this.validateAndNormalizeOptions(options)
59
65
 
60
66
  const {
@@ -247,11 +253,84 @@ export default class TestAction extends AbstractAction<OptionsSchema> {
247
253
  }
248
254
 
249
255
  private async handleToggleRpTraining() {
250
- // this.isRpTraining = !this.isRpTraining
251
- // this.testReporter?.setIsRpTraining(this.isRpTraining)
252
- // if (this.isRpTraining) {
253
- await this.testReporter?.askForTrainingToken()
254
- // }
256
+ const settings = this.Service('settings')
257
+ const rpSettings = settings.get('regressionproof') as
258
+ | { enabled: boolean; projectName: string }
259
+ | undefined
260
+
261
+ if (!rpSettings) {
262
+ const defaultName = await this.getDefaultProjectName()
263
+ const projectName =
264
+ await this.testReporter?.askForProjectName(defaultName)
265
+
266
+ if (projectName) {
267
+ try {
268
+ this.testReporter?.setRpTrainingStatus('installing')
269
+ this.testReporter?.setStatusLabel(
270
+ 'Installing regressionproof packages...'
271
+ )
272
+
273
+ const pkgService = this.Service('pkg')
274
+ await pkgService.install(
275
+ [
276
+ '@regressionproof/cli',
277
+ '@regressionproof/jest-reporter',
278
+ ],
279
+ { isDev: true }
280
+ )
281
+
282
+ this.testReporter?.setStatusLabel(
283
+ 'Initializing regressionproof...'
284
+ )
285
+
286
+ const commandService = this.Service('command')
287
+ await commandService.execute(
288
+ `node node_modules/@regressionproof/cli/build/cli.js init ${projectName}`,
289
+ { ignoreErrors: true }
290
+ )
291
+
292
+ settings.set('regressionproof', {
293
+ enabled: true,
294
+ projectName,
295
+ })
296
+ this.isRpTraining = true
297
+ this.testReporter?.setIsRpTraining(true)
298
+ this.testReporter?.setStatusLabel('')
299
+ } catch (err: any) {
300
+ this.testReporter?.setRpTrainingStatus('off')
301
+ const message = err?.message ?? 'Unknown error'
302
+ this.testReporter?.setStatusLabel(
303
+ `Setup failed: ${message}`
304
+ )
305
+ }
306
+ }
307
+ } else {
308
+ const newEnabled = !rpSettings.enabled
309
+ settings.set('regressionproof', {
310
+ ...rpSettings,
311
+ enabled: newEnabled,
312
+ })
313
+ this.isRpTraining = newEnabled
314
+ this.testReporter?.setIsRpTraining(newEnabled)
315
+ }
316
+ }
317
+
318
+ private async getDefaultProjectName(): Promise<string> {
319
+ try {
320
+ const commandService = this.Service('command')
321
+ const result = await commandService.execute(
322
+ 'git remote get-url origin',
323
+ { ignoreErrors: true }
324
+ )
325
+ if (result.stdout) {
326
+ const match = result.stdout.match(/\/([^/]+?)(?:\.git)?$/)
327
+ if (match) {
328
+ return match[1].replace('.git', '')
329
+ }
330
+ }
331
+ } catch {}
332
+
333
+ return pathUtil.basename(this.cwd)
255
334
  }
256
335
 
257
336
  public setWatchMode(mode: WatchMode) {
@@ -381,6 +460,7 @@ export default class TestAction extends AbstractAction<OptionsSchema> {
381
460
  let testResults: SpruceTestResults = await this.testRunner.run({
382
461
  pattern: this.pattern,
383
462
  debugPort: this.inspect,
463
+ isRpTraining: this.isRpTraining,
384
464
  })
385
465
 
386
466
  if (
@@ -1,3 +1,5 @@
1
+ import fsUtil from 'fs'
2
+ import pathUtil from 'path'
1
3
  import {
2
4
  diskUtil,
3
5
  PkgService as BasePkgService,
@@ -25,7 +27,51 @@ export default class PkgService extends BasePkgService {
25
27
  }
26
28
 
27
29
  public async install(pkg?: string[] | string, options?: AddOptions) {
28
- return this.packageManager.installDependencies(pkg, options)
30
+ const filtered = this.filterOutWorkspaceSiblings(pkg)
31
+ if (Array.isArray(filtered) && filtered.length === 0) {
32
+ return { totalInstalled: 0 }
33
+ }
34
+ return this.packageManager.installDependencies(filtered, options)
35
+ }
36
+
37
+ private filterOutWorkspaceSiblings(
38
+ pkg?: string[] | string
39
+ ): string[] | string | undefined {
40
+ if (!pkg) {
41
+ return pkg
42
+ }
43
+ const siblings = this.getWorkspaceSiblingNames()
44
+ const packages = Array.isArray(pkg) ? pkg : [pkg]
45
+ const filtered = packages.filter((p) => !siblings.includes(p))
46
+ return Array.isArray(pkg) ? filtered : filtered[0]
47
+ }
48
+
49
+ private getWorkspaceSiblingNames(): string[] {
50
+ const packagesDir = pathUtil.dirname(this.cwd)
51
+ if (pathUtil.basename(packagesDir) !== 'packages') {
52
+ return []
53
+ }
54
+
55
+ const monorepoRoot = pathUtil.dirname(packagesDir)
56
+ const rootPkgPath = pathUtil.join(monorepoRoot, 'package.json')
57
+ if (!diskUtil.doesFileExist(rootPkgPath)) {
58
+ return []
59
+ }
60
+
61
+ const siblingDirs = fsUtil.readdirSync(packagesDir)
62
+ const names: string[] = []
63
+
64
+ for (const dir of siblingDirs) {
65
+ const pkgPath = pathUtil.join(packagesDir, dir, 'package.json')
66
+ if (diskUtil.doesFileExist(pkgPath)) {
67
+ const siblingPkg = JSON.parse(diskUtil.readFile(pkgPath))
68
+ if (siblingPkg.name) {
69
+ names.push(siblingPkg.name)
70
+ }
71
+ }
72
+ }
73
+
74
+ return names
29
75
  }
30
76
 
31
77
  public getSkillNamespace() {
@@ -138,6 +138,7 @@ export default abstract class AbstractCliTest extends AbstractSpruceTest {
138
138
  }
139
139
 
140
140
  if (
141
+ this.homeDir &&
141
142
  diskUtil.doesDirExist(this.homeDir) &&
142
143
  testUtil.shouldClearCache()
143
144
  ) {