|
1 | 1 | const fs = require('fs'); |
| 2 | +const path = require('path'); |
2 | 3 | const stringUtils = require('ember-cli-string-utils'); |
3 | 4 | const j = require('jscodeshift').withParser('ts'); |
4 | 5 | const debug = require('debug')('tagless-ember-components-codemod'); |
@@ -120,8 +121,11 @@ function transform(componentPath) { |
120 | 121 | let classNameBindings = findClassNameBindings(properties); |
121 | 122 | debug(`${componentPath}: classNameBindings: %o`, classNameBindings); |
122 | 123 |
|
123 | | - // TODO find corresponding template files |
124 | | - // TODO skip if not found (warn) |
| 124 | + let templatePath = guessTemplatePath(componentPath); |
| 125 | + if (!fs.existsSync(templatePath)) { |
| 126 | + throw new SilentError(`Could not find template at ${templatePath}`); |
| 127 | + } |
| 128 | + debug(`${componentPath}: templatePath: %o`, templatePath); |
125 | 129 |
|
126 | 130 | // TODO set `tagName: ''` and remove `attributeBindings`, `classNames`, ... |
127 | 131 | // TODO wrap existing template with root element |
@@ -212,11 +216,21 @@ function findClassNameBindings(properties) { |
212 | 216 | return classNameBindings; |
213 | 217 | } |
214 | 218 |
|
| 219 | +function guessTemplatePath(componentPath) { |
| 220 | + let isPods = path.basename(componentPath) === 'component.js'; |
| 221 | + if (isPods) { |
| 222 | + return path.dirname(componentPath) + '/template.hbs'; |
| 223 | + } |
| 224 | + |
| 225 | + return componentPath.replace('/components/', '/templates/components/').replace(/\.js$/, '.hbs'); |
| 226 | +} |
| 227 | + |
215 | 228 | module.exports = { |
216 | 229 | transform, |
217 | 230 | findTagName, |
218 | 231 | findElementId, |
219 | 232 | findAttributeBindings, |
220 | 233 | findClassNames, |
221 | 234 | findClassNameBindings, |
| 235 | + guessTemplatePath, |
222 | 236 | }; |
0 commit comments