Skip to content

Commit 003924a

Browse files
committed
Implement template file detection
1 parent eb1e4ff commit 003924a

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

lib/__tests__/guess-template.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { guessTemplatePath } = require('../transform');
2+
3+
describe('guessTemplatePath()', () => {
4+
const TESTS = [
5+
['app/components/checkbox.js', 'app/templates/components/checkbox.hbs'],
6+
['app/components/checkbox/component.js', 'app/components/checkbox/template.hbs'],
7+
[
8+
'app/components/nested/sub/component/component.js',
9+
'app/components/nested/sub/component/template.hbs',
10+
],
11+
];
12+
13+
for (let [input, expected] of TESTS) {
14+
test(input, () => {
15+
expect(guessTemplatePath(input)).toEqual(expected);
16+
});
17+
}
18+
});

lib/transform.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs');
2+
const path = require('path');
23
const stringUtils = require('ember-cli-string-utils');
34
const j = require('jscodeshift').withParser('ts');
45
const debug = require('debug')('tagless-ember-components-codemod');
@@ -120,8 +121,11 @@ function transform(componentPath) {
120121
let classNameBindings = findClassNameBindings(properties);
121122
debug(`${componentPath}: classNameBindings: %o`, classNameBindings);
122123

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);
125129

126130
// TODO set `tagName: ''` and remove `attributeBindings`, `classNames`, ...
127131
// TODO wrap existing template with root element
@@ -212,11 +216,21 @@ function findClassNameBindings(properties) {
212216
return classNameBindings;
213217
}
214218

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+
215228
module.exports = {
216229
transform,
217230
findTagName,
218231
findElementId,
219232
findAttributeBindings,
220233
findClassNames,
221234
findClassNameBindings,
235+
guessTemplatePath,
222236
};

0 commit comments

Comments
 (0)