131 lines
3.0 KiB
JavaScript
131 lines
3.0 KiB
JavaScript
/**
|
|
* 批量替换 image 组件添加长按识别功能
|
|
* 使用方法:在终端执行 node scripts/add-longpress-to-images.js
|
|
*/
|
|
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
// 需要处理的目录
|
|
const dirs = [
|
|
'components',
|
|
'components-diy',
|
|
'pages',
|
|
'pages_goods',
|
|
'pages_order',
|
|
'pages_promotion',
|
|
'pages_tool',
|
|
'uni_modules'
|
|
]
|
|
|
|
// 排除的目录(不需要处理)
|
|
const excludeDirs = [
|
|
'node_modules',
|
|
'.git',
|
|
'unpackage',
|
|
'dist'
|
|
]
|
|
|
|
// 需要排除的图片模式(如图标、背景图等不需要长按识别的)
|
|
const excludePatterns = [
|
|
/icon/i,
|
|
/logo/i,
|
|
/bg/i,
|
|
/background/i,
|
|
/banner/i
|
|
]
|
|
|
|
/**
|
|
* 检查文件是否需要排除
|
|
*/
|
|
function shouldExclude(filePath) {
|
|
return excludeDirs.some(dir => filePath.includes(dir))
|
|
}
|
|
|
|
/**
|
|
* 处理单个 vue 文件
|
|
*/
|
|
function processVueFile(filePath) {
|
|
let content = fs.readFileSync(filePath, 'utf-8')
|
|
let modified = false
|
|
|
|
// 匹配 <image 标签,支持自闭合和非自闭合(不区分大小写)
|
|
// 匹配 <image ... > 或 <image ... />
|
|
const imageRegex = /(<image\b[^>]*?)(\/?>)/gi
|
|
|
|
content = content.replace(imageRegex, (match, tagStart, closing) => {
|
|
// 检查是否已经有 show-menu-by-longpress 属性
|
|
if (tagStart.includes('show-menu-by-longpress')) {
|
|
return match
|
|
}
|
|
|
|
// 检查是否应该排除(如图标等)
|
|
const shouldExclude = excludePatterns.some(pattern =>
|
|
tagStart.match(pattern)
|
|
)
|
|
|
|
// 如果需要排除,返回原匹配
|
|
if (shouldExclude) {
|
|
return match
|
|
}
|
|
|
|
// 在标签结束前添加属性
|
|
const newTag = `${tagStart} show-menu-by-longpress${closing}`
|
|
|
|
modified = true
|
|
return newTag
|
|
})
|
|
|
|
if (modified) {
|
|
fs.writeFileSync(filePath, content, 'utf-8')
|
|
console.log(`✓ 已处理: ${filePath}`)
|
|
return 1
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
/**
|
|
* 递归遍历目录
|
|
*/
|
|
function walkDir(dir, callback) {
|
|
if (shouldExclude(dir)) return
|
|
|
|
const files = fs.readdirSync(dir)
|
|
|
|
files.forEach(file => {
|
|
const filePath = path.join(dir, file)
|
|
|
|
if (shouldExclude(filePath)) return
|
|
|
|
const stat = fs.statSync(filePath)
|
|
|
|
if (stat.isDirectory()) {
|
|
walkDir(filePath, callback)
|
|
} else if (file.endsWith('.vue')) {
|
|
callback(filePath)
|
|
}
|
|
})
|
|
}
|
|
|
|
// 主程序
|
|
let totalFiles = 0
|
|
let processedFiles = 0
|
|
|
|
console.log('开始处理图片长按识别功能...\n')
|
|
|
|
dirs.forEach(dir => {
|
|
const fullPath = path.join(__dirname, '..', dir)
|
|
if (fs.existsSync(fullPath)) {
|
|
walkDir(fullPath, (filePath) => {
|
|
totalFiles++
|
|
processedFiles += processVueFile(filePath)
|
|
})
|
|
}
|
|
})
|
|
|
|
console.log(`\n处理完成!`)
|
|
console.log(`总共扫描: ${totalFiles} 个文件`)
|
|
console.log(`成功处理: ${processedFiles} 个文件`)
|
|
console.log(`\n提示:如需排除某些图片,请在 <image> 标签添加 class="no-longpress"`)
|