11import fs from 'fs-extra' ;
2+ import path from 'path' ;
23import semver from 'semver' ;
34import * as recast from 'recast' ;
45import { version as utilVersion } from '@codeshift/utils/package.json' ;
@@ -98,25 +99,47 @@ function updateConfig(
9899
99100export function initDirectory (
100101 packageName : string ,
101- transform : string ,
102- type : 'version' | 'preset' ,
103102 targetPath : string = './' ,
104103 isReduced : boolean = false ,
105104) {
106- if ( type === 'version' && ! semver . valid ( transform ) ) {
105+
106+ const basePath = `${ targetPath } /${ packageName . replace ( '/' , '__' ) } ` ;
107+ const configPath = `${ basePath } ${
108+ ! isReduced ? '/src' : ''
109+ } /codeshift.config.js`;
110+
111+ fs . copySync ( `${ __dirname } /../template${ isReduced ? '/src' : '' } ` , basePath , {
112+ filter : ( src ) => ! src . includes ( 'src/codemod' )
113+ } ) ;
114+
115+ if ( ! isReduced ) {
116+ fs . writeFileSync ( path . join ( basePath , 'package.json' ) , getPackageJson ( packageName ) ) ;
117+ }
118+
119+ if ( ! fs . existsSync ( configPath ) ) {
120+ fs . writeFileSync ( configPath , getConfig ( packageName ) ) ;
121+ }
122+ }
123+
124+ export function initTransform ( packageName : string ,
125+ id : string ,
126+ type : 'version' | 'preset' ,
127+ targetPath : string = './' ,
128+ isReduced : boolean = false ) {
129+ if ( type === 'version' && ! semver . valid ( id ) ) {
107130 throw new Error (
108- `Provided version ${ transform } is not a valid semver version` ,
131+ `Provided version ${ id } is not a valid semver version` ,
109132 ) ;
110133 }
111134
112135 const basePath = `${ targetPath } /${ packageName . replace ( '/' , '__' ) } ` ;
113- const transformPath = `${ basePath } ${ ! isReduced ? '/src' : '' } /${ transform } ` ;
136+ const transformPath = `${ basePath } ${ ! isReduced ? '/src' : '' } /${ id } ` ;
114137 const configPath = `${ basePath } ${
115138 ! isReduced ? '/src' : ''
116139 } /codeshift.config.js`;
117140
118141 if ( fs . existsSync ( transformPath ) ) {
119- throw new Error ( `Codemod for ${ type } "${ transform } " already exists` ) ;
142+ throw new Error ( `Codemod for ${ type } "${ id } " already exists` ) ;
120143 }
121144
122145 fs . copySync ( `${ __dirname } /../template${ isReduced ? '/src' : '' } ` , basePath ) ;
@@ -125,24 +148,21 @@ export function initDirectory(
125148 transformPath ,
126149 ) ;
127150
151+ const testFilePath = path . join ( transformPath , 'transform.spec.ts' ) ;
128152 const testFile = fs
129- . readFileSync ( ` ${ transformPath } /transform.spec.ts` , 'utf8' )
153+ . readFileSync ( testFilePath , 'utf8' )
130154 . replace ( '<% packageName %>' , packageName )
131155 . replace ( '<% seperator %>' , type === 'version' ? '@' : '#' )
132- . replace ( '<% transform %>' , transform || '' ) ;
156+ . replace ( '<% transform %>' , id || '' ) ;
133157
134- fs . writeFileSync ( ` ${ transformPath } /transform.spec.ts` , testFile ) ;
158+ fs . writeFileSync ( testFilePath , testFile ) ;
135159
136160 if ( ! isReduced ) {
137- fs . writeFileSync ( ` ${ basePath } / package.json` , getPackageJson ( packageName ) ) ;
161+ fs . writeFileSync ( path . join ( basePath , ' package.json' ) , getPackageJson ( packageName ) ) ;
138162 }
139163
140- if ( ! fs . existsSync ( configPath ) ) {
141- fs . writeFileSync ( configPath , getConfig ( packageName , transform ) ) ;
142- } else {
143- fs . writeFileSync (
144- configPath ,
145- updateConfig ( configPath , packageName , transform || '' , type ) ,
146- ) ;
147- }
164+ fs . writeFileSync (
165+ configPath ,
166+ updateConfig ( configPath , packageName , id || '' , type ) ,
167+ ) ;
148168}
0 commit comments