feat: convert all scripts to typescript

This commit is contained in:
niraj-khatiwada 2026-04-06 19:46:42 +05:45
parent 9e00532a6b
commit 146f4fd704
3 changed files with 49 additions and 179 deletions

View file

@ -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"]
}
}

View file

@ -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()

View file

@ -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" <<EOF
CompressO v${VERSION} Checksums
================================
ARM64 (Apple Silicon):
File: ${APP_NAME}_${VERSION}_aarch64.dmg
SHA256: ${ARM64_SHA256}
Intel (x86_64):
File: ${APP_NAME}_${VERSION}_x64.dmg
SHA256: ${X64_SHA256}
EOF
echo -e "${GREEN}✓ Checksum file generated: ${CHECKSUMS_FILE}${NC}"
echo ""
# Display the cask file content
echo -e "${GREEN}=== Generated Cask File ===${NC}"
cat "$HOMEBREW_CASK_FILE"
echo ""
# List all historical cask versions
echo -e "${GREEN}=== Historical Cask Versions ===${NC}"
if [ -d "${HOMEBREW_OUTPUT_DIR}/casks" ] && [ -n "$(ls -A ${HOMEBREW_OUTPUT_DIR}/casks 2>/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}"