Files
mesh-forge/scripts/generate-versions.js

50 lines
1.7 KiB
JavaScript

import { execSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const FIRMWARE_DIR = path.resolve(__dirname, '../vendor/firmware');
const OUTPUT_FILE = path.resolve(__dirname, '../src/constants/versions.ts');
function getVersions() {
try {
// Fetch tags from the meshtastic remote
execSync('git fetch meshtastic --tags', { cwd: FIRMWARE_DIR, encoding: 'utf-8' });
// Run git tag in the firmware directory
const output = execSync('git tag', { cwd: FIRMWARE_DIR, encoding: 'utf-8' });
// Filter and sort tags
// We are looking for tags like v2.5.0...
const tags = output
.split('\n')
.map(tag => tag.trim())
.filter(tag => tag.startsWith('v') && tag.includes('.'))
// Simple sort for now, ideally semver sort but string sort works okay for fixed format vX.Y.Z
// We want reverse sort to show latest first
.sort((a, b) => b.localeCompare(a, undefined, { numeric: true, sensitivity: 'base' }));
return tags;
} catch (error) {
console.error('Error getting git tags:', error);
return [];
}
}
function generateFile(versions) {
const content = `// This file is auto-generated by scripts/generate-versions.js
export const VERSIONS = ${JSON.stringify(versions, null, '\t')} as const;
export type FirmwareVersion = typeof VERSIONS[number];
`;
fs.writeFileSync(OUTPUT_FILE, content);
console.log(`Generated ${OUTPUT_FILE} with ${versions.length} versions`);
}
const versions = getVersions();
generateFile(versions);