gongxiangp2/scripts/scan-history-materials.ts
2026-05-07 16:37:22 +08:00

80 lines
2.5 KiB
TypeScript

import path from 'path'
import { promises as fs } from 'fs'
import { scanHistoryMaterials } from '@/lib/history-material-import'
import { prisma } from '@/lib/prisma'
function parseArgs() {
const args = process.argv.slice(2)
const getArg = (name: string) => {
const match = args.find((arg) => arg.startsWith(`--${name}=`))
return match ? match.slice(name.length + 3) : ''
}
const materialsRoot = getArg('materials-root') || 'S:\\大货营销策划图片'
const cpRoot = getArg('cp-root') || '\\\\192.168.58.116\\cp-gb'
const yyaRoot = getArg('yya-root') || '\\\\192.168.58.116\\yya'
const output =
getArg('output') || path.join(process.cwd(), 'output', `history-material-scan-${Date.now()}.json`)
const limitRaw = getArg('limit')
const spusRaw = getArg('spus')
return {
materialsRoot,
cpRoot,
yyaRoot,
output,
limit: limitRaw ? Number(limitRaw) : undefined,
spuFilter: spusRaw ? spusRaw.split(',').map((item) => item.trim()).filter(Boolean) : undefined,
}
}
async function main() {
const options = parseArgs()
console.log('历史素材扫描开始')
console.log(`- 素材目录: ${options.materialsRoot}`)
console.log(`- 店铺参考目录: ${options.cpRoot}`)
console.log(`- 辅助参考目录: ${options.yyaRoot}`)
const records = await scanHistoryMaterials(options)
await fs.mkdir(path.dirname(options.output), { recursive: true })
await fs.writeFile(options.output, JSON.stringify(records, null, 2), 'utf8')
const summary = records.reduce(
(acc, record) => {
acc.total += 1
acc[record.status] += 1
return acc
},
{ total: 0, ready: 0, needs_review: 0, skipped: 0 } as Record<string, number>,
)
console.log('\n扫描完成')
console.log(`- 总记录数: ${summary.total}`)
console.log(`- 可直接导入: ${summary.ready}`)
console.log(`- 需要人工确认: ${summary.needs_review}`)
console.log(`- 已跳过: ${summary.skipped}`)
console.log(`- 输出文件: ${options.output}`)
const preview = records.slice(0, 12).map((record) => ({
status: record.status,
spu: record.spu,
store: record.guessedStore,
main: record.mainCount,
sku: record.skuCount,
detail: record.detailCount,
path: record.relativeSourcePath,
}))
console.table(preview)
}
main()
.catch((error) => {
console.error('历史素材扫描失败:', error)
process.exitCode = 1
})
.finally(async () => {
await prisma.$disconnect()
process.exit(process.exitCode ?? 0)
})