@@ -22,16 +22,35 @@ import {
2222import { analyzeReactProfile , type ReactProfileAnalysis } from './react-profile-analysis.ts' ;
2323import {
2424 selectPropKindRequests ,
25+ type CommitProfile ,
26+ type ComponentProfile ,
2527 type PropKindsById ,
2628 type RawReactProfile ,
2729} from './react-profile-renders.ts' ;
2830import {
31+ componentsByRenders ,
2932 DEFAULT_FINDING_LIMIT ,
30- formatProfileSummary ,
33+ findComponent ,
3134 formatSeconds ,
35+ formatCommit ,
36+ formatComponentReport ,
37+ formatProfileSummary ,
38+ formatRerenders ,
39+ formatSlowComponents ,
40+ formatTimeline ,
41+ timelineCommits ,
3242} from './react-profile-format.ts' ;
3343
34- const REACT_PROFILE_SUBCOMMANDS = [ 'start' , 'stop' , 'status' ] as const ;
44+ const REACT_PROFILE_SUBCOMMANDS = [
45+ 'start' ,
46+ 'stop' ,
47+ 'status' ,
48+ 'slow' ,
49+ 'rerenders' ,
50+ 'report' ,
51+ 'timeline' ,
52+ 'commit' ,
53+ ] as const ;
3554
3655type ReactProfileSubcommand = ( typeof REACT_PROFILE_SUBCOMMANDS ) [ number ] ;
3756
@@ -43,15 +62,17 @@ export type ReactProfileCommandOptions = {
4362
4463type ProfileArgs = {
4564 subcommand : ReactProfileSubcommand ;
65+ target ?: string ;
4666 limit ?: number ;
4767 app ?: string ;
4868 metroDevice ?: string ;
4969 metroHost ?: string ;
5070 metroPort ?: string ;
5171 bundleUrl ?: string ;
72+ sort ?: 'timeline' | 'duration' ;
5273} ;
5374
54- /** Analysis plus the runtime it came from, as `profile stop` saves it for the session . */
75+ /** Analysis plus the runtime it came from, as `profile stop` saves it for the drill-down views . */
5576type SavedReactProfile = {
5677 savedAt : string ;
5778 appId ?: string ;
@@ -64,6 +85,7 @@ type StartResult = { ok: true; attach: string } | RuntimeFailure;
6485type StopResult = ( { ok : true } & RawReactProfile ) | RuntimeFailure ;
6586type StatusResult = { profiling : boolean ; elapsedMs : number | null } ;
6687
88+ const SUBCOMMANDS_WITH_TARGET = new Set < ReactProfileSubcommand > ( [ 'report' , 'commit' ] ) ;
6789const POSITIVE_INTEGER = / ^ [ 1 - 9 ] \d * $ / ;
6890
6991function isReactProfileSubcommand ( value : string | undefined ) : value is ReactProfileSubcommand {
@@ -77,6 +99,7 @@ const PROFILE_OPTIONS = {
7799 'metro-host' : { type : 'string' } ,
78100 'metro-port' : { type : 'string' } ,
79101 'bundle-url' : { type : 'string' } ,
102+ sort : { type : 'string' } ,
80103} as const ;
81104
82105function parseProfileOptions ( args : string [ ] ) {
@@ -97,24 +120,30 @@ function parseProfileArgs(args: string[]): ProfileArgs {
97120 ) ;
98121 }
99122 const { values, positionals } = parseProfileOptions ( rest ) ;
100- if ( positionals . length > 0 ) {
123+ const expected = SUBCOMMANDS_WITH_TARGET . has ( subcommand ) ? 1 : 0 ;
124+ if ( positionals . length > expected ) {
101125 throw new AppError (
102126 'INVALID_ARGS' ,
103- `Unexpected argument for react-devtools profile ${ subcommand } : ${ positionals [ 0 ] } .` ,
127+ `Unexpected argument for react-devtools profile ${ subcommand } : ${ positionals [ expected ] } .` ,
104128 ) ;
105129 }
106130 if ( values . limit !== undefined && ! POSITIVE_INTEGER . test ( values . limit ) ) {
107131 throw new AppError ( 'INVALID_ARGS' , `Invalid --limit: ${ values . limit } . Use a positive integer.` ) ;
108132 }
109133 const limit = values . limit === undefined ? undefined : Number ( values . limit ) ;
134+ if ( values . sort !== undefined && values . sort !== 'timeline' && values . sort !== 'duration' ) {
135+ throw new AppError ( 'INVALID_ARGS' , `Invalid --sort: ${ values . sort } . Use timeline or duration.` ) ;
136+ }
110137 return {
111138 subcommand,
139+ target : positionals [ 0 ] ,
112140 limit,
113141 app : values . app ,
114142 metroDevice : values [ 'metro-device' ] ,
115143 metroHost : values [ 'metro-host' ] ,
116144 metroPort : values [ 'metro-port' ] ,
117145 bundleUrl : values [ 'bundle-url' ] ,
146+ sort : values . sort as ProfileArgs [ 'sort' ] ,
118147 } ;
119148}
120149
@@ -224,6 +253,39 @@ function writeSavedProfile(
224253 return filePath ;
225254}
226255
256+ function isSavedProfile ( value : SavedReactProfile | undefined ) : value is SavedReactProfile {
257+ const analysis = value ?. analysis ;
258+ return (
259+ analysis ?. version === 1 &&
260+ Array . isArray ( analysis . components ) &&
261+ Array . isArray ( analysis . commits ) &&
262+ Array . isArray ( analysis . findings )
263+ ) ;
264+ }
265+
266+ function readSavedProfile ( options : ReactProfileCommandOptions ) : SavedReactProfile {
267+ let raw : string ;
268+ try {
269+ raw = fs . readFileSync ( savedProfilePath ( options ) , 'utf8' ) ;
270+ } catch {
271+ throw new AppError ( 'COMMAND_FAILED' , `No React profile saved for session ${ options . session } .` , {
272+ hint : 'Run react-devtools profile start, perform the interaction, then react-devtools profile stop.' ,
273+ } ) ;
274+ }
275+ let saved : SavedReactProfile | undefined ;
276+ try {
277+ saved = JSON . parse ( raw ) as SavedReactProfile ;
278+ } catch {
279+ saved = undefined ;
280+ }
281+ if ( ! isSavedProfile ( saved ) ) {
282+ throw new AppError ( 'COMMAND_FAILED' , 'The saved React profile has an unsupported format.' , {
283+ hint : 'Record a new profile with react-devtools profile start and stop.' ,
284+ } ) ;
285+ }
286+ return saved ;
287+ }
288+
227289const elapsed = ( elapsedMs : number ) => formatSeconds ( Math . max ( 0 , elapsedMs ) ) ;
228290
229291async function startProfile (
@@ -302,6 +364,32 @@ type SubcommandHandler = (
302364 emit : Emit ,
303365) => Promise < void > ;
304366
367+ function requireComponent (
368+ analysis : ReactProfileAnalysis ,
369+ name : string | undefined ,
370+ ) : ComponentProfile {
371+ if ( ! name )
372+ throw new AppError ( 'INVALID_ARGS' , 'react-devtools profile report requires a component name.' ) ;
373+ const component = findComponent ( analysis , name ) ;
374+ if ( ! component ) {
375+ throw new AppError ( 'COMMAND_FAILED' , `No renders of ${ name } in the saved profile.` , {
376+ hint : 'Use a name from react-devtools profile slow or profile rerenders.' ,
377+ } ) ;
378+ }
379+ return component ;
380+ }
381+
382+ function requireCommit ( analysis : ReactProfileAnalysis , target : string | undefined ) : CommitProfile {
383+ const digits = ( target ?? '' ) . replace ( / ^ # / , '' ) ;
384+ const commit = / ^ \d + $ / . test ( digits ) ? analysis . commits [ Number ( digits ) ] : undefined ;
385+ if ( ! commit ) {
386+ throw new AppError ( 'INVALID_ARGS' , `No commit ${ target ?? '(none)' } in the saved profile.` , {
387+ hint : `Use a commit index from 0 to ${ Math . max ( analysis . commits . length - 1 , 0 ) } .` ,
388+ } ) ;
389+ }
390+ return commit ;
391+ }
392+
305393const SUBCOMMAND_HANDLERS : Record < ReactProfileSubcommand , SubcommandHandler > = {
306394 start : async ( args , options , emit ) => {
307395 const started = await startProfile ( args , options ) ;
@@ -328,6 +416,35 @@ const SUBCOMMAND_HANDLERS: Record<ReactProfileSubcommand, SubcommandHandler> = {
328416 savedTo : savedProfilePath ( options ) ,
329417 } ) ;
330418 } ,
419+ slow : async ( args , options , emit ) => {
420+ const { analysis } = readSavedProfile ( options ) ;
421+ const limit = args . limit ?? 10 ;
422+ emit ( formatSlowComponents ( analysis , limit ) , analysis . components . slice ( 0 , limit ) ) ;
423+ } ,
424+ rerenders : async ( args , options , emit ) => {
425+ const { analysis } = readSavedProfile ( options ) ;
426+ const limit = args . limit ?? 10 ;
427+ emit ( formatRerenders ( analysis , limit ) , componentsByRenders ( analysis ) . slice ( 0 , limit ) ) ;
428+ } ,
429+ report : async ( args , options , emit ) => {
430+ const { analysis } = readSavedProfile ( options ) ;
431+ const component = requireComponent ( analysis , args . target ) ;
432+ emit ( formatComponentReport ( analysis , component ) , component ) ;
433+ } ,
434+ timeline : async ( args , options , emit ) => {
435+ const { analysis } = readSavedProfile ( options ) ;
436+ const limit = args . limit ?? 20 ;
437+ const byDuration = args . sort === 'duration' ;
438+ emit (
439+ formatTimeline ( analysis , limit , byDuration ) ,
440+ timelineCommits ( analysis , byDuration ) . slice ( 0 , limit ) ,
441+ ) ;
442+ } ,
443+ commit : async ( args , options , emit ) => {
444+ const { analysis } = readSavedProfile ( options ) ;
445+ const commit = requireCommit ( analysis , args . target ) ;
446+ emit ( formatCommit ( commit , args . limit ?? 10 ) , commit ) ;
447+ } ,
331448} ;
332449
333450export async function runReactProfileCommand (
0 commit comments