|
| 1 | +import { findWorkspaceDir } from '@pnpm/find-workspace-dir'; |
| 2 | +import path from 'path'; |
| 3 | +import chalk from 'chalk'; |
| 4 | +import fs from 'fs'; |
| 5 | + |
| 6 | +const workspaceRoot = (await findWorkspaceDir(process.cwd())) as string; |
| 7 | + |
| 8 | +if (!workspaceRoot) { |
| 9 | + throw new Error('Could not find workspace root'); |
| 10 | +} |
| 11 | + |
| 12 | +const docsViewerRoot = path.join(workspaceRoot, 'docs-viewer'); |
| 13 | +const projectRoot = path.join(docsViewerRoot, './projects'); |
| 14 | + |
| 15 | +export { workspaceRoot, docsViewerRoot, projectRoot }; |
| 16 | + |
| 17 | +export function log(message: string) { |
| 18 | + console.log(chalk.grey(`[docs-viewer]\t${message}`)); |
| 19 | +} |
| 20 | + |
| 21 | +export async function getCurrentVersion(tool: string) { |
| 22 | + const proc = Bun.spawn([tool, '--version'], { |
| 23 | + env: process.env, |
| 24 | + stdio: ['inherit', 'pipe', 'inherit'], |
| 25 | + }); |
| 26 | + await proc.exited; |
| 27 | + const version = await new Response(proc.stdout).text(); |
| 28 | + return version.trim().replace('v', ''); |
| 29 | +} |
| 30 | + |
| 31 | +export function determinePackageManager(dir: string) { |
| 32 | + if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) { |
| 33 | + return 'pnpm'; |
| 34 | + } |
| 35 | + if (fs.existsSync(path.join(dir, 'package-lock.json'))) { |
| 36 | + return 'npm'; |
| 37 | + } |
| 38 | + if (fs.existsSync(path.join(dir, 'yarn.lock'))) { |
| 39 | + return 'yarn'; |
| 40 | + } |
| 41 | + |
| 42 | + return 'npm'; |
| 43 | +} |
| 44 | + |
| 45 | +export async function generateDocs() { |
| 46 | + const currentVersion = require(path.join(workspaceRoot, 'package.json')).version; |
| 47 | + const absoluteVersion = currentVersion.split('-')[0]; |
| 48 | + const command = ['bun', 'gen', '--project', 'ember-data', '--version', absoluteVersion]; |
| 49 | + const proc = Bun.spawn(command, { |
| 50 | + cwd: path.join(projectRoot, 'ember-jsonapi-docs'), |
| 51 | + env: Object.assign({}, process.env, { COREPACK_INTEGRITY_KEYS: 0 }), |
| 52 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 53 | + }); |
| 54 | + await proc.exited; |
| 55 | + |
| 56 | + const command2 = ['bun', 'fix:files']; |
| 57 | + const proc2 = Bun.spawn(command2, { |
| 58 | + cwd: path.join(projectRoot, 'ember-api-docs-data'), |
| 59 | + env: process.env, |
| 60 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 61 | + }); |
| 62 | + await proc2.exited; |
| 63 | +} |
| 64 | + |
| 65 | +export function repoDetails(gitUrl: string) { |
| 66 | + const repoPath = gitUrl.replace('.git', '').replace('git@github.com:', ''); |
| 67 | + const [org, name] = repoPath.split('/'); |
| 68 | + const installPathFromRoot = path.join('./projects', name); |
| 69 | + const location = path.join(docsViewerRoot, installPathFromRoot); |
| 70 | + |
| 71 | + return { |
| 72 | + org, |
| 73 | + name, |
| 74 | + repoPath, |
| 75 | + gitUrl, |
| 76 | + installPathFromRoot, |
| 77 | + location, |
| 78 | + relativePath: path.relative(__dirname, path.join(docsViewerRoot, installPathFromRoot)), |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +export async function installDeps(packageManager: 'pnpm' | 'npm' | 'yarn', details: ReturnType<typeof repoDetails>) { |
| 83 | + const proc = Bun.spawn( |
| 84 | + [packageManager, 'install', packageManager === 'pnpm' ? '--ignore-workspace' : ''].filter(Boolean), |
| 85 | + { |
| 86 | + cwd: details.location, |
| 87 | + env: process.env, |
| 88 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 89 | + } |
| 90 | + ); |
| 91 | + await proc.exited; |
| 92 | +} |
| 93 | + |
| 94 | +export async function maybeMakePNPMInstallable(details: ReturnType<typeof repoDetails>) { |
| 95 | + // get the version to use from package.json |
| 96 | + const packageJson = require(path.join(details.location, 'package.json')); |
| 97 | + |
| 98 | + const nodeVersion = await getCurrentVersion('node'); |
| 99 | + const pnpmVersion = await getCurrentVersion('pnpm'); |
| 100 | + |
| 101 | + if ( |
| 102 | + !packageJson.volta || |
| 103 | + packageJson.volta.node !== nodeVersion || |
| 104 | + packageJson.volta.pnpm !== pnpmVersion || |
| 105 | + packageJson.packageManager || |
| 106 | + packageJson.engines?.node !== nodeVersion |
| 107 | + ) { |
| 108 | + delete packageJson.packageManager; |
| 109 | + packageJson.volta = { |
| 110 | + node: nodeVersion, |
| 111 | + pnpm: pnpmVersion, |
| 112 | + }; |
| 113 | + packageJson.engines = packageJson.engines || {}; |
| 114 | + packageJson.engines.node = nodeVersion; |
| 115 | + |
| 116 | + // if this is ember-api-docs we need to also force it to use dart-sass |
| 117 | + if (packageJson.name === 'ember-api-docs') { |
| 118 | + packageJson.pnpm = packageJson.pnpm || {}; |
| 119 | + packageJson.pnpm.overrides = packageJson.pnpm.overrides || {}; |
| 120 | + packageJson.pnpm.overrides['node-sass'] = 'npm:sass@^1.86.0'; |
| 121 | + } |
| 122 | + |
| 123 | + fs.writeFileSync(path.join(details.location, 'package.json'), JSON.stringify(packageJson, null, 2)); |
| 124 | + |
| 125 | + // run install to pickup the lockfile change |
| 126 | + await installDeps('pnpm', details); |
| 127 | + |
| 128 | + const proc = Bun.spawn(['git', 'commit', '-am', '"ensure volta works as expected"'], { |
| 129 | + cwd: details.location, |
| 130 | + env: process.env, |
| 131 | + stdio: ['inherit', 'inherit', 'inherit'], |
| 132 | + }); |
| 133 | + await proc.exited; |
| 134 | + } |
| 135 | +} |
0 commit comments