Skip to content

Commit 226c956

Browse files
authored
template-only-glimmer-components: Add support for pods and mixed layouts (#97)
template-only-glimmer-components: Add support for pods and mixed layouts
2 parents f905af0 + ce248a2 commit 226c956

File tree

2 files changed

+288
-91
lines changed

2 files changed

+288
-91
lines changed

features/template-only-glimmer-components.js

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,78 @@ module.exports = {
3232
}
3333

3434
let root = project.root;
35-
let prefix = project.config().podModulePrefix;
36-
let isPod = !!prefix;
35+
let projectConfig = project.config();
36+
37+
let { modulePrefix, podModulePrefix } = projectConfig;
38+
let podsFolder;
39+
if (podModulePrefix) {
40+
if (!modulePrefix || !podModulePrefix.startsWith(`${modulePrefix}/`)) {
41+
console.log(chalk.yellow(`${chalk.bold('Note:')} There is an automated refactor script available for this feature, but your \`podModulePrefix\` could not be processed correctly.\n`));
42+
return;
43+
}
44+
45+
podsFolder = podModulePrefix.slice(modulePrefix.length + 1);
46+
if (!podsFolder) {
47+
console.log(chalk.yellow(`${chalk.bold('Note:')} There is an automated refactor script available for this feature, but your \`podModulePrefix\` could not be processed correctly.\n`));
48+
return;
49+
}
50+
}
3751

3852
let templates = [];
3953
let components = [];
4054

41-
if (isPod) {
42-
// TODO
43-
console.log(chalk.yellow(`${chalk.bold('Note:')} There is an automated refactor script available for this feature, but it does not currently support "pod" apps. PRs welcome!\n`));
44-
return;
45-
} else {
46-
let templatesRoot = path.join(root, 'app/templates/components');
47-
let templateCandidates = yield p(glob)('**/*.hbs', { cwd: templatesRoot });
48-
49-
let componentsRoot = path.join(root, 'app/components');
50-
let componentCandidates = yield p(glob)('**/*.@(j|t)s', { cwd: componentsRoot });
51-
52-
templateCandidates.forEach(templatePath => {
53-
let jsPath = templatePath.replace(/\.hbs$/, '.js');
54-
let tsPath = templatePath.replace(/\.hbs$/, '.ts');
55-
56-
let foundJs = componentCandidates.indexOf(jsPath) >= 0;
57-
let foundTs = componentCandidates.indexOf(tsPath) >= 0;
58-
if (!foundJs && !foundTs) {
59-
templates.push(path.join('app/templates/components', templatePath));
60-
components.push(path.join('app/components', jsPath)); // Always offer to create JS
61-
}
62-
});
63-
}
55+
// Handle "Classic" layout
56+
let templatesRoot = path.join(root, 'app/templates/components');
57+
let templateCandidates = yield p(glob)('**/*.hbs', { cwd: templatesRoot });
58+
59+
templateCandidates.forEach(template => {
60+
let templatePath = path.join('app/templates/components', template);
61+
62+
let jsPath = path.join('app/components', template.replace(/\.hbs$/, '.js'));
63+
if (fs.existsSync(path.join(root, jsPath))) return;
64+
65+
let tsPath = path.join('app/components', template.replace(/\.hbs$/, '.ts'));
66+
if (fs.existsSync(path.join(root, tsPath))) return;
67+
68+
templates.push(templatePath);
69+
components.push(jsPath); // Always offer to create JS
70+
});
71+
72+
// Handle "Pods" layout without prefix
73+
74+
let componentsRoot = path.join(root, 'app/components');
75+
templateCandidates = yield p(glob)('**/template.hbs', { cwd: componentsRoot });
76+
77+
templateCandidates.forEach(template => {
78+
let templatePath = path.join('app/components', template);
79+
80+
let jsPath = path.join('app/components', template.replace(/template\.hbs$/, 'component.js'));
81+
if (fs.existsSync(path.join(root, jsPath))) return;
82+
83+
let tsPath = path.join('app/components', template.replace(/template\.hbs$/, 'component.ts'));
84+
if (fs.existsSync(path.join(root, tsPath))) return;
85+
86+
templates.push(templatePath);
87+
components.push(jsPath); // Always offer to create JS
88+
});
89+
90+
// Handle "Pods" layout *with* prefix
91+
92+
componentsRoot = path.join(root, `app/${podsFolder}/components`);
93+
templateCandidates = yield p(glob)('**/template.hbs', { cwd: componentsRoot });
94+
95+
templateCandidates.forEach(template => {
96+
let templatePath = path.join(`app/${podsFolder}/components`, template);
97+
98+
let jsPath = path.join(`app/${podsFolder}/components`, template.replace(/template\.hbs$/, 'component.js'));
99+
if (fs.existsSync(path.join(root, jsPath))) return;
100+
101+
let tsPath = path.join(`app/${podsFolder}/components`, template.replace(/template\.hbs$/, 'component.ts'));
102+
if (fs.existsSync(path.join(root, tsPath))) return;
103+
104+
templates.push(templatePath);
105+
components.push(jsPath); // Always offer to create JS
106+
});
64107

65108
if (templates.length === 0) {
66109
return;

0 commit comments

Comments
 (0)