generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/development' into sdk
- Loading branch information
Showing
21 changed files
with
607 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* The purpose of the script is to ensure that the KV for the worker is properly set on deployment. | ||
* There is currently a bug that makes the environment reset on each deploy, because of a problem with Wrangler not | ||
* parsing the TOML configuration properly. See https://github.com/cloudflare/workers-sdk/issues/5634 | ||
* It seems to only work when the values are set at the root of the TOML, not withing the environments. | ||
* This scripts takes out the Production values for kv_namespaces and rewrites them at the root of the TOML file. | ||
*/ | ||
|
||
import { execSync } from "child_process"; | ||
import * as fs from "fs"; | ||
import * as toml from "toml"; | ||
// @ts-expect-error No typings exist for this package | ||
import * as tomlify from "tomlify-j0.4"; | ||
|
||
const tomlFilePath = "./wrangler.toml"; | ||
const wranglerToml: WranglerConfiguration = toml.parse(fs.readFileSync(tomlFilePath, "utf-8")); | ||
|
||
const NAMESPACE_TITLE = "kv"; | ||
const NAMESPACE_TITLE_WITH_PREFIX = `${wranglerToml.name}-${NAMESPACE_TITLE}`; | ||
const BINDING_NAME = "PLUGIN_CHAIN_STATE"; | ||
|
||
interface Namespace { | ||
id: string; | ||
title: string; | ||
} | ||
|
||
interface WranglerConfiguration { | ||
name: string; | ||
env: { | ||
production: { | ||
kv_namespaces?: { | ||
id: string; | ||
binding: string; | ||
}[]; | ||
}; | ||
dev: { | ||
kv_namespaces?: { | ||
id: string; | ||
binding: string; | ||
}[]; | ||
}; | ||
}; | ||
kv_namespaces: { | ||
id: string; | ||
binding: string; | ||
}[]; | ||
} | ||
|
||
function updateWranglerToml(namespaceId: string) { | ||
// Ensure kv_namespaces array exists | ||
if (!wranglerToml.kv_namespaces) { | ||
wranglerToml.kv_namespaces = []; | ||
} | ||
if (wranglerToml.env.production.kv_namespaces) { | ||
wranglerToml.kv_namespaces = wranglerToml.env.production.kv_namespaces; | ||
delete wranglerToml.env.production.kv_namespaces; | ||
} | ||
if (wranglerToml.env.dev.kv_namespaces) { | ||
delete wranglerToml.env.dev.kv_namespaces; | ||
} | ||
|
||
const existingNamespace = wranglerToml.kv_namespaces.find((o) => o.binding === BINDING_NAME); | ||
if (existingNamespace) { | ||
existingNamespace.id = namespaceId; | ||
} else { | ||
wranglerToml.kv_namespaces.push({ | ||
binding: BINDING_NAME, | ||
id: namespaceId, | ||
}); | ||
} | ||
|
||
fs.writeFileSync(tomlFilePath, tomlify.toToml(wranglerToml, { space: 1 })); | ||
} | ||
|
||
async function main() { | ||
// Check if the namespace exists or create a new one | ||
let namespaceId: string; | ||
try { | ||
const res = execSync(`wrangler kv:namespace create ${NAMESPACE_TITLE}`).toString(); | ||
const newId = res.match(/id = \s*"([^"]+)"/)?.[1]; | ||
if (!newId) { | ||
throw new Error(`The new ID could not be found.`); | ||
} | ||
namespaceId = newId; | ||
console.log(`Namespace created with ID: ${namespaceId}`); | ||
} catch (error) { | ||
const listOutput = JSON.parse(execSync(`wrangler kv:namespace list`).toString()) as Namespace[]; | ||
const existingNamespace = listOutput.find((o) => o.title === NAMESPACE_TITLE_WITH_PREFIX); | ||
if (!existingNamespace) { | ||
throw new Error(`Error creating namespace: ${error}`); | ||
} | ||
namespaceId = existingNamespace.id; | ||
console.log(`Namespace ${NAMESPACE_TITLE_WITH_PREFIX} already exists with ID: ${namespaceId}`); | ||
} | ||
|
||
updateWranglerToml(namespaceId); | ||
} | ||
|
||
main() | ||
.then(() => console.log("Successfully bound namespace.")) | ||
.catch((e) => { | ||
console.error("Error checking or creating namespace:", e); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { GitHubContext } from "../github-context"; | ||
import { getConfig } from "../utils/config"; | ||
|
||
export default async function issueCommentCreated(context: GitHubContext<"issue_comment.created">) { | ||
const body = context.payload.comment.body.trim(); | ||
if (/^\/help$/.test(body)) { | ||
const comments = [ | ||
"### Available Commands\n\n", | ||
"| Command | Description | Example |", | ||
"|---|---|---|", | ||
"| `/help` | List all available commands. | `/help` |", | ||
]; | ||
const configuration = await getConfig(context); | ||
for (const pluginArray of Object.values(configuration.plugins)) { | ||
for (const plugin of pluginArray) { | ||
// Only show plugins that have commands available for the user | ||
if (plugin.command) { | ||
comments.push(`| \`${getContent(plugin.command)}\` | ${getContent(plugin.description)} | \`${getContent(plugin.example)}\` |`); | ||
} | ||
} | ||
} | ||
await context.octokit.issues.createComment({ | ||
body: comments.join("\n"), | ||
issue_number: context.payload.issue.number, | ||
owner: context.payload.repository.owner.login, | ||
repo: context.payload.repository.name, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Ensures that passed content does not break MD display within the table. | ||
*/ | ||
function getContent(content: string | undefined) { | ||
return content ? content.replace("|", "\\|") : "-"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.