|
1 | 1 | import { confirm, input, select } from "@inquirer/prompts"; |
2 | | -import { exec, execSync, spawn } from "child_process"; |
| 2 | +import { exec, spawn } from "child_process"; |
| 3 | +import { parse } from "dotenv"; |
3 | 4 | import { readFileSync, unlinkSync, writeFileSync } from "fs"; |
4 | 5 | import ora from "ora"; |
5 | 6 | import path from "path"; |
6 | | -import { parse } from "dotenv"; |
7 | 7 | import toml from "toml"; |
8 | | -// @ts-expect-error No typings exist for this package |
9 | | -import * as tomlify from "tomlify-j0.4"; |
10 | 8 |
|
11 | 9 | interface WranglerConfiguration { |
12 | | - name: string; |
13 | | - env: { |
14 | | - [env: string]: { |
15 | | - kv_namespaces?: { |
16 | | - id: string; |
17 | | - binding: string; |
18 | | - }[]; |
19 | | - }; |
20 | | - }; |
21 | | - kv_namespaces: { |
22 | | - id: string; |
23 | | - binding: string; |
24 | | - }[]; |
| 10 | + env?: Record<string, unknown>; |
25 | 11 | } |
26 | 12 |
|
27 | 13 | const WRANGLER_PATH = path.resolve(__dirname, "..", "node_modules/.bin/wrangler"); |
28 | 14 | const WRANGLER_TOML_PATH = path.resolve(__dirname, "..", "wrangler.toml"); |
29 | | -const BINDING_NAME = "PLUGIN_CHAIN_STATE"; |
30 | 15 |
|
31 | 16 | function checkIfWranglerInstalled() { |
32 | 17 | return new Promise((resolve) => { |
@@ -101,23 +86,6 @@ function wranglerDeploy(env: string | null) { |
101 | 86 | }); |
102 | 87 | } |
103 | 88 |
|
104 | | -function wranglerKvNamespace(projectName: string, namespace: string) { |
105 | | - const kvList = JSON.parse(execSync(`${WRANGLER_PATH} kv namespace list`).toString()) as { id: string; title: string }[]; |
106 | | - const existingNamespace = kvList.find((o) => o.title === namespace || o.title === `${projectName}-${namespace}`); |
107 | | - if (existingNamespace) { |
108 | | - return existingNamespace.id; |
109 | | - } |
110 | | - |
111 | | - const res = execSync(`${WRANGLER_PATH} kv namespace create ${namespace}`).toString(); |
112 | | - |
113 | | - const newId = res.match(/id = \s*"([^"]+)"/)?.[1]; |
114 | | - if (!newId) { |
115 | | - console.log(res); |
116 | | - throw new Error(`The new ID could not be found.`); |
117 | | - } |
118 | | - return newId; |
119 | | -} |
120 | | - |
121 | 89 | void (async () => { |
122 | 90 | const spinner = ora("Checking if Wrangler is installed").start(); |
123 | 91 | const wranglerInstalled = await checkIfWranglerInstalled(); |
@@ -185,40 +153,6 @@ void (async () => { |
185 | 153 | } |
186 | 154 | } |
187 | 155 |
|
188 | | - spinner.start("Setting up KV namespace"); |
189 | | - try { |
190 | | - const kvNamespace = selectedEnv ? `${selectedEnv}-plugin-chain-state` : `plugin-chain-state`; |
191 | | - const namespaceId = wranglerKvNamespace(wranglerToml.name, kvNamespace); |
192 | | - if (selectedEnv) { |
193 | | - const existingBinding = wranglerToml.env[selectedEnv]?.kv_namespaces?.find((o) => o.binding === BINDING_NAME); |
194 | | - if (!existingBinding) { |
195 | | - wranglerToml.env[selectedEnv] = wranglerToml.env[selectedEnv] ?? {}; |
196 | | - wranglerToml.env[selectedEnv].kv_namespaces = wranglerToml.env[selectedEnv].kv_namespaces ?? []; |
197 | | - wranglerToml.env[selectedEnv].kv_namespaces?.push({ |
198 | | - id: namespaceId, |
199 | | - binding: BINDING_NAME, |
200 | | - }); |
201 | | - } else { |
202 | | - existingBinding.id = namespaceId; |
203 | | - } |
204 | | - } else { |
205 | | - const existingBinding = wranglerToml.kv_namespaces.find((o) => o.binding === BINDING_NAME); |
206 | | - if (!existingBinding) { |
207 | | - wranglerToml.kv_namespaces.push({ |
208 | | - id: namespaceId, |
209 | | - binding: BINDING_NAME, |
210 | | - }); |
211 | | - } else { |
212 | | - existingBinding.id = namespaceId; |
213 | | - } |
214 | | - } |
215 | | - writeFileSync(WRANGLER_TOML_PATH, tomlify.toToml(wranglerToml)); |
216 | | - spinner.succeed(`Using KV namespace ${kvNamespace} with ID: ${namespaceId}`); |
217 | | - } catch (err) { |
218 | | - spinner.fail(`Error setting up KV namespace: ${err}`); |
219 | | - process.exit(1); |
220 | | - } |
221 | | - |
222 | 156 | spinner.start("Deploying to Cloudflare Workers").stopAndPersist(); |
223 | 157 | try { |
224 | 158 | await wranglerDeploy(selectedEnv); |
|
0 commit comments