-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
template-only-glimmer-components.js
209 lines (166 loc) · 6.44 KB
/
template-only-glimmer-components.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint-disable no-console */
'use strict';
const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const mkdirp = require('mkdirp');
const p = require('util').promisify;
const path = require('path');
const strip = require('../utils').strip;
const ComponentFile = strip`
import Component from '@ember/component';
export default Component.extend({
});
`;
/* This forces strip`` to start counting the indentaton */
const INDENT_START = '';
module.exports = {
description:
'Use Glimmer Components semantics for template-only components (component templates with no corresponding .js file).',
url: 'https://github.com/emberjs/rfcs/pull/278',
default: false,
since: '3.1.0',
callback: async function (project, value, shouldRunCodemod) {
if (value !== true) {
return;
}
let root = project.root;
let projectConfig = project.config();
let { modulePrefix, podModulePrefix } = projectConfig;
let podsFolder;
if (podModulePrefix) {
if (!modulePrefix || !podModulePrefix.startsWith(`${modulePrefix}/`)) {
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`
)
);
return;
}
podsFolder = podModulePrefix.slice(modulePrefix.length + 1);
if (!podsFolder) {
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`
)
);
return;
}
}
let templates = [];
let components = [];
// Handle "Classic" layout
let templatesRoot = path.join(root, 'app/templates/components');
let templateCandidates = await p(glob)('**/*.hbs', { cwd: templatesRoot });
templateCandidates.forEach((template) => {
let templatePath = path.join('app/templates/components', template);
let jsPath = path.join(
'app/components',
template.replace(/\.hbs$/, '.js')
);
if (fs.existsSync(path.join(root, jsPath))) return;
let tsPath = path.join(
'app/components',
template.replace(/\.hbs$/, '.ts')
);
if (fs.existsSync(path.join(root, tsPath))) return;
templates.push(templatePath);
components.push(jsPath); // Always offer to create JS
});
// Handle "Pods" layout without prefix
let componentsRoot = path.join(root, 'app/components');
templateCandidates = await p(glob)('**/template.hbs', {
cwd: componentsRoot,
});
templateCandidates.forEach((template) => {
let templatePath = path.join('app/components', template);
let jsPath = path.join(
'app/components',
template.replace(/template\.hbs$/, 'component.js')
);
if (fs.existsSync(path.join(root, jsPath))) return;
let tsPath = path.join(
'app/components',
template.replace(/template\.hbs$/, 'component.ts')
);
if (fs.existsSync(path.join(root, tsPath))) return;
templates.push(templatePath);
components.push(jsPath); // Always offer to create JS
});
// Handle "Pods" layout *with* prefix
componentsRoot = path.join(root, `app/${podsFolder}/components`);
templateCandidates = await p(glob)('**/template.hbs', {
cwd: componentsRoot,
});
templateCandidates.forEach((template) => {
let templatePath = path.join(`app/${podsFolder}/components`, template);
let jsPath = path.join(
`app/${podsFolder}/components`,
template.replace(/template\.hbs$/, 'component.js')
);
if (fs.existsSync(path.join(root, jsPath))) return;
let tsPath = path.join(
`app/${podsFolder}/components`,
template.replace(/template\.hbs$/, 'component.ts')
);
if (fs.existsSync(path.join(root, tsPath))) return;
templates.push(templatePath);
components.push(jsPath); // Always offer to create JS
});
if (templates.length === 0) {
return;
}
if (shouldRunCodemod === undefined) {
console.log(strip`
Enabling ${chalk.bold('template-only-glimmer-components')}...
This will change the semantics for template-only components (components without a \`.js\` file).
Some notable differences include...
- They will not have a component instance, so statements like \`{{this}}\`, \`{{this.foo}}\` and \`{{foo}}\` will be \`null\` or \`undefined\`.
- They will not have a wrapper element: what you have in the template will be what is rendered on the screen.
- Passing classes in the invocation (i.e. \`{{my-component class="..."}}\`) will not work, since there is no wrapper element to apply the classes to.
For more information, see ${chalk.underline(
'https://github.com/emberjs/rfcs/pull/278'
)}.
While these changes may be desirable for ${chalk.italic(
'new components'
)}, they may unexpectedly break the styling or runtime behavior of your ${chalk.italic(
'existing components'
)}.
To be conservative, it is recommended that you add a \`.js\` file for existing template-only components. (You can always delete them later if you aren't relying on the differences.)
The following components are affected:`);
for (let i = 0; i < templates.length; i++) {
console.log(strip`
${INDENT_START}
- ${chalk.underline(templates[i])}
${chalk.gray(
`(Recommendation: add ${chalk.cyan.underline(components[i])})`
)}
`);
}
let response = await require('inquirer').prompt({
type: 'confirm',
name: 'shouldGenerate',
message: 'Would you like me to generate these component files for you?',
default: true,
});
shouldRunCodemod = response.shouldGenerate;
console.log();
}
if (shouldRunCodemod) {
for (let i = 0; i < components.length; i++) {
let componentPath = components[i];
console.log(` ${chalk.green('create')} ${componentPath}`);
let absolutePath = path.join(project.root, componentPath);
await mkdirp(path.dirname(absolutePath));
await p(fs.writeFile)(absolutePath, ComponentFile, {
encoding: 'UTF-8',
});
}
console.log();
}
},
};