1 |
|
2 | 'use strict';
|
3 |
|
4 | const chalk = require('chalk');
|
5 | const fs = require('fs');
|
6 | const inquirer = require('inquirer');
|
7 | const p = require('util').promisify;
|
8 | const path = require('path');
|
9 | const strip = require('../utils').strip;
|
10 |
|
11 | module.exports = {
|
12 | description:
|
13 | 'Wrap the top-level application template (application.hbs) with a `<div class="ember-view">` element.',
|
14 | url: 'https://github.com/emberjs/rfcs/pull/280',
|
15 | default: true,
|
16 | since: '3.1.0',
|
17 | callback: async function (project, value, shouldRunCodemod) {
|
18 | if (value !== false) {
|
19 | return;
|
20 | }
|
21 |
|
22 | let root = project.root;
|
23 | let prefix = project.config().podModulePrefix;
|
24 | let isPod = !!prefix;
|
25 |
|
26 | let templatePath, originalContent;
|
27 |
|
28 | if (isPod) {
|
29 |
|
30 | console.log(
|
31 | chalk.yellow(
|
32 | `${chalk.bold(
|
33 | 'Note:'
|
34 | )} There is an automated refactor script available for this feature, but it does not currently support "pod" apps. PRs welcome!\n`
|
35 | )
|
36 | );
|
37 | return;
|
38 | } else {
|
39 | templatePath = 'app/templates/application.hbs';
|
40 | }
|
41 |
|
42 | let absolutePath = path.join(root, templatePath);
|
43 |
|
44 | try {
|
45 | originalContent = await p(fs.readFile)(absolutePath, {
|
46 | encoding: 'UTF-8',
|
47 | });
|
48 | } catch (err) {
|
49 | if (err.code === 'ENOENT') {
|
50 | return;
|
51 | } else {
|
52 | throw err;
|
53 | }
|
54 | }
|
55 |
|
56 | if (shouldRunCodemod === undefined) {
|
57 | console.log(strip`
|
58 | Disabling ${chalk.bold('application-template-wrapper')}...
|
59 |
|
60 | This will remove the \`<div class="ember-view">\` wrapper for the top-level application template (\`${templatePath}\`).
|
61 |
|
62 | While this may be a desirable change, it might break your styling in subtle ways:
|
63 |
|
64 | - Perhaps you were targeting the \`.ember-view\` class in your CSS.
|
65 |
|
66 | - Perhaps you were targeting the \`<div>\` element in your CSS (e.g. \`body > div > .some-child\`).
|
67 |
|
68 | - Depending on your choice of \`rootElement\`, your app might not be wrapped inside a block-level element anymore.
|
69 |
|
70 | For more information, see ${chalk.underline(
|
71 | 'https://github.com/emberjs/rfcs/pull/280'
|
72 | )}.
|
73 |
|
74 | To be very conservative, I could add the \`<div class="ember-view">\` wrapper to your application.hbs. (You can always remove it later.)
|
75 | `);
|
76 |
|
77 | let response = await inquirer.prompt({
|
78 | type: 'confirm',
|
79 | name: 'shouldRewrite',
|
80 | message: 'Would you like me to do that for you?',
|
81 | default: false,
|
82 | });
|
83 |
|
84 | console.log();
|
85 |
|
86 | shouldRunCodemod = response.shouldRewrite;
|
87 | }
|
88 |
|
89 | if (shouldRunCodemod) {
|
90 | let lines = originalContent.split('\n');
|
91 | let hasFinalNewLine;
|
92 |
|
93 | let lastLine = lines.pop();
|
94 |
|
95 | if (lastLine === '') {
|
96 | hasFinalNewLine = true;
|
97 | } else {
|
98 |
|
99 | lines.push(lastLine);
|
100 | hasFinalNewLine = false;
|
101 | }
|
102 |
|
103 | let content = [];
|
104 |
|
105 | content.push('<div class="ember-view">');
|
106 |
|
107 | lines.forEach((line) => {
|
108 | content.push(line === '' ? '' : ` ${line}`);
|
109 | });
|
110 |
|
111 | content.push('</div>');
|
112 |
|
113 | if (hasFinalNewLine) {
|
114 | content.push('');
|
115 | }
|
116 |
|
117 | console.log(` ${chalk.yellow('overwrite')} ${templatePath}`);
|
118 |
|
119 | await p(fs.writeFile)(templatePath, content.join('\n'), {
|
120 | encoding: 'UTF-8',
|
121 | });
|
122 |
|
123 | console.log();
|
124 | }
|
125 | },
|
126 | };
|