Skip to content

Commit 3c0817f

Browse files
authored
Merge pull request #68 from jrjohnson/55-find-colocated
Detect and use collocated templates
2 parents 8a245a5 + 0cea526 commit 3c0817f

3 files changed

Lines changed: 76 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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { possibleTemplatePaths } = require('../transform');
2+
3+
describe('possibleTemplatePaths()', () => {
4+
const TESTS = [
5+
[
6+
'app/components/checkbox.js',
7+
['app/components/checkbox.hbs', 'app/templates/components/checkbox.hbs'],
8+
],
9+
[
10+
'app/components/nested/sub/checkbox.js',
11+
[
12+
'app/components/nested/sub/checkbox.hbs',
13+
'app/templates/components/nested/sub/checkbox.hbs',
14+
],
15+
],
16+
[
17+
'app/components/checkbox/component.js',
18+
[
19+
'app/components/checkbox/template.hbs',
20+
'app/components/checkbox/component.hbs',
21+
'app/templates/components/checkbox/component.hbs',
22+
],
23+
],
24+
[
25+
'app/components/nested/sub/component/component.js',
26+
[
27+
'app/components/nested/sub/component/template.hbs',
28+
'app/components/nested/sub/component/component.hbs',
29+
'app/templates/components/nested/sub/component/component.hbs',
30+
],
31+
],
32+
[
33+
'app/components/checkbox.ts',
34+
['app/components/checkbox.hbs', 'app/templates/components/checkbox.hbs'],
35+
],
36+
[
37+
'app/components/checkbox/component.ts',
38+
[
39+
'app/components/checkbox/template.hbs',
40+
'app/components/checkbox/component.hbs',
41+
'app/templates/components/checkbox/component.hbs',
42+
],
43+
],
44+
[
45+
'app/components/nested/sub/component/component.ts',
46+
[
47+
'app/components/nested/sub/component/template.hbs',
48+
'app/components/nested/sub/component/component.hbs',
49+
'app/templates/components/nested/sub/component/component.hbs',
50+
],
51+
],
52+
];
53+
54+
for (let [input, expected] of TESTS) {
55+
test(input, () => {
56+
expect(possibleTemplatePaths(input)).toEqual(expected);
57+
});
58+
}
59+
});

lib/transform.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ 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+
throw new SilentError('Could not find corresponding template file');
1718
}
1819
debug('templatePath: %o', templatePath);
1920

@@ -99,19 +100,26 @@ function transform(source, template, options = {}) {
99100
return { source, template };
100101
}
101102

102-
function guessTemplatePath(componentPath) {
103+
function possibleTemplatePaths(componentPath) {
104+
let classicTemplatePath = componentPath
105+
.replace('/components/', '/templates/components/')
106+
.replace(/\.(js|ts)$/, '.hbs');
107+
108+
let colocatedTemplatePath = componentPath.replace(/\.(js|ts)$/, '.hbs');
109+
110+
let templatePaths = [colocatedTemplatePath, classicTemplatePath];
111+
103112
let isPods = path.basename(componentPath, path.extname(componentPath)) === 'component';
104113
if (isPods) {
105-
return path.dirname(componentPath) + '/template.hbs';
114+
let podsTemplatePath = path.dirname(componentPath) + '/template.hbs';
115+
templatePaths.unshift(podsTemplatePath);
106116
}
107117

108-
return componentPath
109-
.replace('/components/', '/templates/components/')
110-
.replace(/\.(js|ts)$/, '.hbs');
118+
return templatePaths;
111119
}
112120

113121
module.exports = {
114122
transform,
115123
transformPath,
116-
guessTemplatePath,
124+
possibleTemplatePaths,
117125
};

0 commit comments

Comments
 (0)