prepare-assets.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * 把桌面端的内容资产搬进 Web 工程
  3. * ==============================
  4. * Web 版只读这些文件(技能 + 两个插件),不改桌面端仓库里的任何东西:
  5. *
  6. * <源>/resources/skills → assets/skills
  7. * <源>/resources/plugin/cocorobo → assets/plugins/cocorobo
  8. * <源>/resources/plugin/cocorobo-brand → assets/plugins/cocorobo-brand
  9. *
  10. * node scripts/prepare-assets.js [源 resources 目录]
  11. *
  12. * 两个坑:
  13. * 1. 技能里有只读的 SKILL.md(Windows 上 cp 会直接失败),复制后统一加写权限;
  14. * 2. .git / node_modules 不复制(体积大且没意义)。
  15. */
  16. import fs from 'node:fs'
  17. import fsp from 'node:fs/promises'
  18. import path from 'node:path'
  19. import { fileURLToPath } from 'node:url'
  20. const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
  21. const source = path.resolve(process.argv[2] || path.join(root, '..', 'cocorobo-teacher-client', 'resources'))
  22. const target = path.join(root, 'assets')
  23. const jobs = [
  24. ['skills', path.join(target, 'skills')],
  25. ['plugin/cocorobo', path.join(target, 'plugins', 'cocorobo')],
  26. ['plugin/cocorobo-brand', path.join(target, 'plugins', 'cocorobo-brand')]
  27. ]
  28. function makeWritable (dir) {
  29. for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
  30. const full = path.join(dir, entry.name)
  31. if (entry.isDirectory()) { makeWritable(full); continue }
  32. try { fs.chmodSync(full, 0o644) } catch { /* 某些文件系统不支持 */ }
  33. }
  34. }
  35. async function copy (from, to) {
  36. await fsp.rm(to, { recursive: true, force: true })
  37. await fsp.mkdir(path.dirname(to), { recursive: true })
  38. await fsp.cp(from, to, {
  39. recursive: true,
  40. force: true,
  41. filter: (src) => {
  42. const name = path.basename(src)
  43. return name !== '.git' && name !== 'node_modules' && name !== '.DS_Store'
  44. }
  45. })
  46. makeWritable(to)
  47. }
  48. const missing = jobs.filter(([rel]) => !fs.existsSync(path.join(source, rel)))
  49. if (missing.length) {
  50. console.error(`源目录不完整:${source}`)
  51. for (const [rel] of missing) console.error(` 缺少 ${rel}`)
  52. console.error('用法:node scripts/prepare-assets.js <cocorobo-teacher-client/resources 的路径>')
  53. process.exit(1)
  54. }
  55. for (const [rel, to] of jobs) {
  56. const from = path.join(source, rel)
  57. const started = Date.now()
  58. await copy(from, to)
  59. const files = countFiles(to)
  60. console.log(`✓ ${rel} → ${path.relative(root, to)}(${files} 个文件,${Date.now() - started}ms)`)
  61. }
  62. function countFiles (dir) {
  63. let n = 0
  64. for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
  65. if (entry.isDirectory()) n += countFiles(path.join(dir, entry.name))
  66. else n += 1
  67. }
  68. return n
  69. }
  70. console.log('\n完成。技能只读、插件与桌面端同源;改桌面端的技能后重跑这条命令即可同步。')