Skip to content

Commit 8c98675

Browse files
jrjohnsonTurbo87
authored andcommitted
Detect collocated templates
Look in multiple paths and use the first one that matches for a template. If no paths match then output them all as an error.
1 parent 8a245a5 commit 8c98675

3 files changed

Lines changed: 49 additions & 33 deletions

File tree

lib/__tests__/guess-template.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/__tests__/template-paths.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const { possibleTemplatePaths } = require('../transform');
2+
3+
describe('possibleTemplatePaths()', () => {
4+
const TESTS = [
5+
[
6+
'app/components/checkbox.js',
7+
['app/templates/components/checkbox.hbs', 'app/components/checkbox.hbs'],
8+
],
9+
[
10+
'app/components/nested/sub/checkbox.js',
11+
[
12+
'app/templates/components/nested/sub/checkbox.hbs',
13+
'app/components/nested/sub/checkbox.hbs',
14+
],
15+
],
16+
['app/components/checkbox/component.js', ['app/components/checkbox/template.hbs']],
17+
[
18+
'app/components/nested/sub/component/component.js',
19+
['app/components/nested/sub/component/template.hbs'],
20+
],
21+
[
22+
'app/components/checkbox.ts',
23+
['app/templates/components/checkbox.hbs', 'app/components/checkbox.hbs'],
24+
],
25+
['app/components/checkbox/component.ts', ['app/components/checkbox/template.hbs']],
26+
[
27+
'app/components/nested/sub/component/component.ts',
28+
['app/components/nested/sub/component/template.hbs'],
29+
],
30+
];
31+
32+
for (let [input, expected] of TESTS) {
33+
test(input, () => {
34+
expect(possibleTemplatePaths(input)).toEqual(expected);
35+
});
36+
}
37+
});

lib/transform.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ const transformTemplate = require('./transform/template');
1111
function transformPath(componentPath, options) {
1212
let debug = (fmt, ...args) => _debug(`${componentPath}: ${fmt}`, ...args);
1313

14-
let templatePath = guessTemplatePath(componentPath);
15-
if (!fs.existsSync(templatePath)) {
16-
throw new SilentError(`Could not find template at ${templatePath}`);
14+
let templatePaths = possibleTemplatePaths(componentPath);
15+
let templatePath = templatePaths.find(fs.existsSync);
16+
if (!templatePath) {
17+
let where = templatePaths.join(' or ');
18+
throw new SilentError(`Could not find template at ${where}`);
1719
}
1820
debug('templatePath: %o', templatePath);
1921

@@ -99,19 +101,20 @@ function transform(source, template, options = {}) {
99101
return { source, template };
100102
}
101103

102-
function guessTemplatePath(componentPath) {
104+
function possibleTemplatePaths(componentPath) {
103105
let isPods = path.basename(componentPath, path.extname(componentPath)) === 'component';
104106
if (isPods) {
105-
return path.dirname(componentPath) + '/template.hbs';
107+
return [path.dirname(componentPath) + '/template.hbs'];
106108
}
107109

108-
return componentPath
109-
.replace('/components/', '/templates/components/')
110-
.replace(/\.(js|ts)$/, '.hbs');
110+
return [
111+
componentPath.replace('/components/', '/templates/components/').replace(/\.(js|ts)$/, '.hbs'),
112+
componentPath.replace(/\.(js|ts)$/, '.hbs'),
113+
];
111114
}
112115

113116
module.exports = {
114117
transform,
115118
transformPath,
116-
guessTemplatePath,
119+
possibleTemplatePaths,
117120
};

0 commit comments

Comments
 (0)