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