@@ -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
82105type ProfileOption = keyof typeof PROFILE_OPTIONS ;
@@ -94,6 +117,11 @@ const SUBCOMMAND_OPTIONS: Record<ReactProfileSubcommand, readonly ProfileOption[
94117 start : RUNTIME_OPTIONS ,
95118 status : RUNTIME_OPTIONS ,
96119 stop : [ ...RUNTIME_OPTIONS , 'limit' ] ,
120+ slow : [ 'limit' ] ,
121+ rerenders : [ 'limit' ] ,
122+ report : [ ] ,
123+ timeline : [ 'limit' , 'sort' ] ,
124+ commit : [ 'limit' ] ,
97125} ;
98126
99127function parseProfileOptions ( args : string [ ] ) {
@@ -123,24 +151,30 @@ function parseProfileArgs(args: string[]): ProfileArgs {
123151 `react-devtools profile ${ subcommand } does not take --${ unread } .` ,
124152 ) ;
125153 }
126- if ( positionals . length > 0 ) {
154+ const expected = SUBCOMMANDS_WITH_TARGET . has ( subcommand ) ? 1 : 0 ;
155+ if ( positionals . length > expected ) {
127156 throw new AppError (
128157 'INVALID_ARGS' ,
129- `Unexpected argument for react-devtools profile ${ subcommand } : ${ positionals [ 0 ] } .` ,
158+ `Unexpected argument for react-devtools profile ${ subcommand } : ${ positionals [ expected ] } .` ,
130159 ) ;
131160 }
132161 if ( values . limit !== undefined && ! POSITIVE_INTEGER . test ( values . limit ) ) {
133162 throw new AppError ( 'INVALID_ARGS' , `Invalid --limit: ${ values . limit } . Use a positive integer.` ) ;
134163 }
135164 const limit = values . limit === undefined ? undefined : Number ( values . limit ) ;
165+ if ( values . sort !== undefined && values . sort !== 'timeline' && values . sort !== 'duration' ) {
166+ throw new AppError ( 'INVALID_ARGS' , `Invalid --sort: ${ values . sort } . Use timeline or duration.` ) ;
167+ }
136168 return {
137169 subcommand,
170+ target : positionals [ 0 ] ,
138171 limit,
139172 app : values . app ,
140173 metroDevice : values [ 'metro-device' ] ,
141174 metroHost : values [ 'metro-host' ] ,
142175 metroPort : values [ 'metro-port' ] ,
143176 bundleUrl : values [ 'bundle-url' ] ,
177+ sort : values . sort as ProfileArgs [ 'sort' ] ,
144178 } ;
145179}
146180
@@ -250,6 +284,39 @@ function writeSavedProfile(
250284 return filePath ;
251285}
252286
287+ function isSavedProfile ( value : SavedReactProfile | undefined ) : value is SavedReactProfile {
288+ const analysis = value ?. analysis ;
289+ return (
290+ analysis ?. version === 1 &&
291+ Array . isArray ( analysis . components ) &&
292+ Array . isArray ( analysis . commits ) &&
293+ Array . isArray ( analysis . findings )
294+ ) ;
295+ }
296+
297+ function readSavedProfile ( options : ReactProfileCommandOptions ) : SavedReactProfile {
298+ let raw : string ;
299+ try {
300+ raw = fs . readFileSync ( savedProfilePath ( options ) , 'utf8' ) ;
301+ } catch {
302+ throw new AppError ( 'COMMAND_FAILED' , `No React profile saved for session ${ options . session } .` , {
303+ hint : 'Run react-devtools profile start, perform the interaction, then react-devtools profile stop.' ,
304+ } ) ;
305+ }
306+ let saved : SavedReactProfile | undefined ;
307+ try {
308+ saved = JSON . parse ( raw ) as SavedReactProfile ;
309+ } catch {
310+ saved = undefined ;
311+ }
312+ if ( ! isSavedProfile ( saved ) ) {
313+ throw new AppError ( 'COMMAND_FAILED' , 'The saved React profile has an unsupported format.' , {
314+ hint : 'Record a new profile with react-devtools profile start and stop.' ,
315+ } ) ;
316+ }
317+ return saved ;
318+ }
319+
253320const elapsed = ( elapsedMs : number ) => formatSeconds ( Math . max ( 0 , elapsedMs ) ) ;
254321
255322async function startProfile (
@@ -328,6 +395,32 @@ type SubcommandHandler = (
328395 emit : Emit ,
329396) => Promise < void > ;
330397
398+ function requireComponent (
399+ analysis : ReactProfileAnalysis ,
400+ name : string | undefined ,
401+ ) : ComponentProfile {
402+ if ( ! name )
403+ throw new AppError ( 'INVALID_ARGS' , 'react-devtools profile report requires a component name.' ) ;
404+ const component = findComponent ( analysis , name ) ;
405+ if ( ! component ) {
406+ throw new AppError ( 'COMMAND_FAILED' , `No renders of ${ name } in the saved profile.` , {
407+ hint : 'Use a name from react-devtools profile slow or profile rerenders.' ,
408+ } ) ;
409+ }
410+ return component ;
411+ }
412+
413+ function requireCommit ( analysis : ReactProfileAnalysis , target : string | undefined ) : CommitProfile {
414+ const digits = ( target ?? '' ) . replace ( / ^ # / , '' ) ;
415+ const commit = / ^ \d + $ / . test ( digits ) ? analysis . commits [ Number ( digits ) ] : undefined ;
416+ if ( ! commit ) {
417+ throw new AppError ( 'INVALID_ARGS' , `No commit ${ target ?? '(none)' } in the saved profile.` , {
418+ hint : `Use a commit index from 0 to ${ Math . max ( analysis . commits . length - 1 , 0 ) } .` ,
419+ } ) ;
420+ }
421+ return commit ;
422+ }
423+
331424const SUBCOMMAND_HANDLERS : Record < ReactProfileSubcommand , SubcommandHandler > = {
332425 start : async ( args , options , emit ) => {
333426 const started = await startProfile ( args , options ) ;
@@ -354,6 +447,36 @@ const SUBCOMMAND_HANDLERS: Record<ReactProfileSubcommand, SubcommandHandler> = {
354447 savedTo : savedProfilePath ( options ) ,
355448 } ) ;
356449 } ,
450+ slow : async ( args , options , emit ) => {
451+ const { analysis } = readSavedProfile ( options ) ;
452+ const limit = args . limit ?? 10 ;
453+ emit ( formatSlowComponents ( analysis , limit ) , analysis . components . slice ( 0 , limit ) ) ;
454+ } ,
455+ rerenders : async ( args , options , emit ) => {
456+ const { analysis } = readSavedProfile ( options ) ;
457+ const limit = args . limit ?? 10 ;
458+ emit ( formatRerenders ( analysis , limit ) , componentsByRenders ( analysis ) . slice ( 0 , limit ) ) ;
459+ } ,
460+ report : async ( args , options , emit ) => {
461+ const { analysis } = readSavedProfile ( options ) ;
462+ const component = requireComponent ( analysis , args . target ) ;
463+ emit ( formatComponentReport ( analysis , component ) , component ) ;
464+ } ,
465+ timeline : async ( args , options , emit ) => {
466+ const { analysis } = readSavedProfile ( options ) ;
467+ const limit = args . limit ?? 20 ;
468+ const byDuration = args . sort === 'duration' ;
469+ emit (
470+ formatTimeline ( analysis , limit , byDuration ) ,
471+ timelineCommits ( analysis , byDuration ) . slice ( 0 , limit ) ,
472+ ) ;
473+ } ,
474+ commit : async ( args , options , emit ) => {
475+ const { analysis } = readSavedProfile ( options ) ;
476+ const commit = requireCommit ( analysis , args . target ) ;
477+ const limit = args . limit ?? 10 ;
478+ emit ( formatCommit ( commit , limit ) , { ...commit , top : commit . top . slice ( 0 , limit ) } ) ;
479+ } ,
357480} ;
358481
359482export async function runReactProfileCommand (
0 commit comments