Files
Sable/scripts/icons/symlink.js

183 lines
4.6 KiB
JavaScript
Executable File

#!/usr/bin/env node
//MISE description="Create Tauri icon symlinks"
//MISE raw_args=true
/* eslint-disable no-console */
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { createTextHelpers } from '../utils/console-style.js';
const ROOT = process.cwd();
const appIconsPath = path.join(ROOT, 'src-tauri', 'app-icons.json');
const appIcons = fs.existsSync(appIconsPath)
? JSON.parse(fs.readFileSync(appIconsPath, 'utf8')).icons
: [];
const ANDROID_ICONS = [
'mipmap-hdpi',
'mipmap-mdpi',
'mipmap-xhdpi',
'mipmap-xxhdpi',
'mipmap-xxxhdpi',
]
.flatMap((dir) => [
`${dir}/ic_launcher.png`,
`${dir}/ic_launcher_round.png`,
`${dir}/ic_launcher_foreground.png`,
`${dir}/ic_notification.png`,
])
.concat(['mipmap-anydpi-v26/ic_launcher.xml', 'values/ic_launcher_background.xml'])
.concat(['drawable/ic_notification.xml', 'drawable/notification_icon.xml'])
.concat(
appIcons.flatMap(({ id }) => [
...['mipmap-hdpi', 'mipmap-mdpi', 'mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi'].flatMap(
(dir) => [
`${dir}/ic_launcher_${id}.png`,
`${dir}/ic_launcher_${id}_round.png`,
`${dir}/ic_launcher_${id}_foreground.png`,
]
),
`mipmap-anydpi-v26/ic_launcher_${id}.xml`,
])
);
const IOS_ICONS = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];
function parseArgs(argv) {
let write = false;
let force = false;
argv.forEach((arg) => {
if (arg === '--write') write = true;
if (arg === '--force') force = true;
if (arg === '--help' || arg === '-h') {
console.log(
[
'Usage: node scripts/symlink-icons.js [--write] [--force]',
'',
'Default mode is dry-run.',
'--write Apply changes (create symlinks).',
'--force Overwrite existing symlinks.',
].join('\n')
);
process.exit(0);
}
});
return { write, force };
}
function createSymlink(src, dest, write, force, helpers) {
const { dim, red, green } = helpers;
if (!fs.existsSync(src)) {
console.log(` ${red('!')} ${dim('source missing, skipping:')} ${src}`);
return false;
}
const exists = fs.existsSync(dest);
if (exists && !force) {
console.log(
` ${dim('~')} ${dim(path.relative(process.cwd(), dest))} ${dim('(exists, skipping)')}`
);
return false;
}
if (write) {
fs.mkdirSync(path.dirname(dest), { recursive: true });
try {
fs.unlinkSync(dest);
} catch {
// dest does not exist — nothing to remove
}
const rel = path.relative(path.dirname(dest), src).split(path.sep).join('/');
fs.symlinkSync(rel, dest);
}
console.log(` ${green('+')} ${dim(path.relative(process.cwd(), dest))}`);
return true;
}
function processGroup(label, srcDir, destDir, files, write, force, helpers) {
const { dim, red } = helpers;
if (!fs.existsSync(destDir)) {
console.log(`\n${label}: ${red('destination not found')}, skipping.\n ${dim(destDir)}`);
return;
}
console.log(`\n${label}`);
const results = files.map((file) => {
try {
return createSymlink(
path.join(srcDir, file),
path.join(destDir, file),
write,
force,
helpers
);
} catch (err) {
console.log(` ${red('-')} ${file}: ${err.message}`);
return false;
}
});
const ok = results.filter(Boolean).length;
const verb = write ? 'created' : 'would create';
console.log(` ${dim(`→ ${ok}/${files.length} symlinks ${verb}.`)}`);
}
function main() {
const { write, force } = parseArgs(process.argv.slice(2));
const helpers = createTextHelpers();
processGroup(
'Android',
path.join(ROOT, 'src-tauri', 'icons', 'android'),
path.join(ROOT, 'src-tauri', 'gen', 'android', 'app', 'src', 'main', 'res'),
ANDROID_ICONS,
write,
force,
helpers
);
processGroup(
'iOS',
path.join(ROOT, 'src-tauri', 'icons', 'ios'),
path.join(ROOT, 'src-tauri', 'gen', 'apple', 'Assets.xcassets', 'AppIcon.appiconset'),
IOS_ICONS,
write,
force,
helpers
);
const mode = write ? 'Applied' : 'Dry run';
console.log(`\n${mode}.`);
if (!write) console.log('Re-run with --write to apply changes.');
}
main();