44 * @usage
55 * quant redirect <from> <to> [status]
66 */
7- import { text , select , isCancel } from '@clack/prompts' ;
7+ import { text , select , isCancel , confirm } from '@clack/prompts' ;
8+ import color from 'picocolors' ;
89import config from '../config.js' ;
910import client from '../quant-client.js' ;
1011import isMD5Match from '../helper/is-md5-match.js' ;
12+ import deleteResponse from '../helper/deleteResponse.js' ;
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 ]
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
3347 } ) ;
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' ,
@@ -68,7 +93,7 @@ const command = {
6893 if ( isCancel ( status ) ) return null ;
6994 }
7095
71- return { from, to, status } ;
96+ return { from, to, status, delete : false , force : false } ;
7297 } ,
7398
7499 async handler ( args ) {
@@ -89,11 +114,36 @@ const command = {
89114 const status = args . status || 302 ;
90115
91116 try {
92- await quant . redirect ( args . from , args . to , null , status ) ;
93- return `Created redirect from ${ args . from } to ${ args . to } (${ status } )` ;
117+ if ( args . delete ) {
118+ if ( ! args . force ) {
119+ const shouldDelete = await confirm ( {
120+ message : 'This will delete the redirect. Are you sure?' ,
121+ initialValue : false ,
122+ active : 'Yes' ,
123+ inactive : 'No'
124+ } ) ;
125+ if ( isCancel ( shouldDelete ) || ! shouldDelete ) {
126+ throw new Error ( 'Operation cancelled' ) ;
127+ }
128+ }
129+ await quant . delete ( args . from ) ;
130+ return color . green ( `Deleted redirect from ${ args . from } ` ) ;
131+ } else {
132+ await quant . redirect ( args . from , args . to , null , status ) ;
133+ return color . green ( `Created redirect from ${ args . from } to ${ args . to } (${ status } )` ) ;
134+ }
94135 } catch ( err ) {
136+ const [ ok , message ] = deleteResponse ( err ) ;
137+ if ( ok ) {
138+ if ( message === 'success' ) {
139+ return color . green ( 'Redirect was deleted' ) ;
140+ }
141+ if ( message === 'already deleted' ) {
142+ return color . dim ( 'Redirect was already deleted' ) ;
143+ }
144+ }
95145 if ( isMD5Match ( err ) ) {
96- return `Skipped redirect from ${ args . from } to ${ args . to } (already exists)` ;
146+ return color . dim ( `Skipped redirect from ${ args . from } to ${ args . to } (already exists)` ) ;
97147 }
98148 throw new Error ( `Failed to create redirect: ${ err . message } ` ) ;
99149 }
0 commit comments