@@ -14,11 +14,11 @@ export function getPackageJson(packageName: string, version: string = '0.0.0') {
1414 build : 'tsc --build' ,
1515 test : 'jest' ,
1616 } ,
17- dependencies : {
18- '@codeshift/utils' : `^${ utilVersion } ` ,
19- } ,
17+ dependencies : { } ,
2018 devDependencies : {
19+ '@codeshift/utils' : `^${ utilVersion } ` ,
2120 '@codeshift/test-utils' : '*' ,
21+ '@types/node' : '^16.11.0' ,
2222 '@types/jest' : '^26.0.15' ,
2323 jest : '^26.6.0' ,
2424 jscodeshift : '^0.12.0' ,
@@ -32,46 +32,60 @@ export function getPackageJson(packageName: string, version: string = '0.0.0') {
3232 ) ;
3333}
3434
35- function getConfig ( packageName : string , version : string ) {
36- return `export default {
35+ function getConfig ( packageName : string , transform ?: string , preset ? : string ) {
36+ return `module.exports = {
3737 maintainers: [],
3838 target: [],
3939 description: 'Codemods for ${ packageName } ',
40- transforms: {
41- '${ version } ': require('./${ version } /transform'),
42- },
43- presets: {},
40+ transforms: {${
41+ transform
42+ ? `'${ transform } ': require.resolve('./${ transform } /transform'),`
43+ : ''
44+ } },
45+ presets: {${
46+ preset ? `'${ preset } ': require.resolve('./${ preset } /transform'),` : ''
47+ } },
4448};
4549` ;
4650}
4751
48- function updateConfig ( path : string , packageName : string , version : string ) {
52+ function updateConfig (
53+ path : string ,
54+ packageName : string ,
55+ transformName : string ,
56+ type : 'version' | 'preset' ,
57+ ) {
4958 const source = fs . readFileSync ( path , 'utf8' ) ;
5059 const ast = recast . parse ( source ) ;
5160 const b = recast . types . builders ;
61+ const key = type === 'version' ? 'transforms' : 'presets' ;
5262
5363 recast . visit ( ast , {
5464 visitProperty ( path ) {
5565 // @ts -ignore
56- if ( path . node . key . name !== 'transforms' ) return false ;
66+ if ( path . node . key . name !== key ) return false ;
5767 // @ts -ignore
5868 const properties = path . node . value . properties ;
5969 // @ts -ignore
6070 properties . forEach ( property => {
61- if ( semver . eq ( property . key . value , version ) ) {
71+ if ( property . key . value === transformName ) {
6272 throw new Error (
63- `Transform for ${ packageName } version ${ version } already exists` ,
73+ `Transform for ${ packageName } ${ transformName } already exists` ,
6474 ) ;
6575 }
6676 } ) ;
6777
6878 properties . push (
6979 b . property (
7080 'init' ,
71- b . stringLiteral ( version ) ,
72- b . callExpression ( b . identifier ( 'require' ) , [
73- b . stringLiteral ( `./${ version } /transform` ) ,
74- ] ) ,
81+ b . stringLiteral ( transformName ) ,
82+ b . callExpression (
83+ b . memberExpression (
84+ b . identifier ( 'require' ) ,
85+ b . identifier ( 'resolve' ) ,
86+ ) ,
87+ [ b . stringLiteral ( `./${ transformName } /transform` ) ] ,
88+ ) ,
7589 ) ,
7690 ) ;
7791
@@ -84,46 +98,51 @@ function updateConfig(path: string, packageName: string, version: string) {
8498
8599export function initDirectory (
86100 packageName : string ,
87- version : string ,
88- targetPath : string = './' ,
101+ transform : string ,
102+ type : 'version' | 'preset' ,
103+ path : string = './' ,
89104 isReduced : boolean = false ,
90105) {
91- if ( ! semver . valid ( version ) ) {
106+ if ( type === 'version' && ! semver . valid ( transform ) ) {
92107 throw new Error (
93- `Provided version ${ version } is not a valid semver version` ,
108+ `Provided version ${ transform } is not a valid semver version` ,
94109 ) ;
95110 }
96111
97- const basePath = `${ targetPath } /${ packageName . replace ( '/' , '__' ) } ` ;
98- const codemodPath = `${ basePath } ${ ! isReduced ? '/src/' : '' } /${ version } ` ;
112+ const basePath = `${ path } /${ packageName . replace ( '/' , '__' ) } ` ;
113+ const transformPath = `${ basePath } ${ ! isReduced ? '/src/' : '' } /${ transform } ` ;
99114 const configPath = `${ basePath } ${
100115 ! isReduced ? '/src' : ''
101116 } /codeshift.config.ts`;
102117
103- if ( fs . existsSync ( codemodPath ) ) {
104- throw new Error ( `Codemod for version "${ version } " already exists` ) ;
118+ if ( fs . existsSync ( transformPath ) ) {
119+ throw new Error ( `Codemod for ${ type } "${ transform } " already exists` ) ;
105120 }
106121
107122 fs . copySync ( `${ __dirname } /../template${ isReduced ? '/src' : '' } ` , basePath ) ;
108- fs . renameSync ( `${ basePath } ${ ! isReduced ? '/src/' : '' } /codemod` , codemodPath ) ;
123+ fs . renameSync (
124+ `${ basePath } ${ ! isReduced ? '/src/' : '' } /codemod` ,
125+ transformPath ,
126+ ) ;
109127
110128 const testFile = fs
111- . readFileSync ( `${ codemodPath } /transform.spec.ts` , 'utf8' )
129+ . readFileSync ( `${ transformPath } /transform.spec.ts` , 'utf8' )
112130 . replace ( '<% packageName %>' , packageName )
113- . replace ( '<% version %>' , version ) ;
131+ . replace ( '<% seperator %>' , type === 'version' ? '@' : '#' )
132+ . replace ( '<% transform %>' , transform || '' ) ;
114133
115- fs . writeFileSync ( `${ codemodPath } /transform.spec.ts` , testFile ) ;
134+ fs . writeFileSync ( `${ transformPath } /transform.spec.ts` , testFile ) ;
116135
117136 if ( ! isReduced ) {
118137 fs . writeFileSync ( `${ basePath } /package.json` , getPackageJson ( packageName ) ) ;
119138 }
120139
121140 if ( ! fs . existsSync ( configPath ) ) {
122- fs . writeFileSync ( configPath , getConfig ( packageName , version ) ) ;
141+ fs . writeFileSync ( configPath , getConfig ( packageName , transform ) ) ;
123142 } else {
124143 fs . writeFileSync (
125144 configPath ,
126- updateConfig ( configPath , packageName , version ) ,
145+ updateConfig ( configPath , packageName , transform || '' , type ) ,
127146 ) ;
128147 }
129148}
0 commit comments