@@ -6,7 +6,7 @@ const cache = require("../../../lib/cache");
66const telemetry = cache . has ( "telemetry" )
77 ? JSON . parse ( cache . get ( "telemetry" ) . value )
88 : { } ;
9- const addonFilePaths = { } ;
9+ const ADDON_PATHS = { } ;
1010
1111let packagePaths = walkSync ( "./" , {
1212 globs : [ "**/package.json" ] ,
@@ -16,34 +16,51 @@ let packagePaths = walkSync("./", {
1616for ( let packagePath of packagePaths ) {
1717 let { name } = fs . readJsonSync ( packagePath ) ;
1818
19- addonFilePaths [ path . dirname ( path . resolve ( "." , packagePath ) ) ] = name ;
19+ ADDON_PATHS [ path . dirname ( path . resolve ( "." , packagePath ) ) ] = name ;
2020}
2121
2222/**
23- * Get the runtime data for the file being transformed
23+ * Transforms a literal "on disk" path to a "module path".
2424 *
25- * @param {String } filePath Absolute path of the file to read data from
26- * @returns {Object } Runtime configuration object
25+ * @param {String } filePath the path on disk (from current working directory)
26+ * @returns {String } The in-browser module path for the specified filePath
2727 */
28- module . exports = function getTelemetryData ( filePath ) {
28+ function getModulePathFor ( filePath , addonPaths = ADDON_PATHS ) {
2929 let fileSegments = filePath . split ( "/" ) ;
3030 let addonSegments = [ ] ;
3131
3232 while ( fileSegments . length > 0 ) {
3333 addonSegments . push ( fileSegments . shift ( ) ) ;
3434
35- if ( addonFilePaths [ addonSegments . join ( "/" ) ] ) {
35+ if ( addonPaths [ addonSegments . join ( "/" ) ] ) {
3636 break ;
3737 }
3838 }
3939
4040 let addonFilePath = addonSegments . join ( "/" ) ;
41- let addonName = addonFilePaths [ addonFilePath ] ;
41+ let addonName = addonPaths [ addonFilePath ] ;
4242
4343 let relativeFilePath = fileSegments
4444 . join ( "/" )
4545 . replace ( / ^ ( a d d o n | a p p ) \/ / , "" )
4646 . replace ( / \. [ ^ / . ] + $ / , "" ) ;
4747
48- return telemetry [ `${ addonName } /${ relativeFilePath } ` ] ;
48+ return `${ addonName } /${ relativeFilePath } ` ;
49+ }
50+
51+ /**
52+ * Get the runtime data for the file being transformed
53+ *
54+ * @param {String } filePath Absolute path of the file to read data from
55+ * @returns {Object } Runtime configuration object
56+ */
57+ function getTelemetryFor ( filePath ) {
58+ let modulePath = getModulePathFor ( filePath ) ;
59+
60+ return telemetry [ modulePath ] ;
61+ }
62+
63+ module . exports = {
64+ getTelemetryFor,
65+ getModulePathFor
4966} ;
0 commit comments