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

Blueprint removes <title> from app/index.html upon install for FastBoot #177

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion blueprints/ember-page-title/index.js
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Contributor Author

@raido raido Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works, maybe there are some other well formed ways to do edits like this from blueprints?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of any, but @rwjblue might be more familiar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, we could use ember-template-recast here but that has issues with <DOCTYPE.

I think (since <title> doesn't allow nesting, and the location is stable/knowable) that this is fine...

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`);
}
}
}
};