44 * @usage
55 * quant redirect <from> <to> [status]
66 */
7- const { text, select, isCancel } = require ( '@clack/prompts' ) ;
7+ const { text, select, isCancel, confirm } = require ( '@clack/prompts' ) ;
8+ const color = require ( 'picocolors' ) ;
89const config = require ( '../config' ) ;
910const client = require ( '../quant-client' ) ;
1011const isMD5Match = require ( '../helper/is-md5-match' ) ;
12+ const deleteResponse = require ( '../helper/deleteResponse' ) ;
1113
1214const command = {
1315 command : 'redirect <from> <to> [status]' ,
@@ -30,11 +32,34 @@ const command = {
3032 type : 'number' ,
3133 default : 302 ,
3234 choices : [ 301 , 302 , 303 , 307 , 308 ]
33- } ) ;
35+ } )
36+ . option ( 'delete' , {
37+ describe : 'Delete the redirect' ,
38+ alias : [ 'd' ] ,
39+ type : 'boolean' ,
40+ default : false
41+ } )
42+ . option ( 'force' , {
43+ describe : 'Delete without confirmation' ,
44+ alias : [ 'f' ] ,
45+ type : 'boolean' ,
46+ default : false
47+ } )
3448 } ,
3549
3650 async promptArgs ( providedArgs = { } ) {
51+ let isDelete = providedArgs . delete === true ;
3752 let from = providedArgs . from ;
53+
54+ if ( isDelete ) {
55+ from = await text ( {
56+ message : 'Enter URL to redirect from' ,
57+ validate : value => ! value ? 'From URL is required' : undefined
58+ } ) ;
59+ if ( isCancel ( from ) ) return null ;
60+ return { from, to : null , status : null , delete : true , force : providedArgs . force }
61+ }
62+
3863 if ( ! from ) {
3964 from = await text ( {
4065 message : 'Enter URL to redirect from' ,
@@ -44,7 +69,8 @@ const command = {
4469 }
4570
4671 let to = providedArgs . to ;
47- if ( ! to ) {
72+ // Only prompt for 'to' if --delete is not true
73+ if ( ! to && ! providedArgs . delete ) {
4874 to = await text ( {
4975 message : 'Enter URL to redirect to' ,
5076 validate : value => ! value ? 'To URL is required' : undefined
@@ -68,7 +94,7 @@ const command = {
6894 if ( isCancel ( status ) ) return null ;
6995 }
7096
71- return { from, to, status } ;
97+ return { from, to, status, delete : false , force : false } ;
7298 } ,
7399
74100 async handler ( args ) {
@@ -89,11 +115,36 @@ const command = {
89115 const status = args . status || 302 ;
90116
91117 try {
92- await quant . redirect ( args . from , args . to , null , status ) ;
93- return `Created redirect from ${ args . from } to ${ args . to } (${ status } )` ;
118+ if ( args . delete ) {
119+ if ( ! args . force ) {
120+ const shouldDelete = await confirm ( {
121+ message : 'This will delete the redirect. Are you sure?' ,
122+ initialValue : false ,
123+ active : 'Yes' ,
124+ inactive : 'No'
125+ } ) ;
126+ if ( isCancel ( shouldDelete ) || ! shouldDelete ) {
127+ throw new Error ( 'Operation cancelled' ) ;
128+ }
129+ }
130+ await quant . delete ( args . from ) ;
131+ return color . green ( `Deleted redirect from ${ args . from } ` ) ;
132+ } else {
133+ await quant . redirect ( args . from , args . to , null , status ) ;
134+ return color . green ( `Created redirect from ${ args . from } to ${ args . to } (${ status } )` ) ;
135+ }
94136 } catch ( err ) {
137+ const [ ok , message ] = deleteResponse ( err ) ;
138+ if ( ok ) {
139+ if ( message === "success" ) {
140+ return color . green ( "Redirect was deleted" ) ;
141+ }
142+ if ( message === "already deleted" ) {
143+ return color . dim ( "Redirect was already deleted" ) ;
144+ }
145+ }
95146 if ( isMD5Match ( err ) ) {
96- return `Skipped redirect from ${ args . from } to ${ args . to } (already exists)` ;
147+ return color . dim ( `Skipped redirect from ${ args . from } to ${ args . to } (already exists)` ) ;
97148 }
98149 throw new Error ( `Failed to create redirect: ${ err . message } ` ) ;
99150 }
0 commit comments