|
| 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 { |
| 9 | + determinePackageManager, |
| 10 | + docsViewerRoot, |
| 11 | + generateDocs, |
| 12 | + installDeps, |
| 13 | + log, |
| 14 | + maybeMakePNPMInstallable, |
| 15 | + projectRoot, |
| 16 | + repoDetails, |
| 17 | + workspaceRoot, |
| 18 | +} from './-utils.ts'; |
| 19 | + |
| 20 | +const EMBER_API_DOCS_REPO = `git@github.com:ember-learn/ember-api-docs.git`; |
| 21 | +const EMBER_JSONAPI_DOCS_REPO = `git@github.com:ember-learn/ember-jsonapi-docs.git`; |
| 22 | +const EMBER_API_DOCS_DATA_REPO = `git@github.com:ember-learn/ember-api-docs-data.git`; |
| 23 | + |
| 24 | +async function getOrUpdateRepo(gitUrl: string) { |
| 25 | + const details = repoDetails(gitUrl); |
| 26 | + |
| 27 | + let updated = true; |
| 28 | + if (fs.existsSync(details.location)) { |
| 29 | + updated = await getLatest(details); |
| 30 | + } else { |
| 31 | + await cloneRepo(details); |
| 32 | + } |
| 33 | + |
| 34 | + if (!updated) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // install dependencies |
| 39 | + const packageManager = determinePackageManager(details.location); |
| 40 | + if (packageManager === 'pnpm') { |
| 41 | + // some of the repositories use pnpm but do not have volta configured |
| 42 | + // and have not had their engines/packageManager field updated in a while. |
| 43 | + // this lets us still install them with pnpm if that is the case |
| 44 | + await maybeMakePNPMInstallable(details); |
| 45 | + } |
| 46 | + |
| 47 | + log(`Installing dependencies in ${chalk.green(details.installPathFromRoot)} using ${chalk.yellow(packageManager)}`); |
| 48 | + await installDeps(packageManager, details); |
| 49 | +} |
| 50 | + |
| 51 | +async function getSHA(details: ReturnType<typeof repoDetails>, reference: string) { |
| 52 | + log(`Getting commit sha for ${reference} for ${chalk.green(details.repoPath)}`); |
| 53 | + const shaProc1 = Bun.spawn(['git', 'rev-parse', reference], { |
| 54 | + cwd: details.location, |
| 55 | + }); |
| 56 | + await shaProc1.exited; |
| 57 | + return (await new Response(shaProc1.stdout).text()).trim(); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Updates the repo by fetching only the latest commit on the main branch |
| 62 | + * and resetting the local repo to that commit |
| 63 | + * |
| 64 | + * Returns true if the repo was updated, false if it was already up to date |
| 65 | + */ |
| 66 | +async function getLatest(details: ReturnType<typeof repoDetails>): Promise<boolean> { |
| 67 | + const currentSha = await getSHA(details, 'HEAD^1'); |
| 68 | + |
| 69 | + log(`Updating ${chalk.green(details.repoPath)} in ${chalk.green(details.installPathFromRoot)} to latest`); |
| 70 | + const mainBranch = details.name === 'ember-jsonapi-docs' ? 'master' : 'main'; |
| 71 | + const proc = Bun.spawn(['git', 'fetch', 'origin', mainBranch, '--depth=1'], { |
| 72 | + cwd: details.location, |
| 73 | + }); |
| 74 | + await proc.exited; |
| 75 | + |
| 76 | + // if the commit sha has not changed, we do not need to reset |
| 77 | + const newSha = await getSHA(details, `origin/${mainBranch}`); |
| 78 | + |
| 79 | + if (currentSha === newSha) { |
| 80 | + log(`${chalk.green(details.repoPath)} is already up to date`); |
| 81 | + return false; |
| 82 | + } else { |
| 83 | + log(`Resetting ${chalk.green(details.repoPath)} from ${chalk.red(currentSha)} to ${chalk.green(newSha)}`); |
| 84 | + } |
| 85 | + |
| 86 | + const proc2 = Bun.spawn(['git', 'reset', '--hard', `origin/${mainBranch}`], { |
| 87 | + cwd: details.location, |
| 88 | + }); |
| 89 | + await proc2.exited; |
| 90 | + |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Clones the repo, fetching only the latest commit |
| 96 | + */ |
| 97 | +async function cloneRepo(details: ReturnType<typeof repoDetails>) { |
| 98 | + const relativePath = path.join('./projects', details.name); |
| 99 | + log(`Cloning ${chalk.green(details.repoPath)} to ${chalk.green(relativePath)}`); |
| 100 | + const proc = Bun.spawn(['git', 'clone', details.gitUrl, relativePath, '--depth=1'], { |
| 101 | + cwd: docsViewerRoot, |
| 102 | + }); |
| 103 | + await proc.exited; |
| 104 | +} |
| 105 | + |
| 106 | +async function main() { |
| 107 | + log('Setting up the docs viewer'); |
| 108 | + log(`\tCurrent working directory: ${chalk.green(process.cwd())}`); |
| 109 | + |
| 110 | + // clone repos |
| 111 | + ////////////// |
| 112 | + // |
| 113 | + await getOrUpdateRepo(EMBER_API_DOCS_DATA_REPO); |
| 114 | + await getOrUpdateRepo(EMBER_JSONAPI_DOCS_REPO); |
| 115 | + await getOrUpdateRepo(EMBER_API_DOCS_REPO); |
| 116 | + |
| 117 | + // symlink our own project root into projects as 'data' |
| 118 | + ///////////////////////////////////////////////////////////// |
| 119 | + // |
| 120 | + const emberDataLocation = path.join(projectRoot, 'data'); |
| 121 | + if (!fs.existsSync(emberDataLocation)) { |
| 122 | + log(`Symlinking ${chalk.green('ember-data')} to ${chalk.green('./projects/data')}`); |
| 123 | + fs.symlinkSync(workspaceRoot, emberDataLocation); |
| 124 | + } |
| 125 | + |
| 126 | + // symlink `ember-api-docs-data` into `ember-api-docs` |
| 127 | + //////////////////////////////////////////////////// |
| 128 | + // |
| 129 | + const emberApiDocsData = path.join(projectRoot, 'ember-api-docs-data'); |
| 130 | + const symLinkLocation = path.join(projectRoot, 'ember-api-docs/ember-api-docs-data'); |
| 131 | + if (!fs.existsSync(symLinkLocation)) { |
| 132 | + log(`Symlinking ${chalk.green('ember-api-docs-data')} to ${chalk.green('ember-api-docs')}`); |
| 133 | + fs.symlinkSync(emberApiDocsData, symLinkLocation); |
| 134 | + } |
| 135 | + |
| 136 | + log('Docs viewer setup complete'); |
| 137 | + log('Generating the current docs in ember-jsonapi-docs (these will be inserted into ember-api-docs-data)'); |
| 138 | + |
| 139 | + // generate the docs |
| 140 | + //////////////////// |
| 141 | + // |
| 142 | + await generateDocs(); |
| 143 | + |
| 144 | + log('Docs generated. Run `bun regenerate-docs` to update the docs with any changes'); |
| 145 | + log('Starting the docs viewer'); |
| 146 | + |
| 147 | + // start the docs viewer |
| 148 | + //////////////////////// |
| 149 | + // |
| 150 | + const proc2 = Bun.spawn(['pnpm', 'start'], { |
| 151 | + cwd: path.join(projectRoot, 'ember-api-docs'), |
| 152 | + env: process.env, |
| 153 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 154 | + }); |
| 155 | + await proc2.exited; |
| 156 | +} |
| 157 | + |
| 158 | +main(); |
0 commit comments