|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Builds one of the two doc sites in this repository, then serves it locally: |
| 4 | +// |
| 5 | +// node scripts/preview.mjs node -> ./out served on http://localhost:3000 |
| 6 | +// node scripts/preview.mjs docs -> ./www/out served on http://localhost:3001 |
| 7 | +// |
| 8 | +// The `node` build mirrors `scripts/vercel-build.sh` and needs the Node.js |
| 9 | +// source clone in `./node` (see `scripts/vercel-prepare.sh`); the `docs` build |
| 10 | +// is `scripts/vercel-docs-build.sh`. Press Ctrl+C to stop the server. |
| 11 | + |
| 12 | +import { spawn, spawnSync } from 'node:child_process'; |
| 13 | +import { existsSync, readdirSync, readFileSync } from 'node:fs'; |
| 14 | +import { join } from 'node:path'; |
| 15 | +import process from 'node:process'; |
| 16 | + |
| 17 | +const ROOT = join(import.meta.dirname, '..'); |
| 18 | +const CLI = join(ROOT, 'packages', 'cli', 'bin', 'cli.mjs'); |
| 19 | +const NODE_API_DIR = join(ROOT, 'node', 'doc', 'api'); |
| 20 | + |
| 21 | +const SITES = { |
| 22 | + node: { dir: './out', port: 3000 }, |
| 23 | + docs: { dir: './www/out', port: 3001 }, |
| 24 | +}; |
| 25 | + |
| 26 | +const mode = process.argv[2]; |
| 27 | + |
| 28 | +if (!SITES[mode]) { |
| 29 | + console.error( |
| 30 | + `Usage: node scripts/preview.mjs <${Object.keys(SITES).join('|')}>` |
| 31 | + ); |
| 32 | + process.exit(1); |
| 33 | +} |
| 34 | + |
| 35 | +const run = (command, args) => { |
| 36 | + const result = spawnSync(command, args, { cwd: ROOT, stdio: 'inherit' }); |
| 37 | + |
| 38 | + if (result.error) { |
| 39 | + console.error(`Failed to run \`${command}\`: ${result.error.message}`); |
| 40 | + process.exit(1); |
| 41 | + } |
| 42 | + |
| 43 | + if (result.status !== 0) { |
| 44 | + process.exit(result.status); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +const resolveVersion = () => { |
| 49 | + const tagFile = join(ROOT, '.node-tag'); |
| 50 | + |
| 51 | + if (existsSync(tagFile)) { |
| 52 | + return readFileSync(tagFile, 'utf8').trim(); |
| 53 | + } |
| 54 | + |
| 55 | + const result = spawnSync( |
| 56 | + 'git', |
| 57 | + ['-C', join(ROOT, 'node'), 'describe', '--tags', '--abbrev=0'], |
| 58 | + { encoding: 'utf8' } |
| 59 | + ); |
| 60 | + |
| 61 | + return result.status === 0 ? result.stdout.trim() : undefined; |
| 62 | +}; |
| 63 | + |
| 64 | +const buildNodeDocs = () => { |
| 65 | + if ( |
| 66 | + !existsSync(NODE_API_DIR) || |
| 67 | + readdirSync(NODE_API_DIR).filter(file => file.endsWith('.md')).length === 0 |
| 68 | + ) { |
| 69 | + console.error( |
| 70 | + 'No Node.js API sources in ./node/doc/api. Prepare them first with:\n' + |
| 71 | + ' bash scripts/vercel-prepare.sh\n' + |
| 72 | + '(clones the latest nodejs/node release into ./node)' |
| 73 | + ); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + |
| 77 | + console.log('Building Node.js API docs into ./out …'); |
| 78 | + |
| 79 | + const args = [ |
| 80 | + 'generate', |
| 81 | + '-t', |
| 82 | + 'orama-db', |
| 83 | + '-t', |
| 84 | + 'legacy-json', |
| 85 | + '-t', |
| 86 | + 'llms-txt', |
| 87 | + '-t', |
| 88 | + 'html', |
| 89 | + '-i', |
| 90 | + './node/doc/api/*.md', |
| 91 | + '--ignore', |
| 92 | + './node/doc/api/quic.md', |
| 93 | + '-o', |
| 94 | + './out', |
| 95 | + '-c', |
| 96 | + './node/CHANGELOG.md', |
| 97 | + '--type-map', |
| 98 | + './node/doc/type-map.json', |
| 99 | + '--config-file', |
| 100 | + './beta/doc-kit.config.mjs', |
| 101 | + '--log-level', |
| 102 | + 'debug', |
| 103 | + ]; |
| 104 | + const version = resolveVersion(); |
| 105 | + |
| 106 | + if (version) { |
| 107 | + args.push('-v', version); |
| 108 | + } |
| 109 | + |
| 110 | + run(process.execPath, [CLI, ...args]); |
| 111 | +}; |
| 112 | + |
| 113 | +const buildDocsSite = () => { |
| 114 | + console.log('Building doc-kit docs into ./www/out …'); |
| 115 | + run('bash', [join(ROOT, 'scripts', 'vercel-docs-build.sh')]); |
| 116 | +}; |
| 117 | + |
| 118 | +const { dir, port } = SITES[mode]; |
| 119 | + |
| 120 | +if (mode === 'node') { |
| 121 | + buildNodeDocs(); |
| 122 | +} else { |
| 123 | + buildDocsSite(); |
| 124 | +} |
| 125 | + |
| 126 | +console.log(`\nServing ${dir} on http://localhost:${port} …`); |
| 127 | + |
| 128 | +const server = spawn('npx', ['--yes', 'serve', dir, '-l', String(port)], { |
| 129 | + cwd: ROOT, |
| 130 | + stdio: 'inherit', |
| 131 | +}); |
| 132 | + |
| 133 | +server.on('error', error => { |
| 134 | + console.error(`Failed to start the server: ${error.message}`); |
| 135 | + process.exit(1); |
| 136 | +}); |
| 137 | + |
| 138 | +server.on('exit', code => { |
| 139 | + process.exit(code ?? 0); |
| 140 | +}); |
0 commit comments