write-current-release.ts
1 // SPDX-FileCopyrightText: 2025 Mass Labs 2 // 3 // SPDX-License-Identifier: MIT 4 5 /** 6 * This script writes the current git commit and/or tag to a JSON file 7 * that can be imported in the rest of the codebase. 8 */ 9 10 import { ensureDir } from "@std/fs"; 11 import * as path from "@std/path"; 12 13 async function getCurrentGitInfo(): Promise< 14 { commit: string; tag: string | null; timestamp: string } 15 > { 16 // Get current commit hash 17 const commitProcess = new Deno.Command("git", { 18 args: ["rev-parse", "HEAD"], 19 stdout: "piped", 20 stderr: "piped", 21 }); 22 23 const commitResult = await commitProcess.output(); 24 const commit = new TextDecoder().decode(commitResult.stdout).trim(); 25 26 // Try to get current tag if any 27 const tagProcess = new Deno.Command("git", { 28 args: ["describe", "--tags", "--exact-match", "HEAD"], 29 stdout: "piped", 30 stderr: "piped", 31 }); 32 33 const tagResult = await tagProcess.output(); 34 let tag: string | null = null; 35 36 if (tagResult.code === 0) { 37 tag = new TextDecoder().decode(tagResult.stdout).trim(); 38 } 39 40 // Get commit timestamp 41 const timestampProcess = new Deno.Command("git", { 42 args: ["show", "-s", "--format=%ci", commit], 43 stdout: "piped", 44 stderr: "piped", 45 }); 46 47 const timestampResult = await timestampProcess.output(); 48 const gitTimestamp = new TextDecoder().decode(timestampResult.stdout).trim(); 49 const timestamp = new Date(gitTimestamp).toISOString(); 50 51 return { commit, tag, timestamp }; 52 } 53 54 async function main() { 55 try { 56 const gitInfo = await getCurrentGitInfo(); 57 58 const releaseInfo = { 59 version: gitInfo.tag ?? gitInfo.commit.slice(0, 8), 60 commit: gitInfo.commit, 61 tag: gitInfo.tag, 62 timestamp: gitInfo.timestamp, 63 }; 64 65 // Ensure the directory exists 66 const outputDir = path.join(Deno.cwd(), "src/"); 67 await ensureDir(outputDir); 68 69 // Write to file 70 const outputPath = path.join(outputDir, "release-info.json"); 71 await Deno.writeTextFile(outputPath, JSON.stringify(releaseInfo, null, 2)); 72 73 console.log(`Release info written to ${outputPath}`); 74 } catch (error) { 75 console.error("Failed to write release info:", error); 76 Deno.exit(1); 77 } 78 } 79 80 if (import.meta.main) { 81 main(); 82 }