@@ -2,9 +2,11 @@ var path = require('path');
22var CoreObject = require ( 'core-object' ) ;
33var fse = require ( 'fs-extra' ) ;
44var existsSync = require ( 'exists-sync' ) ;
5+ var jscodeshift = require ( 'jscodeshift' ) ;
56var isTypeInSingleTypeCollection = require ( '../utils/is-type-in-single-type-collection' ) ;
67var defaultTypeForCollection = require ( '../utils/default-type-for-collection' ) ;
78var calculateCollectionInfo = require ( '../utils/calculate-collection-info' ) ;
9+ var importDeclarationsTransform = require ( '../transforms/import-declarations' ) ;
810
911var FileInfo = CoreObject . extend ( {
1012 type : 'FileInfo' ,
@@ -95,7 +97,33 @@ var FileInfo = CoreObject.extend({
9597 this . _fileContents = fse . readFileSync ( fullPath , { encoding : 'utf8' } ) ;
9698 } ,
9799
100+ updateImports : function ( ) {
101+ if ( this . ext !== '.js' || ! this . _fileContents ) { return ; } // only process JavaScript files
102+
103+ var appName = this . options . projectName ;
104+
105+ try {
106+ var newContents = importDeclarationsTransform (
107+ { source : this . _fileContents ,
108+ fileInfo : this ,
109+ appName : appName ,
110+ fileInfos : this . _fileInfoCollection . _fileInfos
111+ } ,
112+ { jscodeshift } ) ;
113+
114+ var fullPath = path . join ( this . projectRoot , this . sourceRelativePath ) ;
115+
116+ fse . writeFileSync ( fullPath , newContents , { encoding : 'utf-8' } ) ;
117+ this . _fileContents = newContents ;
118+ } catch ( e ) {
119+ // eslint-disable-next-line no-console
120+ console . log ( 'error parsing file `' + this . sourceRelativePath + '` failed to apply codeshift. Possible invalid JS file. Returning original file unchanged. error: ' + e . message ) ;
121+ }
122+ } ,
123+
98124 repopulate : function ( ) {
125+ this . updateImports ( ) ;
126+
99127 var inComponentsCollection = this . collection === 'components' ;
100128 var renderableName = path . join ( this . namespace , this . name ) ;
101129
@@ -135,7 +163,7 @@ var FileInfo = CoreObject.extend({
135163 var fullPath = path . join ( this . projectRoot , this . sourceRelativePath ) ;
136164
137165 fse . writeFileSync ( fullPath , newContents , { encoding : 'utf-8' } ) ;
138- this . _fileContents = fse . readFileSync ( fullPath , { encoding : 'utf8' } ) ;
166+ this . _fileContents = newContents ;
139167 } ,
140168
141169 shouldUseDotFormNaming : function ( ) {
0 commit comments