Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove <title> from index.html upon install if ember-page-title is present #794

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

/* eslint-env node */
const path = require('path');
const recast = require('recast');
const { readFileSync, writeFileSync } = require('fs');
const { readFileSync, writeFileSync, existsSync } = require('fs');

module.exports = {
description: '',
Expand All @@ -9,6 +12,11 @@ module.exports = {
},

afterInstall() {
this.updateBabelTargets();
this.removeTitleFromIndexHtml();
},

updateBabelTargets() {
let targetsFile = './config/targets.js'

if(this.project.isEmberCLIAddon()) {
Expand Down Expand Up @@ -40,5 +48,37 @@ module.exports = {
});

writeFileSync(targetsFile, recast.print(targetsAst, { tabWidth: 2, quote: 'single' }).code);
},

removeTitleFromIndexHtml() {
let isEmberPageTitlePresent = 'ember-page-title' in this.project.dependencies();

if (isEmberPageTitlePresent) {
let indexHtmlPath = this.project.isEmberCLIAddon() ?
path.join(this.project.root, 'tests', 'dummy', 'app', 'index.html') :
path.join(this.project.root, 'app', 'index.html');

if (existsSync(indexHtmlPath)) {
const contents = readFileSync(
indexHtmlPath,
{
encoding: 'utf8'
}
);

const titleMatches = contents.match(/<title>\s*(.*)\s*<\/title>/i);
const title = titleMatches && titleMatches[1] || "Example Title";
if (titleMatches) {
writeFileSync(
indexHtmlPath,
contents.replace(/\s*<title>\s*.*\s*<\/title>/gi, ''),
{
encoding: 'utf8'
}
);
}
this.ui.writeWarnLine(`<title> has been removed from index.html due to ember-page-title being present, please add {{page-title "${title}"}} to application.hbs`);
}
}
}
};