/** * 把桌面端的内容资产搬进 Web 工程 * ============================== * Web 版只读这些文件(技能 + 两个插件),不改桌面端仓库里的任何东西: * * <源>/resources/skills → assets/skills * <源>/resources/plugin/cocorobo → assets/plugins/cocorobo * <源>/resources/plugin/cocorobo-brand → assets/plugins/cocorobo-brand * * node scripts/prepare-assets.js [源 resources 目录] * * 两个坑: * 1. 技能里有只读的 SKILL.md(Windows 上 cp 会直接失败),复制后统一加写权限; * 2. .git / node_modules 不复制(体积大且没意义)。 */ import fs from 'node:fs' import fsp from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') const source = path.resolve(process.argv[2] || path.join(root, '..', 'cocorobo-teacher-client', 'resources')) const target = path.join(root, 'assets') const jobs = [ ['skills', path.join(target, 'skills')], ['plugin/cocorobo', path.join(target, 'plugins', 'cocorobo')], ['plugin/cocorobo-brand', path.join(target, 'plugins', 'cocorobo-brand')] ] function makeWritable (dir) { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name) if (entry.isDirectory()) { makeWritable(full); continue } try { fs.chmodSync(full, 0o644) } catch { /* 某些文件系统不支持 */ } } } async function copy (from, to) { await fsp.rm(to, { recursive: true, force: true }) await fsp.mkdir(path.dirname(to), { recursive: true }) await fsp.cp(from, to, { recursive: true, force: true, filter: (src) => { const name = path.basename(src) return name !== '.git' && name !== 'node_modules' && name !== '.DS_Store' } }) makeWritable(to) } const missing = jobs.filter(([rel]) => !fs.existsSync(path.join(source, rel))) if (missing.length) { console.error(`源目录不完整:${source}`) for (const [rel] of missing) console.error(` 缺少 ${rel}`) console.error('用法:node scripts/prepare-assets.js ') process.exit(1) } for (const [rel, to] of jobs) { const from = path.join(source, rel) const started = Date.now() await copy(from, to) const files = countFiles(to) console.log(`✓ ${rel} → ${path.relative(root, to)}(${files} 个文件,${Date.now() - started}ms)`) } function countFiles (dir) { let n = 0 for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { if (entry.isDirectory()) n += countFiles(path.join(dir, entry.name)) else n += 1 } return n } console.log('\n完成。技能只读、插件与桌面端同源;改桌面端的技能后重跑这条命令即可同步。')