@@ -71,7 +71,6 @@ function updateConfig(
7171 packageName : string ,
7272 transformName : string ,
7373 type : 'version' | 'preset' ,
74- isReduced = false ,
7574) {
7675 const configPath = path . join ( targetPath , 'codeshift.config.js' ) ;
7776 const source = fs . readFileSync ( configPath , 'utf8' ) ;
@@ -94,21 +93,15 @@ function updateConfig(
9493 }
9594 } ) ;
9695
97- const transformPath = `./${
98- ! isReduced ? 'codemods/' : ''
99- } ${ transformName } /transform`;
96+ const transformPath = `./${ transformName } /transform` ;
10097
10198 properties . push (
10299 b . property (
103100 'init' ,
104101 b . stringLiteral ( transformName ) ,
105- b . callExpression (
106- b . memberExpression (
107- b . identifier ( 'require' ) ,
108- b . identifier ( 'resolve' ) ,
109- ) ,
110- [ b . stringLiteral ( transformPath ) ] ,
111- ) ,
102+ b . callExpression ( b . identifier ( 'require' ) , [
103+ b . stringLiteral ( transformPath ) ,
104+ ] ) ,
112105 ) ,
113106 ) ;
114107
@@ -130,6 +123,7 @@ export function initConfig(packageName: string, targetPath = './') {
130123 const configPath = path . join ( targetPath , 'codeshift.config.js' ) ;
131124
132125 if ( ! fs . existsSync ( configPath ) ) {
126+ fs . mkdirSync ( targetPath , { recursive : true } ) ;
133127 fs . writeFileSync ( configPath , getConfig ( packageName ) ) ;
134128 }
135129}
@@ -139,15 +133,9 @@ export function initDirectory(
139133 targetPath = './' ,
140134 isReduced = false ,
141135) {
142- const sourcePath = path . join ( targetPath , 'src' ) ;
143-
144- fs . copySync (
145- path . join ( TEMPLATE_PATH , isReduced ? 'codemods' : '' ) ,
146- sourcePath ,
147- {
148- filter : src => ! src . includes ( 'codemods/codemod' ) ,
149- } ,
150- ) ;
136+ if ( ! fs . existsSync ( targetPath ) ) {
137+ fs . mkdirSync ( targetPath ) ;
138+ }
151139
152140 fs . writeFileSync (
153141 path . join ( targetPath , 'package.json' ) ,
@@ -159,8 +147,11 @@ export function initDirectory(
159147 ) ;
160148
161149 if ( ! isReduced ) {
162- fs . writeFileSync ( path . join ( targetPath , '.npmignore' ) , getNpmIgnore ( ) ) ;
150+ fs . copySync ( path . join ( TEMPLATE_PATH ) , targetPath , {
151+ filter : src => ! src . includes ( '/codemods' ) ,
152+ } ) ;
163153
154+ fs . writeFileSync ( path . join ( targetPath , '.npmignore' ) , getNpmIgnore ( ) ) ;
164155 const readmeFilePath = path . join ( targetPath , 'README.md' ) ;
165156 const readmeFile = fs
166157 . readFileSync ( readmeFilePath , 'utf8' )
@@ -169,15 +160,14 @@ export function initDirectory(
169160 fs . writeFileSync ( readmeFilePath , readmeFile ) ;
170161 }
171162
172- initConfig ( packageName , sourcePath ) ;
163+ initConfig ( packageName , path . join ( targetPath , 'src' ) ) ;
173164}
174165
175166export function initTransform (
176167 packageName : string ,
177168 id : string ,
178169 type : 'version' | 'preset' ,
179170 targetPath = './' ,
180- isReduced = false ,
181171) {
182172 if ( type === 'version' && ! semver . valid ( id ) ) {
183173 throw new Error ( `Provided version ${ id } is not a valid semver version` ) ;
@@ -190,17 +180,10 @@ export function initTransform(
190180 throw new Error ( `Codemod for ${ type } "${ id } " already exists` ) ;
191181 }
192182
193- const codemodTemplateDestinationPath = path . join (
194- targetPath ,
195- ! isReduced ? 'codemods' : '' ,
196- 'codemod' ,
197- ) ;
183+ const destinationPath = path . join ( sourcePath , 'codemod' ) ;
198184
199- fs . copySync (
200- path . join ( TEMPLATE_PATH , 'codemods' , 'codemod' ) ,
201- codemodTemplateDestinationPath ,
202- ) ;
203- fs . renameSync ( codemodTemplateDestinationPath , transformPath ) ;
185+ fs . copySync ( path . join ( TEMPLATE_PATH , 'codemods' , 'codemod' ) , destinationPath ) ;
186+ fs . renameSync ( destinationPath , transformPath ) ;
204187
205188 const testFilePath = path . join ( transformPath , 'transform.spec.ts' ) ;
206189 const testFile = fs
@@ -220,5 +203,5 @@ export function initTransform(
220203
221204 fs . writeFileSync ( readmeFilePath , readmeFile ) ;
222205
223- updateConfig ( sourcePath , packageName , id || '' , type , isReduced ) ;
206+ updateConfig ( sourcePath , packageName , id || '' , type ) ;
224207}
0 commit comments