@@ -5,16 +5,31 @@ const cache = require('../../../lib/cache');
55
66const telemetry = cache . has ( 'telemetry' ) ? JSON . parse ( cache . get ( 'telemetry' ) . value ) : { } ;
77const ADDON_PATHS = { } ;
8+ const APP_PATHS = { } ;
89
910let packagePaths = walkSync ( './' , {
1011 globs : [ '**/package.json' ] ,
1112 ignore : [ 'node_modules/**' ] ,
1213} ) ;
1314
1415for ( let packagePath of packagePaths ) {
15- let { name } = fs . readJsonSync ( packagePath ) ;
16+ let pkg = fs . readJsonSync ( packagePath ) ;
1617
17- ADDON_PATHS [ path . dirname ( path . resolve ( '.' , packagePath ) ) ] = name ;
18+ let packageDir = path . dirname ( path . resolve ( '.' , packagePath ) ) ;
19+
20+ if ( pkg . keywords && pkg . keywords . includes ( 'ember-addon' ) ) {
21+ ADDON_PATHS [ packageDir ] = pkg . name ;
22+ } else if ( isEmberCliProject ( pkg ) ) {
23+ APP_PATHS [ packageDir ] = pkg . name ;
24+ }
25+ }
26+
27+ function isEmberCliProject ( pkg ) {
28+ return (
29+ pkg &&
30+ ( ( pkg . dependencies && Object . keys ( pkg . dependencies ) . indexOf ( 'ember-cli' ) !== - 1 ) ||
31+ ( pkg . devDependencies && Object . keys ( pkg . devDependencies ) . indexOf ( 'ember-cli' ) !== - 1 ) )
32+ ) ;
1833}
1934
2035/**
@@ -23,27 +38,63 @@ for (let packagePath of packagePaths) {
2338 * @param {String } filePath the path on disk (from current working directory)
2439 * @returns {String } The in-browser module path for the specified filePath
2540 */
26- function getModulePathFor ( filePath , addonPaths = ADDON_PATHS ) {
27- let fileSegments = filePath . split ( '/' ) ;
28- let addonSegments = [ ] ;
41+ function getModulePathFor ( filePath , addonPaths = ADDON_PATHS , appPaths = APP_PATHS ) {
42+ let bestMatch = '' ;
43+ let moduleNameRoot , relativePath , isApp , result ;
2944
30- while ( fileSegments . length > 0 ) {
31- addonSegments . push ( fileSegments . shift ( ) ) ;
45+ for ( let addonPath in addonPaths ) {
46+ if ( filePath . startsWith ( addonPath ) && addonPath . length > bestMatch . length ) {
47+ bestMatch = addonPath ;
48+ moduleNameRoot = addonPaths [ addonPath ] ;
49+ relativePath = filePath . slice (
50+ addonPath . length + 1 /* for slash */ ,
51+ - path . extname ( filePath ) . length
52+ ) ;
53+ }
54+ }
3255
33- if ( addonPaths [ addonSegments . join ( '/' ) ] ) {
34- break ;
56+ for ( let appPath in appPaths ) {
57+ if ( filePath . startsWith ( appPath ) && appPath . length > bestMatch . length ) {
58+ bestMatch = appPath ;
59+ moduleNameRoot = appPaths [ appPath ] ;
60+ relativePath = filePath . slice (
61+ appPath . length + 1 /* for slash */ ,
62+ - path . extname ( filePath ) . length
63+ ) ;
64+ isApp = true ;
3565 }
3666 }
3767
38- let addonFilePath = addonSegments . join ( '/' ) ;
39- let addonName = addonPaths [ addonFilePath ] ;
68+ if ( ! relativePath && process . cwd ( ) === path . resolve ( __dirname , '../../..' ) ) {
69+ // this is pretty odd, but our tests in
70+ // transforms/ember-object/__testfixtures__ don't actually live in an ember
71+ // app or addon, so the standard logic above doesn't work for them
72+ //
73+ // this works by passing through the input file name when we are operating
74+ // on the local ember-es6-class-codemod repo **and** we were not able to
75+ // resolve a relativePath via normal means
76+ return filePath . replace ( / \. [ ^ / . ] + $ / , '' ) ;
77+ }
78+
79+ if ( ! relativePath ) {
80+ return ;
81+ }
4082
41- let relativeFilePath = fileSegments
42- . join ( '/' )
43- . replace ( / ^ ( a d d o n | a p p ) \/ / , '' )
44- . replace ( / \. [ ^ / . ] + $ / , '' ) ;
83+ if ( isApp ) {
84+ if ( relativePath . startsWith ( 'app' ) ) {
85+ result = `${ moduleNameRoot } ${ relativePath . slice ( 3 ) } ` ;
86+ } else if ( relativePath . startsWith ( 'tests' ) ) {
87+ result = `${ moduleNameRoot } /${ relativePath } ` ;
88+ }
89+ } else {
90+ if ( relativePath . startsWith ( 'addon-test-support' ) ) {
91+ result = `${ moduleNameRoot } /test-support${ relativePath . slice ( 18 ) } ` ;
92+ } else if ( relativePath . startsWith ( 'addon' ) ) {
93+ result = `${ moduleNameRoot } ${ relativePath . slice ( 5 ) } ` ;
94+ }
95+ }
4596
46- return ` ${ addonName } / ${ relativeFilePath } ` ;
97+ return result ;
4798}
4899
49100/**
@@ -54,8 +105,9 @@ function getModulePathFor(filePath, addonPaths = ADDON_PATHS) {
54105 */
55106function getTelemetryFor ( filePath ) {
56107 let modulePath = getModulePathFor ( filePath ) ;
108+ let data = telemetry [ modulePath ] ;
57109
58- return telemetry [ modulePath ] ;
110+ return data ;
59111}
60112
61113module . exports = {
0 commit comments