|
const fs = require("fs"); |
|
const path = require("path"); |
|
const { execSync } = require("child_process"); |
|
|
|
const root = path.resolve(__dirname, ".."); |
|
const deployFilePath = path.join(root, "deploy"); |
|
|
|
// This script automates the deployment process: |
|
// 1. Checks if the current branch is 'main' |
|
// 2. Creates or removes a 'deploy' file in the root directory |
|
// 3. Commits all changes with the message "deploy" |
|
// 4. Pushes the changes to the remote repository |
|
// This script relies that on Vercel's deploy on push hook. |
|
|
|
console.log("[deploy] Checking current branch..."); |
|
try { |
|
const currentBranch = execSync("git rev-parse --abbrev-ref HEAD", { |
|
encoding: "utf8", |
|
cwd: root, |
|
}).trim(); |
|
if (currentBranch !== "main") { |
|
console.error( |
|
"[deploy] Warning: Current branch is not main. Current branch:", |
|
currentBranch, |
|
); |
|
} |
|
console.log("[deploy] Confirmed current branch is main"); |
|
} catch (error) { |
|
console.error("[deploy] Error checking git branch:", error.message); |
|
process.exit(1); |
|
} |
|
|
|
try { |
|
console.log("[deploy] Starting deployment process..."); |
|
|
|
if (fs.existsSync(deployFilePath)) { |
|
fs.unlinkSync(deployFilePath); |
|
} else { |
|
fs.writeFileSync(deployFilePath, ""); |
|
} |
|
|
|
console.log("[deploy] Committing..."); |
|
|
|
// Run git commands |
|
execSync("git add .", { stdio: "inherit", cwd: root }); |
|
execSync('git commit -m "deploy"', { stdio: "inherit", cwd: root }); |
|
|
|
console.log("[deploy] Pushed."); |
|
execSync("git push", { stdio: "inherit", cwd: root }); |
|
} catch (error) { |
|
console.error("[deploy] Error triggering deployment:", error.message); |
|
} |