-
Notifications
You must be signed in to change notification settings - Fork 53
feat(cli): implement alchemy bootstrap
command for automated DOStateStore setup
#499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,107 @@ | ||
import { | ||
intro, | ||
log, | ||
outro, | ||
spinner, | ||
} from "@clack/prompts"; | ||
import { randomBytes } from "node:crypto"; | ||
import pc from "picocolors"; | ||
|
||
import { createCloudflareApi } from "../../src/cloudflare/api.ts"; | ||
import { upsertStateStoreWorker } from "../../src/cloudflare/do-state-store/internal.ts"; | ||
import { throwWithContext } from "../errors.ts"; | ||
import type { BootstrapInput } from "../types.ts"; | ||
|
||
const isTest = process.env.NODE_ENV === "test"; | ||
|
||
function generateSecureToken(): string { | ||
// Generate a 32-byte random token and encode as base64url | ||
return randomBytes(32).toString("base64url"); | ||
} | ||
|
||
function displayTokenInstructions(token: string): void { | ||
log.info(` | ||
${pc.cyan("📋 Setup Instructions:")} | ||
|
||
Add the following line to your ${pc.yellow(".env")} file: | ||
|
||
${pc.green(`ALCHEMY_STATE_TOKEN=${token}`)} | ||
|
||
${pc.gray("If you don't have a .env file, create one in your project root.")}`); | ||
} | ||
|
||
export async function bootstrapAlchemy( | ||
cliOptions: BootstrapInput, | ||
): Promise<void> { | ||
try { | ||
intro(pc.cyan("🧪 Alchemy Bootstrap")); | ||
log.info("Setting up Cloudflare DOStateStore..."); | ||
|
||
const options = { yes: isTest, ...cliOptions }; | ||
|
||
|
||
// Generate secure token | ||
const s = spinner(); | ||
s.start("Generating secure token..."); | ||
const token = generateSecureToken(); | ||
s.stop("Token generated"); | ||
|
||
// Create Cloudflare API client | ||
s.start("Connecting to Cloudflare API..."); | ||
const api = await createCloudflareApi(); | ||
s.stop(`Connected to Cloudflare account: ${pc.green(api.accountId)}`); | ||
|
||
// Deploy DOStateStore worker | ||
const workerName = `alchemy-state-store-${api.accountId}`; | ||
s.start(`Deploying DOStateStore worker (${workerName})...`); | ||
|
||
await upsertStateStoreWorker( | ||
api, | ||
workerName, | ||
token, | ||
options.force ?? false, | ||
); | ||
|
||
s.stop(`DOStateStore worker deployed: ${pc.green(workerName)}`); | ||
|
||
// Display token instructions | ||
displayTokenInstructions(token); | ||
|
||
log.info(`Worker URL: ${pc.cyan(`https://${workerName}.${api.accountId}.workers.dev`)}`); | ||
|
||
outro(pc.green("✅ Bootstrap completed successfully!")); | ||
|
||
log.info(` | ||
${pc.cyan("Next steps:")} | ||
1. Add the token to your ${pc.yellow(".env")} file (see instructions above) | ||
2. Create your ${pc.yellow("alchemy.run.ts")} file | ||
3. Use ${pc.yellow("DOStateStore")} as your state store | ||
4. Run ${pc.yellow("bun ./alchemy.run.ts")} to deploy your app | ||
|
||
${pc.cyan("Example alchemy.run.ts:")} | ||
${pc.gray(`import { alchemy } from "alchemy"; | ||
import { DOStateStore } from "alchemy/cloudflare"; | ||
|
||
const app = alchemy({ | ||
name: "my-app", | ||
stateStore: (scope) => new DOStateStore(scope), | ||
}); | ||
|
||
// Your resources here... | ||
|
||
export default app;`)} | ||
`); | ||
|
||
} catch (error) { | ||
log.error("Bootstrap failed:"); | ||
if (error instanceof Error) { | ||
log.error(`${pc.red("Error:")} ${error.message}`); | ||
if (error.stack && process.env.DEBUG) { | ||
log.error(`${pc.gray("Stack trace:")}\n${error.stack}`); | ||
} | ||
} else { | ||
log.error(pc.red(String(error))); | ||
} | ||
process.exit(1); | ||
} | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's actually
await app.finalize()
and not export default app