From 146f4fd7044010503c4334f2b1a84d4fbd7ed905 Mon Sep 17 00:00:00 2001 From: niraj-khatiwada Date: Mon, 6 Apr 2026 19:46:42 +0545 Subject: [PATCH] feat: convert all scripts to typescript --- package.json | 6 +- ...test-json.mjs => generate-latest-json.mts} | 69 +++++--- scripts/homebrew-release.sh | 153 ------------------ 3 files changed, 49 insertions(+), 179 deletions(-) rename scripts/{generate-latest-json.mjs => generate-latest-json.mts} (77%) delete mode 100755 scripts/homebrew-release.sh diff --git a/package.json b/package.json index c5da46a..629364d 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "tauri:build:mac:arm64": "tauri build --target aarch64-apple-darwin", "tauri:build:mac:x64": "tauri build --target x86_64-apple-darwin", "tauri:build:mac": "pnpm tauri:build:mac:arm64 && pnpm tauri:build:mac:x64", - "generate:latest-json": "node scripts/generate-latest-json.mjs", + "generate:latest-json": "node scripts/generate-latest-json.mts", "homebrew:release": "tsx scripts/homebrew-release.mts", "pnpm:devPreinstall": "fnm use", "postinstall": "husky install", @@ -84,8 +84,6 @@ } }, "lint-staged": { - "src/**/*.{js,ts,jsx,tsx}": [ - "biome check --write" - ] + "src/**/*.{js,ts,jsx,tsx}": ["biome check --write"] } } diff --git a/scripts/generate-latest-json.mjs b/scripts/generate-latest-json.mts similarity index 77% rename from scripts/generate-latest-json.mjs rename to scripts/generate-latest-json.mts index 708046f..2171fbd 100644 --- a/scripts/generate-latest-json.mjs +++ b/scripts/generate-latest-json.mts @@ -12,34 +12,56 @@ * Output: Creates a latest.json file in the root directory */ -import fs from 'fs' -import path from 'path' -import { fileURLToPath } from 'url' +import * as fs from 'node:fs' +import * as path from 'node:path' +import { fileURLToPath } from 'node:url' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) // Configuration -const CONFIG = { +interface Config { + author: string + repo: string + appName: string +} + +const CONFIG: Config = { author: 'codeforreal1', repo: 'compressO', appName: 'CompressO', } +// Types +interface PlatformInfo { + signature: string | null + url: string +} + +interface Platforms { + [key: string]: PlatformInfo +} + +interface LatestJson { + version: string + notes: string + pub_date: string + platforms: Platforms +} + /** * Get the latest version from package.json */ -function getVersion() { - const packageJson = JSON.parse( - fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf-8'), - ) +function getVersion(): string { + const packageJsonPath = path.join(__dirname, '..', 'package.json') + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) return packageJson.version } /** * Get changelog for the specified version from CHANGELOG.md */ -function getChangelog(version) { +function getChangelog(version: string): string { const changelogPath = path.join(__dirname, '..', 'CHANGELOG.md') if (!fs.existsSync(changelogPath)) { @@ -59,7 +81,7 @@ function getChangelog(version) { return 'No changelog available' } - const changelogLines = [] + const changelogLines: string[] = [] for (let i = versionLineIndex + 1; i < lines.length; i++) { const line = lines[i] @@ -71,12 +93,12 @@ function getChangelog(version) { changelogLines.push(line) } - while (changelogLines.length > 0 && changelogLines[0].trim() === '') { + while (changelogLines.length > 0 && changelogLines[0]!.trim() === '') { changelogLines.shift() } while ( changelogLines.length > 0 && - changelogLines[changelogLines.length - 1].trim() === '' + changelogLines[changelogLines.length - 1]!.trim() === '' ) { changelogLines.pop() } @@ -87,14 +109,14 @@ function getChangelog(version) { /** * Get current UTC time in ISO 8601 format */ -function getCurrentUTCTime() { +function getCurrentUTCTime(): string { return new Date().toISOString() } /** * Read signature file content */ -function readSignature(filePath) { +function readSignature(filePath: string): string | null { try { if (fs.existsSync(filePath)) { const content = fs.readFileSync(filePath, 'utf-8') @@ -103,7 +125,7 @@ function readSignature(filePath) { console.warn(`Warning: Signature file not found at ${filePath}`) return null } catch (error) { - console.error(`Error reading signature file:`, error) + console.error('Error reading signature file:', error) return null } } @@ -111,9 +133,9 @@ function readSignature(filePath) { /** * Generate platform-specific URLs */ -function generatePlatformUrls(version) { +function generatePlatformUrls(version: string): Platforms { const baseUrl = `https://github.com/${CONFIG.author}/${CONFIG.repo}/releases/download/${version}` - const platforms = {} + const platforms: Platforms = {} // macOS - Apple Silicon (aarch64) platforms['darwin-aarch64'] = { @@ -140,14 +162,13 @@ function generatePlatformUrls(version) { } // Linux - AppImage (universal) - const appImageItem = { + const appImageItem: PlatformInfo = { signature: readSignature( `./src-tauri/target/release/bundle/appimage/${CONFIG.appName}_${version}_amd64.AppImage.sig`, ), url: `${baseUrl}/${CONFIG.appName}_${version}_amd64.AppImage`, } platforms['linux-x86_64'] = appImageItem - platforms['linux-x86_64-appimage'] = appImageItem // Linux - x86_64 (deb) @@ -164,7 +185,11 @@ function generatePlatformUrls(version) { /** * Generate the latest.json content */ -function generateLatestJson(version, changelog, pubDate) { +function generateLatestJson( + version: string, + changelog: string, + pubDate: string, +): LatestJson { const platforms = generatePlatformUrls(version) return { @@ -178,14 +203,14 @@ function generateLatestJson(version, changelog, pubDate) { /** * Write JSON to file with proper formatting */ -function writeJsonFile(filePath, data) { +function writeJsonFile(filePath: string, data: LatestJson): void { fs.writeFileSync(filePath, JSON.stringify(data, null, 2) + '\n', 'utf-8') } /** * Main function */ -function main() { +function main(): void { console.log('> Generating `latest.json` for Tauri updater\n') const version = getVersion() diff --git a/scripts/homebrew-release.sh b/scripts/homebrew-release.sh deleted file mode 100755 index d514751..0000000 --- a/scripts/homebrew-release.sh +++ /dev/null @@ -1,153 +0,0 @@ -#!/bin/bash - -# Homebrew Cask Release Script for CompressO -# This script generates Homebrew cask files for both architectures - -set -e - -# Colors for output -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -NC='\033[0m' # No Color - -# Get version from package.json -VERSION=$(node -p "require('./package.json').version") -APP_NAME="CompressO" -AUTHOR="codeforreal1" -REPO="compressO" - -echo -e "${GREEN}=== Homebrew Cask Release Script ===${NC}" -echo -e "Version: ${YELLOW}${VERSION}${NC}" -echo "" - -# Define paths -ARM64_DMG_PATH="./src-tauri/target/aarch64-apple-darwin/release/bundle/dmg/${APP_NAME}_${VERSION}_aarch64.dmg" -X64_DMG_PATH="./src-tauri/target/x86_64-apple-darwin/release/bundle/dmg/${APP_NAME}_${VERSION}_x64.dmg" -HOMEBREW_OUTPUT_DIR="./homebrew" -HOMEBREW_CASK_FILE="${HOMEBREW_OUTPUT_DIR}/compresso.rb" -HOMEBREW_TEMPLATE_FILE="${HOMEBREW_OUTPUT_DIR}/compresso.rb.template" - -# Check if DMG files exist -echo -e "${GREEN}Checking for DMG files...${NC}" - -if [ ! -f "$ARM64_DMG_PATH" ]; then - echo -e "${RED}Error: ARM64 DMG not found at ${ARM64_DMG_PATH}${NC}" - echo "Please build the ARM64 version first using: npm run tauri:build -- --target aarch64-apple-darwin" - exit 1 -fi - -if [ ! -f "$X64_DMG_PATH" ]; then - echo -e "${RED}Error: x64 DMG not found at ${X64_DMG_PATH}${NC}" - echo "Please build the x64 version first using: npm run tauri:build -- --target x86_64-apple-darwin" - exit 1 -fi - -echo -e "${GREEN}✓ ARM64 DMG found${NC}" -echo -e "${GREEN}✓ x64 DMG found${NC}" -echo "" - -# Calculate SHA256 checksums -echo -e "${GREEN}Calculating SHA256 checksums...${NC}" -ARM64_SHA256=$(shasum -a 256 "$ARM64_DMG_PATH" | awk '{print $1}') -X64_SHA256=$(shasum -a 256 "$X64_DMG_PATH" | awk '{print $1}') - -echo -e "ARM64 SHA256: ${YELLOW}${ARM64_SHA256}${NC}" -echo -e "x64 SHA256: ${YELLOW}${X64_SHA256}${NC}" -echo "" - -# Create the Homebrew cask directories -mkdir -p "$HOMEBREW_OUTPUT_DIR" -mkdir -p "${HOMEBREW_OUTPUT_DIR}/casks" - -# Check if template exists -if [ ! -f "$HOMEBREW_TEMPLATE_FILE" ]; then - echo -e "${RED}Error: Template file not found at ${HOMEBREW_TEMPLATE_FILE}${NC}" - exit 1 -fi - -# Generate the cask file from template -echo -e "${GREEN}Generating Homebrew cask file from template...${NC}" - -# Read template and replace placeholders -CASK_CONTENT=$(sed "s/{{VERSION}}/${VERSION}/g" "$HOMEBREW_TEMPLATE_FILE") -CASK_CONTENT=$(echo "$CASK_CONTENT" | sed "s/{{ARM64_SHA256}}/${ARM64_SHA256}/g") -CASK_CONTENT=$(echo "$CASK_CONTENT" | sed "s/{{X64_SHA256}}/${X64_SHA256}/g") - -# Write to main cask file -echo "$CASK_CONTENT" > "$HOMEBREW_CASK_FILE" - -# Write versioned backup (with versioned cask name) -VERSIONED_CASK_FILE="${HOMEBREW_OUTPUT_DIR}/casks/compresso-${VERSION}.rb" -VERSIONED_CASK_CONTENT=$(echo "$CASK_CONTENT" | sed "s/cask \"compresso\"/cask \"compresso@${VERSION}\"/") -echo "$VERSIONED_CASK_CONTENT" > "$VERSIONED_CASK_FILE" - -echo -e "${GREEN}✓ Main cask file generated: ${HOMEBREW_CASK_FILE}${NC}" -echo -e "${GREEN}✓ Versioned backup created: ${VERSIONED_CASK_FILE}${NC}" -echo "" - -# Create a checksum info file for reference (overwrites existing) -CHECKSUMS_FILE="${HOMEBREW_OUTPUT_DIR}/checksums.txt" -cat > "$CHECKSUMS_FILE" </dev/null)" ]; then - echo "Found $(ls -1 ${HOMEBREW_OUTPUT_DIR}/casks/*.rb 2>/dev/null | wc -l | tr -d ' ') versioned cask file(s):" - ls -1t "${HOMEBREW_OUTPUT_DIR}/casks"/*.rb 2>/dev/null | while read -r file; do - filename=$(basename "$file") - version=$(echo "$filename" | sed 's/compresso-\(.*\)\.rb/\1/') - size=$(du -h "$file" | awk '{print $1}') - echo -e " ${YELLOW}• v${version}${NC} (${size})" - done -else - echo " No historical cask files found" -fi -echo "" - -# Instructions -echo -e "${GREEN}=== Next Steps ===${NC}" -echo "" -echo "1. Test the cask locally:" -echo -e " ${YELLOW}brew install --cask --debug ${HOMEBREW_CASK_FILE}${NC}" -echo "" -echo "2. Upload DMG files to GitHub Releases:" -echo -e " ${YELLOW}gh release create v${VERSION} ${ARM64_DMG_PATH} ${X64_DMG_PATH} --title \"v${VERSION}\" --notes \"Release v${VERSION}\"${NC}" -echo "" -echo " You can also attach the checksums file for reference:" -echo -e " ${YELLOW}gh release upload v${VERSION} ${CHECKSUMS_FILE}${NC}" -echo "" -echo "3. Versioned cask backup saved at:" -echo -e " ${YELLOW}${VERSIONED_CASK_FILE}${NC}" -echo "" -echo "4. Submit to Homebrew:" -echo -e " ${YELLOW}brew info compresso # Check if cask already exists${NC}" -echo -e " ${YELLOW}brew audit --cask --online ${HOMEBREW_CASK_FILE} # Audit the cask${NC}" -echo -e " ${YELLOW}brew style --cask ${HOMEBREW_CASK_FILE} # Check style${NC}" -echo "" -echo "5. If the cask doesn't exist, submit a PR to:" -echo -e " ${YELLOW}https://github.com/Homebrew/homebrew-cask${NC}" -echo "" -echo "6. If the cask exists, update it:" -echo -e " ${YELLOW}brew bump-cask-pr compresso --version=${VERSION}${NC}" -echo "" -echo -e "${GREEN}=== Script Complete ===${NC}"