tools: add sync-version.js to sync package.json -> Tauri files
- Add tools/sync-version.js script to read root package.json version and update src-tauri/tauri.conf.json and src-tauri/Cargo.toml. - Update only the [package] version line in Cargo.toml to preserve formatting. - Include JSON read/write helpers and basic error handling/reporting.
This commit is contained in:
60
tools/sync-version.js
Normal file
60
tools/sync-version.js
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env node
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const repoRoot = process.cwd();
|
||||
|
||||
function readJson(p) {
|
||||
return JSON.parse(fs.readFileSync(p, 'utf8'));
|
||||
}
|
||||
|
||||
function writeJson(p, obj) {
|
||||
fs.writeFileSync(p, JSON.stringify(obj, null, 2) + '\n', 'utf8');
|
||||
}
|
||||
|
||||
function updateCargoTomlVersion(cargoTomlPath, version) {
|
||||
const input = fs.readFileSync(cargoTomlPath, 'utf8');
|
||||
|
||||
// Replace only the [package] version line.
|
||||
const packageBlockStart = input.indexOf('[package]');
|
||||
if (packageBlockStart === -1) {
|
||||
throw new Error('Could not find [package] in Cargo.toml');
|
||||
}
|
||||
|
||||
const packageBlockEnd = input.indexOf('\n[', packageBlockStart + 1);
|
||||
const blockEnd = packageBlockEnd === -1 ? input.length : packageBlockEnd;
|
||||
const pkgBlock = input.slice(packageBlockStart, blockEnd);
|
||||
|
||||
const versionRe = /^version\s*=\s*"([^"]*)"/m;
|
||||
const m = pkgBlock.match(versionRe);
|
||||
if (!m) {
|
||||
throw new Error('Could not find version line in Cargo.toml [package] block');
|
||||
}
|
||||
|
||||
const replaced = pkgBlock.replace(versionRe, `version = "${version}"`);
|
||||
|
||||
const output = input.slice(0, packageBlockStart) + replaced + input.slice(blockEnd);
|
||||
fs.writeFileSync(cargoTomlPath, output, 'utf8');
|
||||
}
|
||||
|
||||
try {
|
||||
const rootPkgPath = path.join(repoRoot, 'package.json');
|
||||
const tauriConfPath = path.join(repoRoot, 'src-tauri', 'tauri.conf.json');
|
||||
const cargoTomlPath = path.join(repoRoot, 'src-tauri', 'Cargo.toml');
|
||||
|
||||
const rootPkg = readJson(rootPkgPath);
|
||||
if (!rootPkg.version) throw new Error('Root package.json has no version');
|
||||
|
||||
const version = String(rootPkg.version);
|
||||
|
||||
const tauriConf = readJson(tauriConfPath);
|
||||
tauriConf.version = version;
|
||||
writeJson(tauriConfPath, tauriConf);
|
||||
|
||||
updateCargoTomlVersion(cargoTomlPath, version);
|
||||
|
||||
console.log(`Synced Tauri version to ${version}`);
|
||||
} catch (e) {
|
||||
console.error('sync-version failed:', e?.message || e);
|
||||
process.exit(1);
|
||||
}
|
||||
54
tools/write-build-flag.js
Normal file
54
tools/write-build-flag.js
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env node
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const cmd = process.argv[2] || 'set';
|
||||
const repoRoot = process.cwd();
|
||||
const dst = path.join(repoRoot, 'src', 'build-info.json');
|
||||
|
||||
function getPackageVersion() {
|
||||
try {
|
||||
const pkgPath = path.join(repoRoot, 'package.json');
|
||||
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
||||
return pkg && pkg.version ? String(pkg.version) : null;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function computeDebugFlag() {
|
||||
const envVal = process.env.RADIO_DEBUG_DEVTOOLS;
|
||||
if (envVal === '1' || envVal === 'true') return true;
|
||||
const arg = (process.argv[3] || '').toLowerCase();
|
||||
return arg === 'debug' || arg === '--debug';
|
||||
}
|
||||
|
||||
if (cmd === 'set') {
|
||||
try {
|
||||
const version = getPackageVersion();
|
||||
const debug = computeDebugFlag();
|
||||
const payload = {
|
||||
version,
|
||||
debug,
|
||||
builtAt: new Date().toISOString(),
|
||||
};
|
||||
fs.writeFileSync(dst, JSON.stringify(payload, null, 2) + '\n', 'utf8');
|
||||
console.log(`Wrote build-info.json (debug=${debug}${version ? `, version=${version}` : ''})`);
|
||||
process.exit(0);
|
||||
} catch (e) {
|
||||
console.error('Failed to write build-info.json', e);
|
||||
process.exit(1);
|
||||
}
|
||||
} else if (cmd === 'clear') {
|
||||
try {
|
||||
if (fs.existsSync(dst)) fs.unlinkSync(dst);
|
||||
console.log('Removed build-info.json');
|
||||
process.exit(0);
|
||||
} catch (e) {
|
||||
console.error('Failed to remove build-info.json', e);
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
console.error('Unknown command:', cmd);
|
||||
process.exit(2);
|
||||
}
|
||||
Reference in New Issue
Block a user