1
+ 'use strict' ;
2
+
1
3
/* eslint-env node */
4
+ const path = require ( 'path' ) ;
2
5
const recast = require ( 'recast' ) ;
3
- const { readFileSync, writeFileSync } = require ( 'fs' ) ;
6
+ const { readFileSync, writeFileSync, existsSync } = require ( 'fs' ) ;
4
7
5
8
module . exports = {
6
9
description : '' ,
@@ -9,6 +12,11 @@ module.exports = {
9
12
} ,
10
13
11
14
afterInstall ( ) {
15
+ this . updateBabelTargets ( ) ;
16
+ this . removeTitleFromIndexHtml ( ) ;
17
+ } ,
18
+
19
+ updateBabelTargets ( ) {
12
20
let targetsFile = './config/targets.js'
13
21
14
22
if ( this . project . isEmberCLIAddon ( ) ) {
@@ -40,5 +48,37 @@ module.exports = {
40
48
} ) ;
41
49
42
50
writeFileSync ( targetsFile , recast . print ( targetsAst , { tabWidth : 2 , quote : 'single' } ) . code ) ;
51
+ } ,
52
+
53
+ removeTitleFromIndexHtml ( ) {
54
+ let isEmberPageTitlePresent = 'ember-page-title' in this . project . dependencies ( ) ;
55
+
56
+ if ( isEmberPageTitlePresent ) {
57
+ let indexHtmlPath = this . project . isEmberCLIAddon ( ) ?
58
+ path . join ( this . project . root , 'tests' , 'dummy' , 'app' , 'index.html' ) :
59
+ path . join ( this . project . root , 'app' , 'index.html' ) ;
60
+
61
+ if ( existsSync ( indexHtmlPath ) ) {
62
+ const contents = readFileSync (
63
+ indexHtmlPath ,
64
+ {
65
+ encoding : 'utf8'
66
+ }
67
+ ) ;
68
+
69
+ const titleMatches = contents . match ( / < t i t l e > \s * ( .* ) \s * < \/ t i t l e > / i) ;
70
+ const title = titleMatches && titleMatches [ 1 ] || "Example Title" ;
71
+ if ( titleMatches ) {
72
+ writeFileSync (
73
+ indexHtmlPath ,
74
+ contents . replace ( / \s * < t i t l e > \s * .* \s * < \/ t i t l e > / gi, '' ) ,
75
+ {
76
+ encoding : 'utf8'
77
+ }
78
+ ) ;
79
+ }
80
+ 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` ) ;
81
+ }
82
+ }
43
83
}
44
84
} ;
0 commit comments