|
| 1 | +#!/usr/bin/env node |
| 2 | +import { spawn } from 'node:child_process'; |
| 3 | +import { copyFile, mkdir, readdir, rm } from 'node:fs/promises'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +const rootDir = path.resolve(import.meta.dirname, '..'); |
| 7 | +const websiteDir = path.join(rootDir, 'website'); |
| 8 | +const websiteDistDir = path.join(websiteDir, 'doc_build'); |
| 9 | +const packageDocsDir = path.join(rootDir, 'packages/rstack/dist/docs'); |
| 10 | + |
| 11 | +const run = (command, args) => |
| 12 | + new Promise((resolve, reject) => { |
| 13 | + const child = spawn(command, args, { |
| 14 | + cwd: rootDir, |
| 15 | + env: { |
| 16 | + ...process.env, |
| 17 | + RSPRESS_INJECT_LLMS_HINT: 'false', |
| 18 | + }, |
| 19 | + stdio: 'inherit', |
| 20 | + }); |
| 21 | + |
| 22 | + child.on('error', reject); |
| 23 | + child.on('exit', (code, signal) => { |
| 24 | + if (code === 0) { |
| 25 | + resolve(); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const reason = signal ? `signal ${signal}` : `exit code ${code}`; |
| 30 | + reject(new Error(`${command} ${args.join(' ')} failed with ${reason}.`)); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | +const collectMarkdownFiles = async (directory, relativeDir = '') => { |
| 35 | + const entries = await readdir(directory, { withFileTypes: true }); |
| 36 | + const files = []; |
| 37 | + |
| 38 | + for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) { |
| 39 | + if (entry.isDirectory() && entry.name === 'zh') { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + const relativePath = path.join(relativeDir, entry.name); |
| 44 | + const absolutePath = path.join(directory, entry.name); |
| 45 | + |
| 46 | + if (entry.isDirectory()) { |
| 47 | + files.push(...(await collectMarkdownFiles(absolutePath, relativePath))); |
| 48 | + } else if (entry.isFile() && path.extname(entry.name) === '.md') { |
| 49 | + files.push(relativePath); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return files; |
| 54 | +}; |
| 55 | + |
| 56 | +const pnpmCommand = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm'; |
| 57 | + |
| 58 | +console.log('Building the website...'); |
| 59 | +await run(pnpmCommand, ['--dir', websiteDir, 'build']); |
| 60 | + |
| 61 | +const markdownFiles = await collectMarkdownFiles(websiteDistDir); |
| 62 | +if (markdownFiles.length === 0) { |
| 63 | + throw new Error(`No English Markdown files found in ${websiteDistDir}.`); |
| 64 | +} |
| 65 | + |
| 66 | +await rm(packageDocsDir, { recursive: true, force: true }); |
| 67 | + |
| 68 | +for (const relativePath of markdownFiles) { |
| 69 | + const destination = path.join(packageDocsDir, relativePath); |
| 70 | + await mkdir(path.dirname(destination), { recursive: true }); |
| 71 | + await copyFile(path.join(websiteDistDir, relativePath), destination); |
| 72 | +} |
| 73 | + |
| 74 | +console.log(`Copied ${markdownFiles.length} English Markdown files to ${packageDocsDir}.`); |
0 commit comments