@@ -12,50 +12,53 @@ export function onCancel() {
1212
1313const transformerDirectory = join ( __dirname , '../' , 'transforms' )
1414
15- export async function transform ( codemodName ?: string , source ?: string , options ?: Record < string , unknown > ) {
16- let codemodSelected = codemodName
17- let sourceSelected = source
15+ const selectCodemod = async ( ) : Promise < string | undefined > => {
16+ const res = await prompts (
17+ {
18+ type : 'select' ,
19+ name : 'transformer' ,
20+ message : 'Which codemod would you like to apply?' ,
21+ choices : TRANSFORM_OPTIONS . map ( ( { description, value, version } ) => {
22+ return {
23+ title : `(${ bold ( `v${ version } ` ) } ) ${ value } ` ,
24+ description,
25+ value,
26+ }
27+ } ) ,
28+ } ,
29+ { onCancel } ,
30+ )
1831
19- const existCodemod = TRANSFORM_OPTIONS . find ( ( { value } ) => value === codemodSelected )
32+ return res . transformer
33+ }
2034
21- if ( ! codemodSelected || ( codemodSelected && ! existCodemod ) ) {
22- const res = await prompts (
23- {
24- type : 'select' ,
25- name : 'transformer' ,
26- message : 'Which codemod would you like to apply?' ,
27- choices : TRANSFORM_OPTIONS . map ( ( { description, value, version } ) => {
28- return {
29- title : `(${ bold ( `v${ version } ` ) } ) ${ value } ` ,
30- description,
31- value,
32- }
33- } ) ,
34- } ,
35- { onCancel } ,
36- )
35+ const selectSource = async ( ) : Promise < string | undefined > => {
36+ const res = await prompts (
37+ {
38+ type : 'text' ,
39+ name : 'path' ,
40+ message : 'Which files or directories should the codemods be applied to?' ,
41+ initial : '.' ,
42+ } ,
43+ { onCancel } ,
44+ )
3745
38- codemodSelected = res . transformer
39- }
46+ return res . path
47+ }
4048
41- if ( ! sourceSelected ) {
42- const res = await prompts (
43- {
44- type : 'text' ,
45- name : 'path' ,
46- message : 'Which files or directories should the codemods be applied to?' ,
47- initial : '.' ,
48- } ,
49- { onCancel } ,
50- )
49+ export async function transform ( codemodName ?: string , source ?: string , options ?: Record < string , unknown > ) {
50+ const existCodemod = TRANSFORM_OPTIONS . find ( ( { value } ) => value === codemodName )
51+ const codemodSelected = ! codemodName || ( codemodName && ! existCodemod ) ? await selectCodemod ( ) : codemodName
5152
52- sourceSelected = res . path
53+ if ( ! codemodSelected ) {
54+ console . info ( '> Codemod is not selected. Exits the program. \n' )
55+ process . exit ( 1 )
5356 }
5457
55- sourceSelected = resolve ( sourceSelected || '' )
58+ const sourceSelected = source || ( await selectSource ( ) )
5659
57- if ( ! codemodSelected ) {
58- console . info ( '> Codemod is not selected. Exist the program. \n' )
60+ if ( ! sourceSelected ) {
61+ console . info ( '> Source path for project is not selected. Exits the program. \n' )
5962 process . exit ( 1 )
6063 }
6164
@@ -69,5 +72,5 @@ export async function transform(codemodName?: string, source?: string, options?:
6972 extensions : 'cts,mts,ts,js,mjs,cjs' ,
7073 }
7174
72- await jscodeshift ( transformerPath , [ sourceSelected || '' ] , args )
75+ await jscodeshift ( transformerPath , [ resolve ( sourceSelected ) ] , args )
7376}
0 commit comments