-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type-safe event handler for surfacing runtime info in CLI
- Loading branch information
Showing
8 changed files
with
232 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { onDebugEvent } from './debug'; | ||
import prettyBytes from 'pretty-bytes'; | ||
import prettyMS from 'pretty-ms'; | ||
|
||
export function initLogListener() { | ||
onDebugEvent(event => { | ||
switch (event.type) { | ||
case 'collectStoreData:start': { | ||
console.log( | ||
'Collecting data about themes and modules from the store', | ||
); | ||
break; | ||
} | ||
case 'collectStoreData:end': { | ||
const { timing } = event; | ||
const total = timing.end - timing.start; | ||
|
||
console.log(`Data collection completed in ${prettyMS(total)}`); | ||
break; | ||
} | ||
case 'eligibleThemes': { | ||
const themeLines = event.payload.map(n => ` - ${n}`); | ||
const themeLabel = themeLines.length > 1 ? 'themes' : 'theme'; | ||
console.log( | ||
[ | ||
`Found ${event.payload.length} eligible ${themeLabel} to optimize\n`, | ||
...themeLines, | ||
].join(''), | ||
); | ||
break; | ||
} | ||
case 'createBundle:start': { | ||
const { bundleName, themeID, deps } = event; | ||
console.log( | ||
`Starting to bundle ${bundleName} for ${themeID} with ${deps.length} dependencies`, | ||
); | ||
break; | ||
} | ||
case 'invalidShims': { | ||
const { themeID, deps } = event; | ||
const shimLines = deps.map(d => ` - ${d}\n`); | ||
console.log( | ||
[ | ||
'One or more invalid shim configurations were found ', | ||
`while bundling ${themeID}. RequireJS does not support `, | ||
'shim configuration for modules that already call "define". ', | ||
'You may have unexpected results when running the bundle in ', | ||
`your store\n Invalid shims for:\n`, | ||
...shimLines, | ||
].join(''), | ||
); | ||
break; | ||
} | ||
case 'createBundle:end': { | ||
const { timing, themeID, bundleName, bundleSize } = event; | ||
const total = timing.end - timing.start; | ||
const size = prettyBytes(bundleSize); | ||
|
||
console.log( | ||
`Finished bundling ${bundleName} (${size} unminified) ` + | ||
`for ${themeID} in ${prettyMS(total)}`, | ||
); | ||
break; | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { StoreData } from './types'; | ||
|
||
type EventTiming = { start: number; end: number }; | ||
|
||
type BalerDebugEvent = | ||
| { type: 'collectStoreData:start' } | ||
| { | ||
type: 'collectStoreData:end'; | ||
storeData: StoreData; | ||
timing: EventTiming; | ||
} | ||
| { type: 'eligibleThemes'; payload: string[] } | ||
| { | ||
type: 'createBundle:start'; | ||
themeID: string; | ||
bundleName: string; | ||
deps: string[]; | ||
} | ||
| { | ||
type: 'invalidShims'; | ||
themeID: string; | ||
deps: string[]; | ||
} | ||
| { | ||
type: 'createBundle:end'; | ||
themeID: string; | ||
bundleName: string; | ||
bundleSize: number; | ||
timing: EventTiming; | ||
}; | ||
|
||
type DebugEventHandler = (event: BalerDebugEvent) => void; | ||
|
||
const events: BalerDebugEvent[] = []; | ||
|
||
const handlers: Set<DebugEventHandler> = new Set(); | ||
|
||
/** | ||
* @summary Logs data that may be useful to a user of Baler. | ||
* Currently surfaced via specific CLI flags | ||
*/ | ||
export function debugEvent(event: BalerDebugEvent) { | ||
events.push(event); | ||
for (const handler of handlers) { | ||
handler(event); | ||
} | ||
} | ||
|
||
export function debugTimer(): () => EventTiming { | ||
const start = Date.now(); | ||
return () => { | ||
const end = Date.now(); | ||
return { start, end }; | ||
}; | ||
} | ||
|
||
/** | ||
* @summary Get a list of all debug events | ||
*/ | ||
export function getEvents() { | ||
return events.slice(); | ||
} | ||
|
||
/** | ||
* @summary Register a handler to be called whenever a new | ||
* debug event is dispatched | ||
*/ | ||
export function onDebugEvent(handler: DebugEventHandler) { | ||
handlers.add(handler); | ||
return () => { | ||
handlers.delete(handler); | ||
}; | ||
} |
Oops, something went wrong.