_sec_component.mjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { readFileSync } from 'node:fs'
  2. import { fileURLToPath, pathToFileURL } from 'node:url'
  3. import { dirname, resolve } from 'node:path'
  4. import { JSDOM, VirtualConsole } from 'jsdom'
  5. const HERE = dirname(fileURLToPath(import.meta.url))
  6. const GATEWAY = process.argv[2]
  7. const CLASSIC = resolve(HERE, '../../../release/web/detail_v2.html')
  8. const state = { apis: {}, cache: new Map() }
  9. function installFetch(window, state) {
  10. window.fetch = async (input) => {
  11. const raw = typeof input === 'string' ? input : (input && input.url) || String(input)
  12. const url = raw.startsWith('http') ? raw : GATEWAY + (raw.startsWith('/') ? raw : '/' + raw)
  13. if (state.cache.has(url)) { const c = state.cache.get(url); return { ok: true, status: 200, headers: new Map(), text: async () => c, json: async () => JSON.parse(c) } }
  14. const r = await fetch(url); const b = await r.text(); if (r.ok) state.cache.set(url, b)
  15. return { ok: r.ok, status: r.status, headers: r.headers, text: async () => b, json: async () => JSON.parse(b) }
  16. }
  17. }
  18. const dom = new JSDOM(readFileSync(CLASSIC, 'utf8'), { url: GATEWAY + '/detail/v2#tab=component', runScripts: 'dangerously', pretendToBeVisual: true, virtualConsole: new VirtualConsole(), beforeParse: (w) => installFetch(w, state) })
  19. const doc = dom.window.document
  20. for (let i = 0; i < 120; i++) { const r = doc.getElementById('root'); if (r && r.querySelector('.page-head')) break; await new Promise((x) => setTimeout(x, 500)) }
  21. let last = -1, same = 0
  22. for (let i = 0; i < 40; i++) { const r = doc.getElementById('root'); const n = r ? r.querySelectorAll('*').length : 0; if (n === last) { if (++same >= 3) break } else same = 0; last = n; await new Promise((x) => setTimeout(x, 700)) }
  23. function sections(root, tag) {
  24. const all = Array.from(root.querySelectorAll('*'))
  25. console.log(tag + '_TOTAL ' + all.length)
  26. all.forEach((el, i) => {
  27. if (el.tagName.toLowerCase() === 'section') {
  28. const kids = Array.from(el.children).slice(0, 4).map((c) => c.tagName.toLowerCase() + (c.className ? '.' + String(c.className).trim().split(/\s+/).join('.') : ''))
  29. console.log(tag + '_SEC #' + i + ' kids=' + JSON.stringify(kids))
  30. }
  31. })
  32. }
  33. sections(doc.getElementById('root'), 'BASE')
  34. const fleetRaw = state.cache.get(GATEWAY + '/api/fleet')
  35. const entry = pathToFileURL(resolve(HERE, '../target/ssr/entry-server.js')).href
  36. const html = await (await import(entry)).renderTab(fleetRaw ? JSON.parse(fleetRaw) : null, 'component', null, null, null, {})
  37. const vd = new JSDOM('<div id="wrap">' + html + '</div>')
  38. const wrap = vd.window.document.getElementById('wrap')
  39. const nav = wrap.querySelector('.wb-tabs'); if (nav) nav.remove()
  40. sections(wrap, 'VUE')