@@ -2,51 +2,73 @@ import semver from 'semver';
22import chalk from 'chalk' ;
33import path from 'path' ;
44import { PluginManager } from 'live-plugin-manager' ;
5+ import merge from 'lodash/merge' ;
56// @ts -ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
67import * as jscodeshift from 'jscodeshift/src/Runner' ;
78import { isValidConfig } from '@codeshift/validator' ;
9+ import { CodeshiftConfig } from '@codeshift/types' ;
810
911import { Flags } from './types' ;
1012import { InvalidUserInputError } from './errors' ;
1113
1214const packageManager = new PluginManager ( ) ;
1315
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 ;
16+ async function fetchCommunityPackageConfig ( packageName : string ) {
17+ const commPackageName = `@codeshift/mod- ${ packageName } ` ;
18+ await packageManager . install ( commPackageName ) ;
19+ const pkg = packageManager . require ( packageName ) ;
20+ const config : CodeshiftConfig = pkg . default ? pkg . default : pkg ;
1921
20- // if (!isValidConfig(commConfig)) {
21- // }
22+ if ( ! isValidConfig ( config ) ) {
23+ throw new Error ( `Invalid config found in module ${ commPackageName } ` ) ;
24+ }
2225
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 ;
26+ return config ;
27+ }
2728
28- // Attemp to fetch from the main entrypoint
29- const info = packageManager . getInfo ( packageName ) ;
29+ async function fetchRemotePackageConfig ( packageName : string ) {
30+ await packageManager . install ( packageName ) ;
3031 const pkg = packageManager . require ( packageName ) ;
3132
32- if ( info || pkg ) {
33- config = pkg . default ? pkg . default : pkg ;
33+ if ( pkg ) {
34+ const config : CodeshiftConfig = pkg . default ? pkg . default : pkg ;
3435
35- if ( config && isValidConfig ) {
36+ if ( config && isValidConfig ( config ) ) {
3637 // Found a config at the main entry-point
38+ return config ;
3739 }
40+ }
3841
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- ) ) ;
42+ const info = packageManager . getInfo ( packageName ) ;
43+
44+ if ( info ) {
45+ let config : any ;
46+
47+ [
48+ path . join ( info ?. location , 'codeshift.config.js' ) ,
49+ path . join ( info ?. location , 'codeshift.config.ts' ) ,
50+ path . join ( info ?. location , 'src' , 'codeshift.config.js' ) ,
51+ path . join ( info ?. location , 'src' , 'codeshift.config.ts' ) ,
52+ path . join ( info ?. location , 'codemods' , 'codeshift.config.js' ) ,
53+ path . join ( info ?. location , 'codemods' , 'codeshift.config.ts' ) ,
54+ ] . forEach ( searchPath => {
55+ try {
56+ // eslint-disable-next-line @typescript-eslint/no-var-requires
57+ const pkg = require ( searchPath ) ;
58+ const searchConfig : CodeshiftConfig = pkg . default ? pkg . default : pkg ;
59+
60+ if ( isValidConfig ( searchConfig ) ) {
61+ config = searchConfig ;
62+ }
63+ } catch ( e ) { }
64+ } ) ;
65+
66+ if ( config ) return config ;
4667 }
47- // if ()
4868
49- return config ;
69+ throw new Error (
70+ `Unable to locate a valid codeshift.config in package ${ packageName } ` ,
71+ ) ;
5072}
5173
5274export default async function main ( paths : string [ ] , flags : Flags ) {
@@ -77,7 +99,9 @@ export default async function main(paths: string[], flags: Flags) {
7799 . filter ( str => ! ! str ) [ 0 ]
78100 . replace ( '/' , '__' ) ;
79101
80- const config = await fetchPackageConfig ( pkgName ) ;
102+ const communityConfig = await fetchCommunityPackageConfig ( pkgName ) ;
103+ const remoteConfig = await fetchRemotePackageConfig ( pkgName ) ;
104+ const config : CodeshiftConfig = merge ( communityConfig , remoteConfig ) ;
81105
82106 const rawTransformIds = pkg . split ( / (? = [ @ # ] ) / ) . filter ( str => ! ! str ) ;
83107 rawTransformIds . shift ( ) ;
0 commit comments