11/* eslint-env node */
22const recast = require ( 'recast' ) ;
33const { readFileSync, writeFileSync } = require ( 'fs' ) ;
4+ const { join, dirname } = require ( 'path' )
5+ const tmp = require ( 'tmp' ) ;
6+ const mkdirp = require ( 'mkdirp' ) ;
47
58module . exports = {
69 description : '' ,
710 normalizeEntityName ( ) {
811 // no-op
912 } ,
1013
11- afterInstall ( ) {
12- let targetsFile = './config/targets.js'
14+ filesPath ( ) {
15+ return this . _filesPath ;
16+ } ,
17+
18+ _targetsFile ( project ) {
19+ let configPath = 'config' ;
1320
14- if ( this . project . isEmberCLIAddon ( ) ) {
15- targetsFile = './tests/dummy/config/targets.js' ;
21+ if ( project . pkg [ 'ember-addon' ] && project . pkg [ 'ember-addon' ] [ 'configPath' ] ) {
22+ configPath = project . pkg [ 'ember-addon' ] [ 'configPath' ] ;
1623 }
1724
25+ return join ( configPath , 'targets.js' ) ;
26+ } ,
27+
28+ install ( options ) {
29+ this . _filesPath = tmp . dirSync ( ) . name ;
30+
31+ const targetsFile = this . _targetsFile ( options . project ) ;
32+
1833 const targetsAst = recast . parse ( readFileSync ( targetsFile ) ) ;
1934
2035 recast . visit ( targetsAst , {
@@ -39,6 +54,10 @@ module.exports = {
3954 }
4055 } ) ;
4156
42- writeFileSync ( targetsFile , recast . print ( targetsAst , { tabWidth : 2 , quote : 'single' } ) . code ) ;
57+ let newFile = join ( this . _filesPath , targetsFile ) ;
58+ mkdirp . sync ( dirname ( newFile ) ) ;
59+ writeFileSync ( newFile , recast . print ( targetsAst , { tabWidth : 2 , quote : 'single' } ) . code ) ;
60+
61+ return this . _super . install . apply ( this , arguments ) ;
4362 }
4463} ;
0 commit comments