|
| 1 | +import assert from "node:assert"; |
| 2 | +import { confirm, input } from "@inquirer/prompts"; |
| 3 | +import { connect, type Framer } from "framer-api"; |
| 4 | + |
| 5 | +// --- Configuration --- |
| 6 | + |
| 7 | +const projectUrl = process.env["EXAMPLE_PROJECT_URL"]; |
| 8 | +assert(projectUrl, "EXAMPLE_PROJECT_URL environment variable is required"); |
| 9 | + |
| 10 | +const intervalFromEnv = process.env["INTERVAL_MINUTES"]; |
| 11 | + |
| 12 | +// --- Main --- |
| 13 | + |
| 14 | +const minutes = intervalFromEnv |
| 15 | + ? parseInt(intervalFromEnv, 10) |
| 16 | + : parseInt(await input({ message: "Interval in minutes:", default: "10" }), 10); |
| 17 | + |
| 18 | +const shouldStart = |
| 19 | + intervalFromEnv || |
| 20 | + (await confirm({ |
| 21 | + message: `🔄 This will publish and deploy every ${minutes} minute(s). Start?`, |
| 22 | + default: true, |
| 23 | + })); |
| 24 | + |
| 25 | +if (!shouldStart) { |
| 26 | + console.log("🛑 Cancelled."); |
| 27 | + process.exit(0); |
| 28 | +} |
| 29 | + |
| 30 | +console.log(`\n🔄 Will check for changes and publish every ${minutes} minute(s)`); |
| 31 | +console.log(` Press Ctrl+C to stop\n`); |
| 32 | + |
| 33 | +const checkAndPublish = async () => { |
| 34 | + using framer = await connect(projectUrl); |
| 35 | + |
| 36 | + const timestamp = new Date().toLocaleTimeString(); |
| 37 | + const hasChanges = await showChanges(framer); |
| 38 | + |
| 39 | + if (hasChanges) { |
| 40 | + console.log(`[${timestamp}] Publishing...`); |
| 41 | + await publishAndDeploy(framer); |
| 42 | + } else { |
| 43 | + console.log(`[${timestamp}] ⏳ No changes`); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +await checkAndPublish(); |
| 48 | +setInterval(checkAndPublish, minutes * 60 * 1000); |
| 49 | + |
| 50 | +// --- Helper Functions --- |
| 51 | + |
| 52 | +async function showChanges(framer: Framer): Promise<boolean> { |
| 53 | + const changedPaths = await framer.getChangedPaths(); |
| 54 | + const entries = Object.entries(changedPaths); |
| 55 | + const totalChanges = entries.reduce((sum, [, paths]) => sum + paths.length, 0); |
| 56 | + |
| 57 | + if (totalChanges === 0) return false; |
| 58 | + |
| 59 | + console.log(`📄 ${totalChanges} change(s):`); |
| 60 | + for (const [type, paths] of entries) { |
| 61 | + for (const path of paths) { |
| 62 | + console.log(` ${type}: ${path}`); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return true; |
| 67 | +} |
| 68 | + |
| 69 | +async function publishAndDeploy(framer: Framer) { |
| 70 | + const { deployment } = await framer.publish(); |
| 71 | + console.log(`🚀 Published deployment ${deployment.id}`); |
| 72 | + |
| 73 | + const deployedHostnames = await framer.deploy(deployment.id); |
| 74 | + console.log(`✅ Deployed to:`); |
| 75 | + for (const hostname of deployedHostnames) { |
| 76 | + console.log(` https://${hostname.hostname}`); |
| 77 | + } |
| 78 | +} |
0 commit comments