UNPKG

3.15 kBJavaScriptView Raw
1/* eslint-disable no-console */
2'use strict';
3
4const chalk = require('chalk');
5const co = require('co');
6const fs = require('fs');
7const inquirer = require('inquirer');
8const p = require('util.promisify');
9const path = require('path');
10const strip = require('../utils').strip;
11
12module.exports = {
13 description: '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: co.wrap(function *(project, value) {
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 // TODO
30 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`));
31 return;
32 } else {
33 templatePath = 'app/templates/application.hbs';
34 }
35
36 let absolutePath = path.join(root, templatePath);
37
38 try {
39 originalContent = yield p(fs.readFile)(absolutePath, { encoding: 'UTF-8' });
40 } catch(err) {
41 if (err.code === 'ENOENT') {
42 return;
43 } else {
44 throw err;
45 }
46 }
47
48 console.log(strip`
49 Disabling ${chalk.bold('application-template-wrapper')}...
50
51 This will remove the \`<div class="ember-view">\` wrapper for the top-level application template (\`${templatePath}\`).
52
53 While this may be a desirable change, it might break your styling in subtle ways:
54
55 - Perhaps you were targeting the \`.ember-view\` class in your CSS.
56
57 - Perhaps you were targeting the \`<div>\` element in your CSS (e.g. \`body > div > .some-child\`).
58
59 - Depending on your choice of \`rootElement\`, your app might not be wrapped inside a block-level element anymore.
60
61 For more information, see ${chalk.underline('https://github.com/emberjs/rfcs/pull/280')}.
62
63 To be very conservative, I could add the \`<div class="ember-view">\` wrapper to your application.hbs. (You can always remove it later.)
64 `);
65
66 let response = yield inquirer.prompt({
67 type: 'confirm',
68 name: 'shouldRewrite',
69 message: 'Would you like me to do that for you?',
70 default: false
71 });
72
73 console.log();
74
75 if (response.shouldRewrite) {
76 let lines = originalContent.split('\n');
77 let hasFinalNewLine;
78
79 let lastLine = lines.pop();
80
81 if (lastLine === '') {
82 hasFinalNewLine = true;
83 } else {
84 // put it back
85 lines.push(lastLine);
86 hasFinalNewLine = false;
87 }
88
89 let content = [];
90
91 content.push('<div class="ember-view">');
92
93 lines.forEach(line => {
94 content.push(line === '' ? '' : ` ${line}`);
95 });
96
97 content.push('</div>');
98
99 if (hasFinalNewLine) {
100 content.push('');
101 }
102
103 console.log(` ${chalk.yellow('overwrite')} ${templatePath}`);
104
105 yield p(fs.writeFile)(templatePath, content.join('\n'), { encoding: 'UTF-8' });
106
107 console.log();
108 }
109 })
110};