@@ -47,7 +47,7 @@ jest.config.js
4747function getConfig ( packageName : string , transform ?: string , preset ?: string ) {
4848 return `module.exports = {
4949 maintainers: [],
50- target : [],
50+ targets : [],
5151 description: 'Codemods for ${ packageName } ',
5252 transforms: {${
5353 transform
@@ -62,22 +62,24 @@ function getConfig(packageName: string, transform?: string, preset?: string) {
6262}
6363
6464function updateConfig (
65- path : string ,
65+ targetPath : string ,
6666 packageName : string ,
6767 transformName : string ,
6868 type : 'version' | 'preset' ,
69+ isReduced = false ,
6970) {
70- const source = fs . readFileSync ( path , 'utf8' ) ;
71+ const configPath = path . join ( targetPath , 'codeshift.config.js' ) ;
72+ const source = fs . readFileSync ( configPath , 'utf8' ) ;
7173 const ast = recast . parse ( source ) ;
7274 const b = recast . types . builders ;
7375 const key = type === 'version' ? 'transforms' : 'presets' ;
7476
7577 recast . visit ( ast , {
76- visitProperty ( path ) {
78+ visitProperty ( propertyPath ) {
7779 // @ts -ignore
78- if ( path . node . key . name !== key ) return false ;
80+ if ( propertyPath . node . key . name !== key ) return false ;
7981 // @ts -ignore
80- const properties = path . node . value . properties ;
82+ const properties = propertyPath . node . value . properties ;
8183 // @ts -ignore
8284 properties . forEach ( property => {
8385 if ( property . key . value === transformName ) {
@@ -87,6 +89,10 @@ function updateConfig(
8789 }
8890 } ) ;
8991
92+ const transformPath = `./${
93+ ! isReduced ? 'src/' : ''
94+ } ${ transformName } /transform`;
95+
9096 properties . push (
9197 b . property (
9298 'init' ,
@@ -96,7 +102,7 @@ function updateConfig(
96102 b . identifier ( 'require' ) ,
97103 b . identifier ( 'resolve' ) ,
98104 ) ,
99- [ b . stringLiteral ( `./ ${ transformName } /transform` ) ] ,
105+ [ b . stringLiteral ( transformPath ) ] ,
100106 ) ,
101107 ) ,
102108 ) ;
@@ -105,41 +111,47 @@ function updateConfig(
105111 } ,
106112 } ) ;
107113
108- return recast . prettyPrint ( ast , { quote : 'single' , trailingComma : true } ) . code ;
114+ fs . writeFileSync (
115+ configPath ,
116+ recast . prettyPrint ( ast , {
117+ quote : 'single' ,
118+ trailingComma : true ,
119+ tabWidth : 2 ,
120+ } ) . code ,
121+ ) ;
122+ }
123+
124+ export function initConfig ( packageName : string , targetPath = './' ) {
125+ const configPath = path . join ( targetPath , 'codeshift.config.js' ) ;
126+
127+ if ( ! fs . existsSync ( configPath ) ) {
128+ fs . writeFileSync ( configPath , getConfig ( packageName ) ) ;
129+ }
109130}
110131
111132export function initDirectory (
112133 packageName : string ,
113134 targetPath = './' ,
114135 isReduced = false ,
115136) {
116- const basePath = path . join ( targetPath , packageName . replace ( '/' , '__' ) ) ;
117- const configPath = path . join (
118- basePath ,
119- ! isReduced ? 'src' : '' ,
120- 'codeshift.config.js' ,
121- ) ;
122-
123137 fs . copySync (
124138 path . join ( __dirname , '..' , 'template' , isReduced ? 'src' : '' ) ,
125- basePath ,
139+ targetPath ,
126140 {
127141 filter : src => ! src . includes ( 'src/codemod' ) ,
128142 } ,
129143 ) ;
130144
131145 if ( ! isReduced ) {
132146 fs . writeFileSync (
133- path . join ( basePath , 'package.json' ) ,
147+ path . join ( targetPath , 'package.json' ) ,
134148 getPackageJson ( packageName ) ,
135149 ) ;
136150
137- fs . writeFileSync ( path . join ( basePath , '.npmignore' ) , getNpmIgnore ( ) ) ;
151+ fs . writeFileSync ( path . join ( targetPath , '.npmignore' ) , getNpmIgnore ( ) ) ;
138152 }
139153
140- if ( ! fs . existsSync ( configPath ) ) {
141- fs . writeFileSync ( configPath , getConfig ( packageName ) ) ;
142- }
154+ initConfig ( packageName , targetPath ) ;
143155}
144156
145157export function initTransform (
@@ -153,26 +165,23 @@ export function initTransform(
153165 throw new Error ( `Provided version ${ id } is not a valid semver version` ) ;
154166 }
155167
156- const basePath = path . join ( targetPath , packageName . replace ( '/' , '__' ) ) ;
157- const transformPath = path . join ( basePath , ! isReduced ? 'src' : '' , id ) ;
158- const configPath = path . join (
159- basePath ,
160- ! isReduced ? 'src' : '' ,
161- 'codeshift.config.js' ,
162- ) ;
168+ const transformPath = path . join ( targetPath , ! isReduced ? 'src' : '' , id ) ;
163169
164170 if ( fs . existsSync ( transformPath ) ) {
165171 throw new Error ( `Codemod for ${ type } "${ id } " already exists` ) ;
166172 }
167173
168- fs . copySync (
169- path . join ( __dirname , '..' , 'template' , isReduced ? 'src' : '' ) ,
170- basePath ,
174+ const codemodTemplateDestinationPath = path . join (
175+ targetPath ,
176+ ! isReduced ? 'src' : '' ,
177+ 'codemod' ,
171178 ) ;
172- fs . renameSync (
173- path . join ( basePath , ! isReduced ? 'src' : '' , 'codemod' ) ,
174- transformPath ,
179+
180+ fs . copySync (
181+ path . join ( __dirname , '..' , 'template' , 'src' , 'codemod' ) ,
182+ codemodTemplateDestinationPath ,
175183 ) ;
184+ fs . renameSync ( codemodTemplateDestinationPath , transformPath ) ;
176185
177186 const testFilePath = path . join ( transformPath , 'transform.spec.ts' ) ;
178187 const testFile = fs
@@ -183,15 +192,5 @@ export function initTransform(
183192
184193 fs . writeFileSync ( testFilePath , testFile ) ;
185194
186- if ( ! isReduced ) {
187- fs . writeFileSync (
188- path . join ( basePath , 'package.json' ) ,
189- getPackageJson ( packageName ) ,
190- ) ;
191- }
192-
193- fs . writeFileSync (
194- configPath ,
195- updateConfig ( configPath , packageName , id || '' , type ) ,
196- ) ;
195+ updateConfig ( targetPath , packageName , id || '' , type , isReduced ) ;
197196}
0 commit comments