@@ -3,6 +3,12 @@ import { PercyIntegrationTypeEnum } from "./sdk-utils/common/types.js";
33import { BrowserStackConfig } from "../lib/types.js" ;
44import { getBrowserStackAuth } from "../lib/get-auth.js" ;
55import { fetchPercyToken } from "./sdk-utils/percy-web/fetchPercyToken.js" ;
6+ import { storedPercyResults } from "../lib/inmemory-store.js" ;
7+ import {
8+ getFrameworkTestCommand ,
9+ PERCY_FALLBACK_STEPS ,
10+ } from "./sdk-utils/percy-web/constants.js" ;
11+ import path from "path" ;
612
713export async function runPercyScan (
814 args : {
@@ -18,25 +24,22 @@ export async function runPercyScan(
1824 type : integrationType ,
1925 } ) ;
2026
21- const steps : string [ ] = [ generatePercyTokenInstructions ( percyToken ) ] ;
22-
23- if ( instruction ) {
24- steps . push (
25- `Use the provided test command with Percy:\n${ instruction } ` ,
26- `If this command fails or is incorrect, fall back to the default approach below.` ,
27- ) ;
28- }
29-
30- steps . push (
31- `Attempt to infer the project's test command from context (high confidence commands first):
32- - Java → mvn test
33- - Python → pytest
34- - Node.js → npm test or yarn test
35- - Cypress → cypress run
36- or from package.json scripts` ,
37- `Wrap the inferred command with Percy along with label: \nnpx percy exec --labels=mcp -- <test command>` ,
38- `If the test command cannot be inferred confidently, ask the user directly for the correct test command.` ,
39- ) ;
27+ // Check if we have stored data and project matches
28+ const stored = storedPercyResults . get ( ) ;
29+
30+ // Compute if we have updated files to run
31+ const hasUpdatedFiles = checkForUpdatedFiles ( stored , projectName ) ;
32+ const updatedFiles = hasUpdatedFiles ? getUpdatedFiles ( stored ) : [ ] ;
33+
34+ // Build steps array with conditional spread
35+ const steps = [
36+ generatePercyTokenInstructions ( percyToken ) ,
37+ ...( hasUpdatedFiles ? generateUpdatedFilesSteps ( stored , updatedFiles ) : [ ] ) ,
38+ ...( instruction && ! hasUpdatedFiles
39+ ? generateInstructionSteps ( instruction )
40+ : [ ] ) ,
41+ ...( ! hasUpdatedFiles ? PERCY_FALLBACK_STEPS : [ ] ) ,
42+ ] ;
4043
4144 const instructionContext = steps
4245 . map ( ( step , index ) => `${ index + 1 } . ${ step } ` )
@@ -59,3 +62,57 @@ export PERCY_TOKEN="${percyToken}"
5962
6063(For Windows: use 'setx PERCY_TOKEN "${ percyToken } "' or 'set PERCY_TOKEN=${ percyToken } ' as appropriate.)` ;
6164}
65+
66+ const toAbs = ( p : string ) : string | undefined =>
67+ p ? path . resolve ( p ) : undefined ;
68+
69+ function checkForUpdatedFiles (
70+ stored : any , // storedPercyResults structure
71+ projectName : string ,
72+ ) : boolean {
73+ const projectMatches = stored ?. projectName === projectName ;
74+ return (
75+ projectMatches &&
76+ stored ?. uuid &&
77+ stored [ stored . uuid ] &&
78+ Object . values ( stored [ stored . uuid ] ) . some ( ( status ) => status === true )
79+ ) ;
80+ }
81+
82+ function getUpdatedFiles ( stored : any ) : string [ ] {
83+ const updatedFiles : string [ ] = [ ] ;
84+ const fileStatusMap = stored [ stored . uuid ! ] ;
85+
86+ Object . entries ( fileStatusMap ) . forEach ( ( [ filePath , status ] ) => {
87+ if ( status === true ) {
88+ updatedFiles . push ( filePath ) ;
89+ }
90+ } ) ;
91+
92+ return updatedFiles ;
93+ }
94+
95+ function generateUpdatedFilesSteps (
96+ stored : any ,
97+ updatedFiles : string [ ] ,
98+ ) : string [ ] {
99+ const filesToRun = updatedFiles . map ( toAbs ) . filter ( Boolean ) as string [ ] ;
100+ const { detectedLanguage, detectedTestingFramework } = stored ;
101+ const exampleCommand = getFrameworkTestCommand (
102+ detectedLanguage ,
103+ detectedTestingFramework ,
104+ ) ;
105+
106+ return [
107+ `Run only the updated files with Percy:\n` +
108+ `Example: ${ exampleCommand } -- <file1> <file2> ...` ,
109+ `Updated files to run:\n${ filesToRun . join ( "\n" ) } ` ,
110+ ] ;
111+ }
112+
113+ function generateInstructionSteps ( instruction : string ) : string [ ] {
114+ return [
115+ `Use the provided test command with Percy:\n${ instruction } ` ,
116+ `If this command fails or is incorrect, fall back to the default approach below.` ,
117+ ] ;
118+ }
0 commit comments