|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs/promises'); |
| 4 | +const path = require('path'); |
| 5 | +const os = require('os'); |
| 6 | +const { execFile } = require('child_process'); |
| 7 | +const { promisify } = require('util'); |
| 8 | + |
| 9 | +const execFileAsync = promisify(execFile); |
| 10 | + |
| 11 | +async function runCommand(command, args, options) { |
| 12 | + return execFileAsync(command, args, options); |
| 13 | +} |
| 14 | + |
| 15 | +function parsePackJson(output) { |
| 16 | + const match = output.match(/\[\s*\{[\s\S]*\}\s*\]\s*$/); |
| 17 | + if (!match) { |
| 18 | + throw new Error('npm pack did not produce JSON output'); |
| 19 | + } |
| 20 | + |
| 21 | + const jsonPayload = match[0]; |
| 22 | + return JSON.parse(jsonPayload); |
| 23 | +} |
| 24 | + |
| 25 | +async function run() { |
| 26 | + const repoRoot = path.resolve(__dirname, '..'); |
| 27 | + const cliDir = path.join(repoRoot, 'packages', 'cli'); |
| 28 | + const uiDistDir = process.env.AUTODOCS_UI_DIST_SOURCE |
| 29 | + ? path.resolve(process.env.AUTODOCS_UI_DIST_SOURCE) |
| 30 | + : path.join(repoRoot, 'packages', 'ui', 'dist'); |
| 31 | + const tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'autodocs-cli-smoke-')); |
| 32 | + const extractDir = path.join(tmpRoot, 'extract'); |
| 33 | + const fixtureDir = path.join(tmpRoot, 'fixture'); |
| 34 | + const npmCache = path.join(tmpRoot, 'npm-cache'); |
| 35 | + |
| 36 | + let tarballPath; |
| 37 | + |
| 38 | + try { |
| 39 | + const { stdout: packStdout } = await runCommand('npm', ['pack', '--json'], { |
| 40 | + cwd: cliDir, |
| 41 | + env: { |
| 42 | + ...process.env, |
| 43 | + AUTODOCS_UI_DIST_SOURCE: uiDistDir, |
| 44 | + npm_config_cache: npmCache, |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + const packed = parsePackJson(packStdout); |
| 49 | + const tarballFile = packed[0]?.filename; |
| 50 | + if (!tarballFile) { |
| 51 | + throw new Error('npm pack did not return a tarball filename'); |
| 52 | + } |
| 53 | + |
| 54 | + tarballPath = path.join(cliDir, tarballFile); |
| 55 | + |
| 56 | + await fs.mkdir(extractDir, { recursive: true }); |
| 57 | + await runCommand('tar', ['-xzf', tarballPath, '-C', extractDir], { cwd: repoRoot }); |
| 58 | + |
| 59 | + const extractedPackageDir = path.join(extractDir, 'package'); |
| 60 | + const extractedNodeModulesDir = path.join(extractedPackageDir, 'node_modules'); |
| 61 | + const extractedCoreScopeDir = path.join(extractedNodeModulesDir, '@opensyntaxhq'); |
| 62 | + const extractedCoreLink = path.join(extractedCoreScopeDir, 'autodocs-core'); |
| 63 | + const localCorePackageDir = path.join(repoRoot, 'packages', 'core'); |
| 64 | + |
| 65 | + await fs.mkdir(extractedCoreScopeDir, { recursive: true }); |
| 66 | + await fs.rm(extractedCoreLink, { recursive: true, force: true }); |
| 67 | + await fs.symlink(localCorePackageDir, extractedCoreLink, 'dir'); |
| 68 | + |
| 69 | + const cliEntry = path.join(extractedPackageDir, 'dist', 'index.js'); |
| 70 | + |
| 71 | + await fs.mkdir(path.join(fixtureDir, 'src'), { recursive: true }); |
| 72 | + await fs.writeFile(path.join(fixtureDir, 'src', 'index.ts'), 'export const value = 1;\n', 'utf-8'); |
| 73 | + await fs.writeFile( |
| 74 | + path.join(fixtureDir, 'autodocs.config.json'), |
| 75 | + JSON.stringify( |
| 76 | + { |
| 77 | + include: ['src/**/*.ts'], |
| 78 | + output: { dir: './docs-dist', format: 'static', clean: true }, |
| 79 | + }, |
| 80 | + null, |
| 81 | + 2 |
| 82 | + ), |
| 83 | + 'utf-8' |
| 84 | + ); |
| 85 | + |
| 86 | + const nodePathParts = [path.join(repoRoot, 'node_modules')]; |
| 87 | + if (process.env.NODE_PATH) { |
| 88 | + nodePathParts.push(process.env.NODE_PATH); |
| 89 | + } |
| 90 | + |
| 91 | + await runCommand( |
| 92 | + process.execPath, |
| 93 | + [cliEntry, 'build', '--config', path.join(fixtureDir, 'autodocs.config.json')], |
| 94 | + { |
| 95 | + cwd: fixtureDir, |
| 96 | + env: { |
| 97 | + ...process.env, |
| 98 | + NODE_PATH: nodePathParts.join(path.delimiter), |
| 99 | + npm_config_cache: npmCache, |
| 100 | + }, |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + const outputDir = path.join(fixtureDir, 'docs-dist'); |
| 105 | + const indexHtmlPath = path.join(outputDir, 'index.html'); |
| 106 | + const docsJsonPath = path.join(outputDir, 'docs.json'); |
| 107 | + const configJsonPath = path.join(outputDir, 'config.json'); |
| 108 | + |
| 109 | + await fs.access(indexHtmlPath); |
| 110 | + await fs.access(docsJsonPath); |
| 111 | + await fs.access(configJsonPath); |
| 112 | + |
| 113 | + const indexHtml = await fs.readFile(indexHtmlPath, 'utf-8'); |
| 114 | + if (!indexHtml.includes('<div id="root"></div>')) { |
| 115 | + throw new Error('Generated index.html does not appear to be React UI output'); |
| 116 | + } |
| 117 | + |
| 118 | + console.log('[smoke-cli-tarball] Packed CLI generated React UI output successfully'); |
| 119 | + } finally { |
| 120 | + if (tarballPath) { |
| 121 | + await fs.rm(tarballPath, { force: true }).catch(() => undefined); |
| 122 | + } |
| 123 | + await fs.rm(tmpRoot, { recursive: true, force: true }).catch(() => undefined); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +run().catch((error) => { |
| 128 | + const message = error instanceof Error ? error.message : String(error); |
| 129 | + console.error(`[smoke-cli-tarball] ${message}`); |
| 130 | + process.exit(1); |
| 131 | +}); |
0 commit comments