|
| 1 | +const path = require('path'); |
| 2 | +const globby = require('globby'); |
| 3 | + |
| 4 | +/** |
| 5 | + * Generates a lookup name for a backing js class. |
| 6 | + * @param {*} matchedItem |
| 7 | + * @param {*} fileName |
| 8 | + * @param {*} type |
| 9 | + */ |
| 10 | +function getDetectedName(matchedItem, fileName, type) { |
| 11 | + let detectedName = null; |
| 12 | + // create regex string to derive the potential addon name and generated the |
| 13 | + // lookup name. |
| 14 | + let regexString = `(.*)/(addon|app)/${type}s(.*)/${fileName}.js`; |
| 15 | + let matchedRegex = matchedItem.match(regexString); |
| 16 | + if (matchedRegex) { |
| 17 | + const folderType = matchedRegex[2]; |
| 18 | + const rootName = matchedRegex[1].split('/').pop(); |
| 19 | + detectedName = |
| 20 | + folderType === 'addon' ? `${rootName}@${type}:${fileName}` : `${type}:${fileName}`; |
| 21 | + } |
| 22 | + return detectedName; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Returns lookup name for a given file path (template file) by searching for a backing |
| 27 | + * js file for the template. |
| 28 | + * @param {*} entry |
| 29 | + */ |
| 30 | +function detectTypeAndName(entry) { |
| 31 | + let detectedComponentName = null; |
| 32 | + let detectedHelperName = null; |
| 33 | + let detectedControllerName = null; |
| 34 | + const fileName = path.basename(entry).split('.')[0]; |
| 35 | + // Since we care about the component and helpers (as we do not want to prefix `this`) and |
| 36 | + // also we would need to generate lookupNames for controllers and components pertaining to the |
| 37 | + // current template file, do a globby search and gather filepaths that match these folders. |
| 38 | + const matched = globby.sync( |
| 39 | + [ |
| 40 | + `**/components/**/${fileName}.js`, |
| 41 | + `**/helpers/**/${fileName}.js`, |
| 42 | + `**/controllers/**/${fileName}.js`, |
| 43 | + ], |
| 44 | + { |
| 45 | + ignore: ['node_modules/**'], |
| 46 | + } |
| 47 | + ); |
| 48 | + if (matched.length) { |
| 49 | + matched.forEach(matchedItem => { |
| 50 | + detectedComponentName = getDetectedName(matchedItem, fileName, 'component'); |
| 51 | + detectedHelperName = getDetectedName(matchedItem, fileName, 'helper'); |
| 52 | + detectedControllerName = getDetectedName(matchedItem, fileName, 'controller'); |
| 53 | + }); |
| 54 | + } |
| 55 | + let detectedName = detectedComponentName || detectedHelperName || detectedControllerName; |
| 56 | + return { lookupName: detectedName, filePath: entry }; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Analyzes the app and collects local properties and type of the lookup name. |
| 61 | + * Returns the map of lookupName to its metadata. |
| 62 | + * { |
| 63 | + * "component:foo": { localProperties: ['foo', 'bar'], type: 'Component', filePath: 'app/components/foo.js' } |
| 64 | + * } |
| 65 | + * @param {*} lookupNames |
| 66 | + * @param {*} appname |
| 67 | + */ |
| 68 | +function appResolver(lookupNames, currAppName) { |
| 69 | + let mapping = {}; |
| 70 | + if (Array.isArray(currAppName)) { |
| 71 | + currAppName.forEach(appItem => { |
| 72 | + try { |
| 73 | + let app = require(`${appItem}/app`).default.create({ autoboot: false }); |
| 74 | + let localProperties = []; |
| 75 | + lookupNames.forEach(item => { |
| 76 | + // Resolve the class from the lookup name, if found, then check if its a helper, component |
| 77 | + // or controller and add the local properties & the type to the map. |
| 78 | + let klass = app.resolveRegistration(item.lookupName); |
| 79 | + if (klass) { |
| 80 | + if (klass.proto) { |
| 81 | + const protoInfo = klass.proto(); |
| 82 | + localProperties = Object.keys(protoInfo).filter( |
| 83 | + key => !['_super', 'actions'].includes(key) |
| 84 | + ); |
| 85 | + /* globals Ember */ |
| 86 | + // Determine the type of the class's instance. |
| 87 | + let klassType = null; |
| 88 | + if (protoInfo instanceof Ember['Controller']) { |
| 89 | + klassType = 'Controller'; |
| 90 | + } else if (protoInfo instanceof Ember['Component']) { |
| 91 | + klassType = 'Component'; |
| 92 | + } |
| 93 | + |
| 94 | + // Create a map with lookupName as key with meta information. |
| 95 | + mapping[item.lookupName] = { |
| 96 | + filePath: item.filePath, |
| 97 | + localProperties, |
| 98 | + type: klassType, |
| 99 | + }; |
| 100 | + } else if (klass.isHelperFactory) { |
| 101 | + mapping[item.lookupName] = { type: 'Helper', filePath: item.filePath }; |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + app.destroy(); |
| 106 | + } catch (e) { |
| 107 | + console.log(e); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + return mapping; |
| 113 | +} |
| 114 | + |
| 115 | +module.exports = { appResolver, detectTypeAndName }; |
0 commit comments