import fs from 'node:fs' import path from 'node:path' import notificationRules from '../src/config/notificationRules.js' const rootDir = path.resolve(import.meta.dirname, '..') const targetFile = path.join( rootDir, 'android', 'app', 'src', 'main', 'java', 'com', 'echo', 'app', 'notification', 'NotificationRuleData.kt', ) const toKotlinString = (value) => JSON.stringify(value).replace(/\$/g, '\\$') const toKotlinStringList = (values = []) => values.length ? `listOf(${values.map((value) => toKotlinString(value)).join(', ')})` : 'emptyList()' const toKotlinRegex = (spec) => { if (!spec?.pattern) return 'null' const flags = spec.flags?.includes('i') ? ', setOf(RegexOption.IGNORE_CASE)' : '' return `Regex(${toKotlinString(spec.pattern)}${flags})` } const toRuleBlock = (rule) => ` NotificationRuleDefinition( id = ${toKotlinString(rule.id)}, label = ${toKotlinString(rule.label)}, channels = ${toKotlinStringList(rule.channels)}, keywords = ${toKotlinStringList(rule.keywords)}, requiredTextPatterns = ${toKotlinStringList(rule.requiredTextPatterns)}, direction = NotificationRuleDirection.${rule.direction === 'income' ? 'INCOME' : 'EXPENSE'}, defaultCategory = ${toKotlinString(rule.defaultCategory)}, autoCapture = ${rule.autoCapture ? 'true' : 'false'}, merchantPattern = ${toKotlinRegex(rule.merchantPattern)}, defaultMerchant = ${ rule.defaultMerchant ? toKotlinString(rule.defaultMerchant) : 'null' }, )` const content = `package com.echo.app.notification // Generated from src/config/notificationRules.js by scripts/sync-notification-rules.mjs. internal object NotificationRuleData { val fallbackRules: List = listOf( ${notificationRules.map(toRuleBlock).join(',\n')} ) } ` fs.writeFileSync(targetFile, content) console.log(`[rules:sync] wrote ${path.relative(rootDir, targetFile)}`)