feat: 优化统一规则逻辑
This commit is contained in:
57
scripts/sync-notification-rules.mjs
Normal file
57
scripts/sync-notification-rules.mjs
Normal file
@@ -0,0 +1,57 @@
|
||||
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<NotificationRuleDefinition> = listOf(
|
||||
${notificationRules.map(toRuleBlock).join(',\n')}
|
||||
)
|
||||
}
|
||||
`
|
||||
|
||||
fs.writeFileSync(targetFile, content)
|
||||
console.log(`[rules:sync] wrote ${path.relative(rootDir, targetFile)}`)
|
||||
Reference in New Issue
Block a user