| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /**
- * Normalize lobster page date displays to use lobsterTimeFormatter / lobsterTime filter.
- * Run from repo root: node saas-companyui/scripts/normalize-lobster-dates.js
- */
- const fs = require('fs')
- const path = require('path')
- const ROOT = path.resolve(__dirname, '../..')
- const UI_DIRS = [
- path.join(ROOT, 'saas-companyui/src/views/lobster'),
- path.join(ROOT, 'saas-mgnui/src/views/lobster'),
- path.join(ROOT, 'adminui/src/views/lobster')
- ]
- const DATE_PROPS = [
- 'createTime', 'create_time', 'updateTime', 'update_time',
- 'sendTime', 'startTime', 'endTime', 'triggerTime', 'executeTime',
- 'lastConversationTime', 'lastEvolutionTime', 'lastMsgTime', 'last_msg_time'
- ]
- const DATE_PROP_PATTERN = DATE_PROPS.join('|')
- function walk(dir, files = []) {
- if (!fs.existsSync(dir)) return files
- for (const name of fs.readdirSync(dir)) {
- const full = path.join(dir, name)
- if (fs.statSync(full).isDirectory()) walk(full, files)
- else if (name.endsWith('.vue')) files.push(full)
- }
- return files
- }
- function addTableFormatters(content) {
- const re = new RegExp(
- `<el-table-column\\b((?![^>]*:formatter)(?![^>]*slot-scope)[^>]*\\bprop="(${DATE_PROP_PATTERN})"[^>]*?)(\\s*\\/?>)`,
- 'g'
- )
- return content.replace(re, (m, attrs, prop, end) => {
- if (attrs.includes('lobsterTimeFormatter')) return m
- const trimmedEnd = end.trim() === '/' ? ' />' : '>'
- return `<el-table-column${attrs} :formatter="lobsterTimeFormatter"${trimmedEnd}`
- })
- }
- function patchMustache(content) {
- let out = content
- const exprs = [
- 'detail.createTime', 'detail.updateTime', 'detail.create_time', 'detail.update_time',
- 'detail.triggerTime', 'logDetail.createTime', 'customer.createTime',
- 'gepaDetail.triggerTime'
- ]
- for (const expr of exprs) {
- const fallback = new RegExp(`\\{\\{\\s*${expr.replace(/\./g, '\\.')}\\s*\\|\\|\\s*'-'\\s*\\}\\}`, 'g')
- out = out.replace(fallback, `{{ ${expr} | lobsterTime }}`)
- const bare = new RegExp(`\\{\\{\\s*${expr.replace(/\./g, '\\.')}\\s*\\}\\}`, 'g')
- out = out.replace(bare, `{{ ${expr} | lobsterTime }}`)
- }
- out = out.replace(/\{\{\s*scope\.row\.createTime\s*\|\|\s*scope\.row\.create_time\s*\}\}/g,
- '{{ (scope.row.createTime || scope.row.create_time) | lobsterTime }}')
- return out.replace(/\{\{\s*([^|{}]+)\s*\|\s*lobsterTime\s*\|\s*lobsterTime\s*\}\}/g, '{{ $1 | lobsterTime }}')
- }
- function patchTimestamps(content) {
- return content.replace(/:timestamp="([a-zA-Z0-9_.]+)"/g, (m, expr) => {
- if (expr.includes('formatLobsterDateTime') || expr.includes('lobsterTime')) return m
- return `:timestamp="formatLobsterDateTime(${expr})"`
- })
- }
- function patchFmtTimeFilter(content) {
- if (!content.includes('fmtTime(v)')) return content
- return content.replace(
- /fmtTime\(v\)\s*\{\s*return v \? v\.replace\('T', ' '\)\.substring\(0, 19\) : '-'\s*\}/,
- 'fmtTime(v) { return this.formatLobsterDateTime(v) }'
- )
- }
- function patchChatTestFormatTime(content) {
- if (!content.includes('formatTime(date)')) return content
- return content.replace(
- /formatTime\(date\)\s*\{[\s\S]*?\n\s*\}/,
- `formatTime(date) {
- return this.formatLobsterDateTime(date)
- }`
- )
- }
- function patchWorkflowExecution(content) {
- if (!content.includes('formatDateTime(val)')) return content
- let out = content.replace(
- /formatDateTime\(val\)\s*\{\s*\n\s*return parseTime\(val\) \|\| '??'\s*\n\s*\}/,
- `formatDateTime(val) {
- return this.formatLobsterDateTime(val)
- }`
- )
- if (out.includes("import { parseTime } from '@/utils/common'") && !out.includes('parseTime(')) {
- out = out.replace(/import \{ parseTime \} from '@\/utils\/common'\n/, '')
- }
- return out
- }
- function processFile(file) {
- let content = fs.readFileSync(file, 'utf8')
- const original = content
- content = addTableFormatters(content)
- content = patchMustache(content)
- content = patchTimestamps(content)
- content = patchFmtTimeFilter(content)
- content = patchChatTestFormatTime(content)
- content = patchWorkflowExecution(content)
- if (content !== original) {
- fs.writeFileSync(file, content, 'utf8')
- return true
- }
- return false
- }
- let changed = 0
- for (const dir of UI_DIRS) {
- for (const file of walk(dir)) {
- if (processFile(file)) {
- changed++
- console.log('updated', path.relative(ROOT, file))
- }
- }
- }
- console.log('done, files changed:', changed)
|