mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-08 10:56:15 -08:00
42 lines
2.6 KiB
TypeScript
42 lines
2.6 KiB
TypeScript
import type { DocumentationExample } from './parser.ts'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import { Script } from 'node:vm'
|
|
import { build } from 'esbuild'
|
|
import { extractExamples } from './parser.ts'
|
|
|
|
export function collectExamples(directory: string, prefix = ''): DocumentationExample[] {
|
|
return fs.readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name, 'en')).flatMap((entry) => {
|
|
if (['en', '.vitepress', 'public', 'plugin'].includes(entry.name))
|
|
return []
|
|
const relative = prefix ? `${prefix}/${entry.name}` : entry.name
|
|
const file = path.join(directory, entry.name)
|
|
if (entry.isDirectory())
|
|
return collectExamples(file, relative)
|
|
if (!entry.name.endsWith('.md'))
|
|
return []
|
|
const examples = extractExamples(fs.readFileSync(file, 'utf8'), relative)
|
|
for (const example of examples) {
|
|
// eslint-disable-next-line no-new -- Compile syntax without executing documentation code.
|
|
new Script(example.code, { filename: example.id })
|
|
}
|
|
return examples
|
|
})
|
|
}
|
|
|
|
export async function generateDocumentationSmoke(root: string): Promise<Map<string, string>> {
|
|
const examples = collectExamples(path.join(root, 'packages/artplayer-vitepress/docs'))
|
|
const html = fs.readFileSync(path.join(root, 'docs/test/index.html'), 'utf8').replace(/<!--[\s\S]*?-->/g, '')
|
|
const scripts = [...html.matchAll(/<script\b[^>]+\bsrc=["']([^"']+)["']/g)].map(match => match[1]!).filter(url => !['./chai.js', './mocha.js', './test.js'].includes(url))
|
|
if (!scripts.some(url => /\/artplayer\.js$/.test(url)))
|
|
throw new Error('Documentation smoke page must load the core')
|
|
const bundle = await build({ stdin: { contents: `import { registerDocumentationSmoke } from './runtime.ts';\nregisterDocumentationSmoke(${JSON.stringify(examples)}, ${JSON.stringify(scripts)});`, resolveDir: path.join(root, 'scripts/docs-smoke'), sourcefile: 'documentation-smoke-entry.ts', loader: 'ts' }, bundle: true, write: false, platform: 'browser', format: 'iife', target: 'es2020', legalComments: 'none', logLevel: 'silent' })
|
|
const script = bundle.outputFiles?.[0]?.text
|
|
if (!script)
|
|
throw new Error('Documentation smoke build produced no script')
|
|
return new Map([
|
|
['docs/test/test.js', `// Generated by yarn build:test. Readiness smoke only; do not edit.\n${script}`],
|
|
['docs/test/examples.json', `${JSON.stringify({ scope: 'Chinese Run Code JavaScript; excludes en/plugin/public/.vitepress. Waits for created players to become ready, observes errors until that boundary, then destroys the frame. Not complete feature, delayed-callback or interaction acceptance.', examples, scripts }, null, 2)}\n`],
|
|
])
|
|
}
|