Skip to content

Commit 65c2e4d

Browse files
committed
Feat: Add redirect delete handling.
1 parent 02fd0f0 commit 65c2e4d

File tree

3 files changed

+95
-20
lines changed

3 files changed

+95
-20
lines changed

src/commands/delete.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,13 @@ const command = {
100100
} catch (err) {
101101
// If we have a response in the error message, try to parse it
102102
try {
103-
const match = err.message.match(/Response: (.*)/s);
104-
if (match) {
105-
const responseData = JSON.parse(match[1]);
106-
107-
// Check if this was actually a successful deletion
108-
if (!responseData.error && responseData.meta && responseData.meta[0]) {
109-
const meta = responseData.meta[0];
110-
if (meta.deleted) {
111-
return color.green(`Successfully removed [${args.path}]`);
112-
}
113-
if (meta.deleted_timestamp) {
114-
return color.dim(`Path [${args.path}] was already deleted`);
115-
}
103+
const [ok, message] = deleteResponse(err);
104+
if (ok) {
105+
if (message === "success") {
106+
return color.green(`Successfully removed [${args.path}]`);
107+
}
108+
if (message === "already deleted") {
109+
return color.dim(`Path [${args.path}] was already deleted`);
116110
}
117111
}
118112
} catch (parseError) {

src/commands/redirect.js

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
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');
89
const config = require('../config');
910
const client = require('../quant-client');
1011
const isMD5Match = require('../helper/is-md5-match');
12+
const deleteResponse = require('../helper/deleteResponse');
1113

1214
const 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
}

src/helper/deleteResponse.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Handle the delete response.
3+
*/
4+
5+
/**
6+
* Handle the delete response.
7+
* @param {*} error
8+
* @returns [boolean, string]
9+
* - Success, if the delete was successful
10+
* - Message, the message from the response
11+
*/
12+
const deleteResponse = function(error) {
13+
const reponseData = error.message.match(/Response: (.*)/s);
14+
if (reponseData) {
15+
const responseData = JSON.parse(reponseData[1]);
16+
if (responseData.meta && responseData.meta[0]) {
17+
const meta = responseData.meta[0];
18+
if (meta.deleted) {
19+
return [true, "success"];
20+
}
21+
if (meta.deleted_timestamp) {
22+
return [true, "already deleted"];
23+
}
24+
}
25+
}
26+
27+
return [false, "unknown error"];
28+
}
29+
30+
module.exports = deleteResponse;

0 commit comments

Comments
 (0)