|
| 1 | +import chalk from 'chalk'; |
| 2 | +import YAML from 'yaml'; |
| 3 | + |
| 4 | +import { loadConfig, loadProjectConfig } from '../config'; |
| 5 | +import type { FileAccessHandler } from '../config/references'; |
| 6 | +import { getPluginDependencyDiagnostic, normalizePluginName } from '../dependency'; |
| 7 | +import { selectPackageManager } from '../package-manager'; |
| 8 | +import { getVersionsPath } from '../paths'; |
| 9 | +import { completeAndSyncVersions, readVersions } from '../versions'; |
| 10 | +import { getWorkspacePluginEntryPoint } from '../workspace-plugins'; |
| 11 | +import type { PreparedApp } from './lifecycle'; |
| 12 | + |
| 13 | +import { writeFileSync } from 'node:fs'; |
| 14 | +import { isDeepStrictEqual } from 'node:util'; |
| 15 | + |
| 16 | +export async function syncVersions( |
| 17 | + options: { onFileAccess?: FileAccessHandler; silent?: boolean } = {}, |
| 18 | +): Promise<boolean> { |
| 19 | + if (!options.silent) console.log(chalk.cyan('Syncing lockfile versions...')); |
| 20 | + const config = await loadConfig({ onFileAccess: options.onFileAccess, throwOnValidationError: true }); |
| 21 | + const locked = readVersions(); |
| 22 | + config.versions = { ...locked, ...config.versions }; |
| 23 | + const completed = await completeAndSyncVersions(config, config.versions); |
| 24 | + const changed = !isDeepStrictEqual(locked, completed); |
| 25 | + if (changed) writeFileSync(getVersionsPath(), YAML.stringify(completed)); |
| 26 | + if (!options.silent) console.log(chalk.green('Successfully synced lockfile versions.')); |
| 27 | + return changed; |
| 28 | +} |
| 29 | + |
| 30 | +export async function prepareApp( |
| 31 | + options: { watch?: boolean; frozenLockfile?: boolean } = {}, |
| 32 | + accessedFiles = new Set<string>(), |
| 33 | +): Promise<PreparedApp> { |
| 34 | + const onFileAccess = (file: string) => { |
| 35 | + accessedFiles.add(file); |
| 36 | + }; |
| 37 | + accessedFiles.add(getVersionsPath()); |
| 38 | + if (!options.frozenLockfile) await syncVersions({ onFileAccess, silent: options.watch }); |
| 39 | + const config = await loadProjectConfig({ resolveAllReferences: true, onFileAccess }); |
| 40 | + const diagnostic = await getPluginDependencyDiagnostic(config, { onFileAccess }); |
| 41 | + if (diagnostic.status === 'missing') { |
| 42 | + throw new Error(`There are issues with the plugin dependencies:\n${diagnostic.message.join('\n')}`); |
| 43 | + } |
| 44 | + const restartFiles = new Set<string>(); |
| 45 | + if (options.watch) { |
| 46 | + for (const name of Object.keys(config.workspacePlugins ?? {})) { |
| 47 | + const entry = getWorkspacePluginEntryPoint(config, name, normalizePluginName(name), onFileAccess); |
| 48 | + accessedFiles.add(entry); |
| 49 | + restartFiles.add(entry); |
| 50 | + } |
| 51 | + } |
| 52 | + return { config, packageManager: await selectPackageManager(config.packageManager), restartFiles }; |
| 53 | +} |
0 commit comments