@@ -2,10 +2,90 @@ import fs from 'fs-extra';
2
2
import semver from 'semver' ;
3
3
import * as recast from 'recast' ;
4
4
5
+ export function getPackageJson ( packageName : string , version : string = '0.0.0' ) {
6
+ return JSON . stringify (
7
+ {
8
+ name : packageName ,
9
+ version : version ,
10
+ license : 'MIT' ,
11
+ main : 'dist/codeshift.config.js' ,
12
+ scripts : {
13
+ build : 'tsc --build' ,
14
+ test : 'jest' ,
15
+ } ,
16
+ dependencies : {
17
+ '@codeshift/utils' : '*' ,
18
+ } ,
19
+ devDependencies : {
20
+ '@codeshift/test-utils' : '*' ,
21
+ '@types/jest' : '^26.0.15' ,
22
+ jest : '^26.6.0' ,
23
+ jscodeshift : '^0.12.0' ,
24
+ prettier : '^1.16.4' ,
25
+ 'ts-jest' : '^26.4.4' ,
26
+ typescript : '^4.3.5' ,
27
+ } ,
28
+ } ,
29
+ null ,
30
+ 2 ,
31
+ ) ;
32
+ }
33
+
34
+ function getConfig ( packageName : string , version : string ) {
35
+ return `export default {
36
+ maintainers: [],
37
+ target: [],
38
+ description: 'Codemods for ${ packageName } ',
39
+ transforms: {
40
+ '${ version } ': require('./${ version } /transform'),
41
+ },
42
+ presets: {},
43
+ };
44
+ ` ;
45
+ }
46
+
47
+ function updateConfig ( path : string , packageName : string , version : string ) {
48
+ const source = fs . readFileSync ( path , 'utf8' ) ;
49
+ const ast = recast . parse ( source ) ;
50
+ const b = recast . types . builders ;
51
+
52
+ recast . visit ( ast , {
53
+ visitProperty ( path ) {
54
+ // @ts -ignore
55
+ if ( path . node . key . name !== 'transforms' ) return false ;
56
+ // @ts -ignore
57
+ const properties = path . node . value . properties ;
58
+ // @ts -ignore
59
+ properties . forEach ( property => {
60
+ if ( semver . eq ( property . key . value , version ) ) {
61
+ throw new Error (
62
+ `Transform for ${ packageName } version ${ version } already exists` ,
63
+ ) ;
64
+ }
65
+ } ) ;
66
+
67
+ properties . push (
68
+ b . property (
69
+ 'init' ,
70
+ b . stringLiteral ( version ) ,
71
+ b . callExpression ( b . identifier ( 'require' ) , [
72
+ b . stringLiteral ( `./${ version } /transform` ) ,
73
+ ] ) ,
74
+ ) ,
75
+ ) ;
76
+
77
+ return false ;
78
+ } ,
79
+ } ) ;
80
+
81
+ return recast . prettyPrint ( ast , { quote : 'single' , trailingComma : true } ) . code ;
82
+ }
83
+
5
84
export function initDirectory (
6
85
packageName : string ,
7
86
version : string ,
8
87
targetPath : string = './' ,
88
+ isReduced : boolean = false ,
9
89
) {
10
90
if ( ! semver . valid ( version ) ) {
11
91
throw new Error (
@@ -14,22 +94,17 @@ export function initDirectory(
14
94
}
15
95
16
96
const basePath = `${ targetPath } /${ packageName . replace ( '/' , '__' ) } ` ;
17
- const codemodPath = `${ basePath } /${ version } ` ;
18
- const configPath = `${ basePath } /codeshift.config.js` ;
19
- const packagePath = ` ${ basePath } /package.json` ;
20
- const motionsPath = ` ${ codemodPath } /motions `;
97
+ const codemodPath = `${ basePath } ${ ! isReduced ? '/src/' : '' } /${ version } ` ;
98
+ const configPath = `${ basePath } ${
99
+ ! isReduced ? '/src' : ''
100
+ } /codeshift.config.ts `;
21
101
22
- fs . mkdirSync ( codemodPath , { recursive : true } ) ;
102
+ if ( fs . existsSync ( codemodPath ) ) {
103
+ throw new Error ( `Codemod for version "${ version } " already exists` ) ;
104
+ }
23
105
24
- fs . copyFileSync (
25
- `${ __dirname } /../template/transform.spec.ts` ,
26
- `${ codemodPath } /transform.spec.ts` ,
27
- ) ;
28
- fs . copyFileSync (
29
- `${ __dirname } /../template/transform.ts` ,
30
- `${ codemodPath } /transform.ts` ,
31
- ) ;
32
- fs . copySync ( `${ __dirname } /../template/motions` , motionsPath ) ;
106
+ fs . copySync ( `${ __dirname } /../template${ isReduced ? '/src' : '' } ` , basePath ) ;
107
+ fs . renameSync ( `${ basePath } ${ ! isReduced ? '/src/' : '' } /codemod` , codemodPath ) ;
33
108
34
109
const testFile = fs
35
110
. readFileSync ( `${ codemodPath } /transform.spec.ts` , 'utf8' )
@@ -38,70 +113,16 @@ export function initDirectory(
38
113
39
114
fs . writeFileSync ( `${ codemodPath } /transform.spec.ts` , testFile ) ;
40
115
41
- fs . writeFileSync (
42
- packagePath ,
43
- `{
44
- "name": "${ packageName } ",
45
- "version": "0.0.1",
46
- "license": "MIT",
47
- "main": "dist/${ packageName } .cjs.js",
48
- "dependencies": {
49
- "@codeshift/utils": "^0.1.2"
50
- },
51
- "devDependencies": {
52
- "jscodeshift": "^0.12.0"
116
+ if ( ! isReduced ) {
117
+ fs . writeFileSync ( `${ basePath } /package.json` , getPackageJson ( packageName ) ) ;
53
118
}
54
- }` ,
55
- ) ;
56
119
57
120
if ( ! fs . existsSync ( configPath ) ) {
58
- fs . writeFileSync (
59
- configPath ,
60
- `export default {
61
- maintainers: [],
62
- transforms: {
63
- '${ version } ': require('./${ version } /transform'),
64
- }
65
- };
66
- ` ,
67
- ) ;
121
+ fs . writeFileSync ( configPath , getConfig ( packageName , version ) ) ;
68
122
} else {
69
- const source = fs . readFileSync ( configPath , 'utf8' ) ;
70
- const ast = recast . parse ( source ) ;
71
- const b = recast . types . builders ;
72
-
73
- recast . visit ( ast , {
74
- visitProperty ( path ) {
75
- // @ts -ignore
76
- if ( path . node . key . name !== 'transforms' ) return false ;
77
- // @ts -ignore
78
- const properties = path . node . value . properties ;
79
- // @ts -ignore
80
- properties . forEach ( property => {
81
- if ( semver . eq ( property . key . value , version ) ) {
82
- throw new Error (
83
- `Transform for ${ packageName } version ${ version } already exists` ,
84
- ) ;
85
- }
86
- } ) ;
87
-
88
- properties . push (
89
- b . property (
90
- 'init' ,
91
- b . stringLiteral ( version ) ,
92
- b . callExpression ( b . identifier ( 'require' ) , [
93
- b . stringLiteral ( `./${ version } /transform` ) ,
94
- ] ) ,
95
- ) ,
96
- ) ;
97
-
98
- return false ;
99
- } ,
100
- } ) ;
101
-
102
123
fs . writeFileSync (
103
124
configPath ,
104
- recast . prettyPrint ( ast , { quote : 'single' , trailingComma : true } ) . code ,
125
+ updateConfig ( configPath , packageName , version ) ,
105
126
) ;
106
127
}
107
128
}
0 commit comments