|
| 1 | +#! /usr/bin/env bun |
| 2 | +/** |
| 3 | + * Sets up the docs viewer app |
| 4 | + */ |
| 5 | +import chalk from 'chalk'; |
| 6 | +import path from 'path'; |
| 7 | +import fs from 'fs'; |
| 8 | +import { findWorkspaceDir } from '@pnpm/find-workspace-dir'; |
| 9 | + |
| 10 | +const EMBER_API_DOCS_REPO = `git@github.com:ember-learn/ember-api-docs.git`; |
| 11 | +const EMBER_JSONAPI_DOCS_REPO = `git@github.com:ember-learn/ember-jsonapi-docs.git`; |
| 12 | +const EMBER_API_DOCS_DATA_REPO = `git@github.com:ember-learn/ember-api-docs-data.git`; |
| 13 | + |
| 14 | +const workspaceRoot = (await findWorkspaceDir(process.cwd())) as string; |
| 15 | + |
| 16 | +if (!workspaceRoot) { |
| 17 | + throw new Error('Could not find workspace root'); |
| 18 | +} |
| 19 | + |
| 20 | +const docsViewerRoot = path.join(workspaceRoot, 'docs-viewer'); |
| 21 | + |
| 22 | +function log(message: string) { |
| 23 | + console.log(chalk.grey(`[docs-viewer]\t${message}`)); |
| 24 | +} |
| 25 | + |
| 26 | +function repoDetails(gitUrl: string) { |
| 27 | + const repoPath = gitUrl.replace('.git', '').replace('git@github.com:', ''); |
| 28 | + const [org, name] = repoPath.split('/'); |
| 29 | + const installPathFromRoot = path.join('./projects', name); |
| 30 | + const location = path.join(docsViewerRoot, installPathFromRoot); |
| 31 | + |
| 32 | + return { |
| 33 | + org, |
| 34 | + name, |
| 35 | + repoPath, |
| 36 | + gitUrl, |
| 37 | + installPathFromRoot, |
| 38 | + location, |
| 39 | + relativePath: path.relative(__dirname, path.join(docsViewerRoot, installPathFromRoot)), |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +async function getCurrentVersion(tool: string) { |
| 44 | + const proc = Bun.spawn([tool, '--version'], { |
| 45 | + env: process.env, |
| 46 | + stdio: ['inherit', 'pipe', 'inherit'], |
| 47 | + }); |
| 48 | + await proc.exited; |
| 49 | + const version = await new Response(proc.stdout).text(); |
| 50 | + return version.trim().replace('v', ''); |
| 51 | +} |
| 52 | + |
| 53 | +function determinePackageManager(dir: string) { |
| 54 | + if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) { |
| 55 | + return 'pnpm'; |
| 56 | + } |
| 57 | + if (fs.existsSync(path.join(dir, 'package-lock.json'))) { |
| 58 | + return 'npm'; |
| 59 | + } |
| 60 | + if (fs.existsSync(path.join(dir, 'yarn.lock'))) { |
| 61 | + return 'yarn'; |
| 62 | + } |
| 63 | + |
| 64 | + return 'npm'; |
| 65 | +} |
| 66 | + |
| 67 | +async function getOrUpdateRepo(gitUrl: string) { |
| 68 | + const details = repoDetails(gitUrl); |
| 69 | + |
| 70 | + if (fs.existsSync(details.location)) { |
| 71 | + await getLatest(details); |
| 72 | + } else { |
| 73 | + await cloneRepo(details); |
| 74 | + } |
| 75 | + |
| 76 | + // install dependencies |
| 77 | + const packageManager = determinePackageManager(details.location); |
| 78 | + |
| 79 | + if (packageManager === 'pnpm') { |
| 80 | + // get the version to use from package.json |
| 81 | + const packageJson = require(path.join(details.location, 'package.json')); |
| 82 | + |
| 83 | + let nodeVersion = await getCurrentVersion('node'); |
| 84 | + const pnpmVersion = await getCurrentVersion('pnpm'); |
| 85 | + |
| 86 | + // ember-api-docs requires an older node due to node-sass |
| 87 | + if (packageJson.name === 'ember-api-docs') { |
| 88 | + nodeVersion = '20.19.0'; |
| 89 | + } |
| 90 | + |
| 91 | + if ( |
| 92 | + !packageJson.volta || |
| 93 | + packageJson.volta.node !== nodeVersion || |
| 94 | + packageJson.volta.pnpm !== pnpmVersion || |
| 95 | + packageJson.packageManager || |
| 96 | + packageJson.engines?.node !== nodeVersion |
| 97 | + ) { |
| 98 | + delete packageJson.packageManager; |
| 99 | + packageJson.volta = { |
| 100 | + node: nodeVersion, |
| 101 | + pnpm: pnpmVersion, |
| 102 | + }; |
| 103 | + packageJson.engines = packageJson.engines || {}; |
| 104 | + packageJson.engines.node = nodeVersion; |
| 105 | + |
| 106 | + fs.writeFileSync(path.join(details.location, 'package.json'), JSON.stringify(packageJson, null, 2)); |
| 107 | + const proc = Bun.spawn(['git', 'commit', '-am', '"ensure volta works as expected"'], { |
| 108 | + cwd: details.location, |
| 109 | + env: process.env, |
| 110 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 111 | + }); |
| 112 | + await proc.exited; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + log(`Installing dependencies in ${chalk.green(details.installPathFromRoot)} using ${chalk.yellow(packageManager)}`); |
| 117 | + const proc = Bun.spawn( |
| 118 | + [packageManager, 'install', packageManager === 'pnpm' ? '--ignore-workspace' : ''].filter(Boolean), |
| 119 | + { |
| 120 | + cwd: details.location, |
| 121 | + env: process.env, |
| 122 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 123 | + } |
| 124 | + ); |
| 125 | + await proc.exited; |
| 126 | +} |
| 127 | + |
| 128 | +async function getLatest(details: ReturnType<typeof repoDetails>) { |
| 129 | + log(`Updating ${chalk.green(details.repoPath)} in ${chalk.green(details.installPathFromRoot)} to latest`); |
| 130 | + const mainBranch = details.name === 'ember-jsonapi-docs' ? 'master' : 'main'; |
| 131 | + const proc = Bun.spawn(['git', 'fetch', 'origin', mainBranch, '--depth=1'], { |
| 132 | + cwd: details.location, |
| 133 | + }); |
| 134 | + await proc.exited; |
| 135 | + const proc2 = Bun.spawn(['git', 'reset', '--hard', `origin/${mainBranch}`], { |
| 136 | + cwd: details.location, |
| 137 | + }); |
| 138 | + await proc2.exited; |
| 139 | +} |
| 140 | + |
| 141 | +async function cloneRepo(details: ReturnType<typeof repoDetails>) { |
| 142 | + const relativePath = path.join('./projects', details.name); |
| 143 | + log(`Cloning ${chalk.green(details.repoPath)} to ${chalk.green(relativePath)}`); |
| 144 | + const proc = Bun.spawn(['git', 'clone', details.gitUrl, relativePath, '--depth=1'], { |
| 145 | + cwd: docsViewerRoot, |
| 146 | + }); |
| 147 | + await proc.exited; |
| 148 | +} |
| 149 | + |
| 150 | +async function main() { |
| 151 | + log('Setting up the docs viewer'); |
| 152 | + log(`\tCurrent working directory: ${chalk.green(process.cwd())}`); |
| 153 | + |
| 154 | + // clone repos |
| 155 | + await getOrUpdateRepo(EMBER_API_DOCS_DATA_REPO); |
| 156 | + await getOrUpdateRepo(EMBER_JSONAPI_DOCS_REPO); |
| 157 | + await getOrUpdateRepo(EMBER_API_DOCS_REPO); |
| 158 | + |
| 159 | + // symlink our own project root into projects as 'ember-data' |
| 160 | + const emberDataLocation = path.join(docsViewerRoot, './projects/ember-data'); |
| 161 | + if (!fs.existsSync(emberDataLocation)) { |
| 162 | + log(`Symlinking ${chalk.green('ember-data')} to ${chalk.green('./projects/ember-data')}`); |
| 163 | + fs.symlinkSync(workspaceRoot, emberDataLocation); |
| 164 | + } |
| 165 | + |
| 166 | + // symlink `ember-api-docs-data` to `ember-api-docs` |
| 167 | + const projectRoot = path.join(docsViewerRoot, './projects'); |
| 168 | + const emberApiDocsData = path.join(projectRoot, 'ember-api-docs-data'); |
| 169 | + const symLinkLocation = path.join(projectRoot, 'ember-api-docs/ember-api-docs-data'); |
| 170 | + |
| 171 | + if (!fs.existsSync(symLinkLocation)) { |
| 172 | + log(`Symlinking ${chalk.green('ember-api-docs-data')} to ${chalk.green('ember-api-docs')}`); |
| 173 | + fs.symlinkSync(emberApiDocsData, symLinkLocation); |
| 174 | + } |
| 175 | + |
| 176 | + log('Docs viewer setup complete'); |
| 177 | + |
| 178 | + log('Generating the current docs in ember-jsonapi-docs (these will be inserted into ember-api-docs-data)'); |
| 179 | + const currentVersion = require(path.join(workspaceRoot, 'package.json')).version; |
| 180 | + const absoluteVersion = currentVersion.split('-')[0]; |
| 181 | + const command = ['bun', 'gen', '--project', 'ember-data', '--version', absoluteVersion]; |
| 182 | + |
| 183 | + const proc = Bun.spawn(command, { |
| 184 | + cwd: path.join(projectRoot, 'ember-jsonapi-docs'), |
| 185 | + env: process.env, |
| 186 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 187 | + }); |
| 188 | + await proc.exited; |
| 189 | + |
| 190 | + log('Docs generated. Run `bun regenerate-docs` to update the docs with any changes'); |
| 191 | + log('Starting the docs viewer'); |
| 192 | + |
| 193 | + const proc2 = Bun.spawn(['pnpm', 'start'], { |
| 194 | + cwd: path.join(projectRoot, 'ember-api-docs'), |
| 195 | + env: process.env, |
| 196 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 197 | + }); |
| 198 | + await proc2.exited; |
| 199 | +} |
| 200 | + |
| 201 | +main(); |
0 commit comments