forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete.ts
86 lines (75 loc) · 2.24 KB
/
delete.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import path from "path";
import { fetchResult } from "./cfetch";
import { findWranglerToml, readConfig } from "./config";
import { confirm } from "./dialogs";
import { CI } from "./is-ci";
import isInteractive from "./is-interactive";
import { logger } from "./logger";
import * as metrics from "./metrics";
import { requireAuth } from "./user";
import { getScriptName, printWranglerBanner } from "./index";
import type { ConfigPath } from "./index";
import type { YargsOptionsToInterface } from "./yargs-types";
import type { Argv, ArgumentsCamelCase } from "yargs";
export function deleteOptions(yargs: Argv) {
return yargs
.option("env", {
type: "string",
requiresArg: true,
describe: "Perform on a specific environment",
alias: "e",
})
.positional("script", {
describe: "The path to an entry point for your worker",
type: "string",
requiresArg: true,
})
.option("name", {
describe: "Name of the worker",
type: "string",
requiresArg: true,
})
.option("dry-run", {
describe: "Don't actually delete",
type: "boolean",
})
.option("legacy-env", {
type: "boolean",
describe: "Use legacy environments",
hidden: true,
});
}
type DeleteArgs = YargsOptionsToInterface<typeof deleteOptions>;
export async function deleteHandler(args: ArgumentsCamelCase<DeleteArgs>) {
await printWranglerBanner();
const configPath =
(args.config as ConfigPath) ||
(args.script && findWranglerToml(path.dirname(args.script)));
const config = readConfig(configPath, args);
await metrics.sendMetricsEvent(
"delete worker script",
{},
{ sendMetrics: config.send_metrics }
);
const accountId = args.dryRun ? undefined : await requireAuth(config);
const scriptName = getScriptName(args, config);
if (args.dryRun) {
logger.log(`--dry-run: exiting now.`);
return;
}
let confirmed = true;
if (isInteractive() || !CI.isCI()) {
confirmed = await confirm(
`Are you sure you want to delete ${scriptName}? This action cannot be undone.`
);
}
if (confirmed) {
await fetchResult(
`/accounts/${accountId}/workers/services/${scriptName}`,
{ method: "DELETE" },
new URLSearchParams({ force: "true" })
);
logger.log("Successfully deleted", scriptName);
}
// TODO: maybe delete sites/assets kv namespace as well?
}