-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapplication-template-wrapper.js
125 lines (96 loc) · 3.28 KB
/
application-template-wrapper.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
/* eslint-disable no-console */
'use strict';
const chalk = require('chalk');
const fs = require('fs');
const p = require('util').promisify;
const path = require('path');
const strip = require('../utils').strip;
module.exports = {
description:
'Wrap the top-level application template (application.hbs) with a `<div class="ember-view">` element.',
url: 'https://github.com/emberjs/rfcs/pull/280',
default: true,
since: '3.1.0',
callback: async function (project, value, shouldRunCodemod) {
if (value !== false) {
return;
}
let root = project.root;
let prefix = project.config().podModulePrefix;
let isPod = !!prefix;
let templatePath, originalContent;
if (isPod) {
// TODO
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`
)
);
return;
} else {
templatePath = 'app/templates/application.hbs';
}
let absolutePath = path.join(root, templatePath);
try {
originalContent = await p(fs.readFile)(absolutePath, {
encoding: 'UTF-8',
});
} catch (err) {
if (err.code === 'ENOENT') {
return;
} else {
throw err;
}
}
if (shouldRunCodemod === undefined) {
console.log(strip`
Disabling ${chalk.bold('application-template-wrapper')}...
This will remove the \`<div class="ember-view">\` wrapper for the top-level application template (\`${templatePath}\`).
While this may be a desirable change, it might break your styling in subtle ways:
- Perhaps you were targeting the \`.ember-view\` class in your CSS.
- Perhaps you were targeting the \`<div>\` element in your CSS (e.g. \`body > div > .some-child\`).
- Depending on your choice of \`rootElement\`, your app might not be wrapped inside a block-level element anymore.
For more information, see ${chalk.underline(
'https://github.com/emberjs/rfcs/pull/280'
)}.
To be very conservative, I could add the \`<div class="ember-view">\` wrapper to your application.hbs. (You can always remove it later.)
`);
let response = await require('inquirer').prompt({
type: 'confirm',
name: 'shouldRewrite',
message: 'Would you like me to do that for you?',
default: false,
});
console.log();
shouldRunCodemod = response.shouldRewrite;
}
if (shouldRunCodemod) {
let lines = originalContent.split('\n');
let hasFinalNewLine;
let lastLine = lines.pop();
if (lastLine === '') {
hasFinalNewLine = true;
} else {
// put it back
lines.push(lastLine);
hasFinalNewLine = false;
}
let content = [];
content.push('<div class="ember-view">');
lines.forEach((line) => {
content.push(line === '' ? '' : ` ${line}`);
});
content.push('</div>');
if (hasFinalNewLine) {
content.push('');
}
console.log(` ${chalk.yellow('overwrite')} ${templatePath}`);
await p(fs.writeFile)(templatePath, content.join('\n'), {
encoding: 'UTF-8',
});
console.log();
}
},
};