@@ -2,6 +2,7 @@ import semver from 'semver';
2
2
import chalk from 'chalk' ;
3
3
import path from 'path' ;
4
4
import { PluginManager } from 'live-plugin-manager' ;
5
+ import merge from 'lodash/merge' ;
5
6
// @ts -ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
6
7
import * as jscodeshift from 'jscodeshift/src/Runner' ;
7
8
import { isValidConfig } from '@codeshift/validator' ;
@@ -11,42 +12,59 @@ import { InvalidUserInputError } from './errors';
11
12
12
13
const packageManager = new PluginManager ( ) ;
13
14
14
- async function fetchPackageConfig ( packageName : string ) {
15
- // Attempt to find package from the community folder
16
- await packageManager . install ( `@codeshift/mod- ${ packageName } ` ) ;
17
- const commPkg = packageManager . require ( packageName ) ;
18
- const commConfig = commPkg . default ? commPkg . default : commPkg ;
15
+ async function fetchCommunityPackageConfig ( packageName : string ) {
16
+ const commPackageName = `@codeshift/mod- ${ packageName } ` ;
17
+ await packageManager . install ( commPackageName ) ;
18
+ const pkg = packageManager . require ( packageName ) ;
19
+ const config = pkg . default ? pkg . default : pkg ;
19
20
20
- // if (!isValidConfig(commConfig)) {
21
- // }
21
+ if ( ! isValidConfig ( config ) ) {
22
+ throw new Error ( `Invalid config found in module ${ commPackageName } ` ) ;
23
+ }
22
24
23
- // Attempt to find source package from npm
24
- await packageManager . install ( packageName ) ;
25
- // For source packages, fetching configs is a bit more elaborate
26
- let config ;
25
+ return config ;
26
+ }
27
27
28
- // Attemp to fetch from the main entrypoint
29
- const info = packageManager . getInfo ( packageName ) ;
28
+ async function fetchRemotePackageConfig ( packageName : string ) {
29
+ await packageManager . install ( packageName ) ;
30
30
const pkg = packageManager . require ( packageName ) ;
31
31
32
- if ( info || pkg ) {
33
- config = pkg . default ? pkg . default : pkg ;
32
+ if ( pkg ) {
33
+ const config = pkg . default ? pkg . default : pkg ;
34
34
35
- if ( config && isValidConfig ) {
35
+ if ( config && isValidConfig ( config ) ) {
36
36
// Found a config at the main entry-point
37
+ return config ;
37
38
}
39
+ }
38
40
39
- config = require ( path . join ( info ?. location , 'codeshift.config.js' ) ) ;
40
- config = require ( path . join ( info ?. location , 'src' , 'codeshift.config.js' ) ) ;
41
- config = require ( path . join (
42
- info ?. location ,
43
- 'codemods' ,
44
- 'codeshift.config.js' ,
45
- ) ) ;
41
+ const info = packageManager . getInfo ( packageName ) ;
42
+
43
+ if ( info ) {
44
+ let config : any ;
45
+
46
+ [
47
+ path . join ( info ?. location , 'codeshift.config.js' ) ,
48
+ path . join ( info ?. location , 'src' , 'codeshift.config.js' ) ,
49
+ path . join ( info ?. location , 'codemods' , 'codeshift.config.js' ) ,
50
+ ] . forEach ( searchPath => {
51
+ try {
52
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
53
+ const pkg = require ( searchPath ) ;
54
+ const searchConfig = pkg . default ? pkg . default : pkg ;
55
+
56
+ if ( isValidConfig ( searchConfig ) ) {
57
+ config = searchConfig ;
58
+ }
59
+ } catch ( e ) { }
60
+ } ) ;
61
+
62
+ if ( config ) return config ;
46
63
}
47
- // if ()
48
64
49
- return config ;
65
+ throw new Error (
66
+ `Unable to locate a valid codeshift.config in package ${ packageName } ` ,
67
+ ) ;
50
68
}
51
69
52
70
export default async function main ( paths : string [ ] , flags : Flags ) {
@@ -77,7 +95,9 @@ export default async function main(paths: string[], flags: Flags) {
77
95
. filter ( str => ! ! str ) [ 0 ]
78
96
. replace ( '/' , '__' ) ;
79
97
80
- const config = await fetchPackageConfig ( pkgName ) ;
98
+ const communityConfig = await fetchCommunityPackageConfig ( pkgName ) ;
99
+ const remoteConfig = await fetchRemotePackageConfig ( pkgName ) ;
100
+ const config = merge ( communityConfig , remoteConfig ) ;
81
101
82
102
const rawTransformIds = pkg . split ( / (? = [ @ # ] ) / ) . filter ( str => ! ! str ) ;
83
103
rawTransformIds . shift ( ) ;
0 commit comments