From 84a39f027cd592a748089442222934438d99cd61 Mon Sep 17 00:00:00 2001 From: Raido Kuli Date: Wed, 23 Sep 2020 23:42:01 +0300 Subject: [PATCH] feat: Blueprint removes from app/index.html upon install for FastBoot (#177) * Blueprint removes <title> from app/index.html upon install for FastBoot * Make replacement regular expression more strict --- blueprints/ember-page-title/index.js | 41 +++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/blueprints/ember-page-title/index.js b/blueprints/ember-page-title/index.js index 06bbaa5d..8dcd5ab0 100644 --- a/blueprints/ember-page-title/index.js +++ b/blueprints/ember-page-title/index.js @@ -1,3 +1,42 @@ +'use strict'; + +const path = require('path'); +const fs = require('fs'); + module.exports = { - normalizeEntityName: function () {} + normalizeEntityName() {}, + + afterInstall(opts) { + let project = opts.project; + + let isFastBootPresent = 'ember-cli-fastboot' in project.dependencies(); + + if (isFastBootPresent) { + let isAddon = 'ember-addon' in project.pkg; + + let indexHtmlPath = isAddon ? + path.join(project.root, 'tests', 'dummy', 'app', 'index.html') : + path.join(project.root, 'app', 'index.html'); + + if (fs.existsSync(indexHtmlPath)) { + const contents = fs.readFileSync( + indexHtmlPath, + { + encoding: 'utf8' + } + ); + + const titleMatches = contents.match(/<title>(.*)<\/title>/i); + const title = titleMatches[1] || "Example Title"; + fs.writeFileSync( + indexHtmlPath, + contents.replace(/\s*<title>.*<\/title>/gi, ''), + { + encoding: 'utf8' + } + ); + opts.ui.writeWarnLine(`<title> has been removed from index.html due to ember-cli-fastboot being present, please add {{page-title "${title}"}} to application.hbs`); + } + } + } };