Skip to content

Commit

Permalink
Remove <title> from index.html upon install if ember-page-title is pr…
Browse files Browse the repository at this point in the history
…esent
  • Loading branch information
raido committed Sep 24, 2020
1 parent 1dbd839 commit 8f13be6
Showing 1 changed file with 39 additions and 1 deletion.
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,35 @@ 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>(.*)<\/title>/i);
const title = titleMatches && titleMatches[1] || "Example Title";
writeFileSync(
indexHtmlPath,
contents.replace(/\s*<title>.*<\/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`);
}
}
}
};

0 comments on commit 8f13be6

Please sign in to comment.