UNPKG

@strapi/strapi

Version:

An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite

68 lines (65 loc) 2.73 kB
import os from 'node:os'; import { hashPackageJson, readCachedHash, findUndeclaredAdminPeerDeps, installAdminPeerDeps, reexecCurrentCommand, reportMissingAdminPeerDeps, MissingAdminPeerDepsError, validateDeclaredAdminPeerDeps, writeCachedHash } from './dependencies.mjs'; /** * Ensures admin peer dependencies are declared (and optionally auto-installed). * * Policy (`installIfMissing`) is decided by the caller — e.g. build vs develop CLI flags. * Checking and installation are command-agnostic. */ const ensureAdminDependencies = async ({ cwd, logger, installIfMissing })=>{ if (process.env.USE_EXPERIMENTAL_DEPENDENCIES === 'true') { logger.warn('You are using experimental dependencies that may not be compatible with Strapi.'); return { didInstall: false }; } // Hash-cache: skip the full check when package.json hasn't changed since // the last successful pass. The cache lives under node_modules so it's // already gitignored and disposable (a `yarn install` wipe re-runs it). const currentHash = await hashPackageJson(cwd); if (currentHash) { const cachedHash = await readCachedHash(cwd); if (cachedHash === currentHash) { return { didInstall: false }; } } const missing = await findUndeclaredAdminPeerDeps(cwd); if (missing.length > 0) { if (installIfMissing) { logger.info('The Strapi admin needs to install the following dependencies:', os.EOL, missing.map(({ name, wantedVersion })=>` - ${name}@${wantedVersion}`).join(os.EOL)); await installAdminPeerDeps(missing, { cwd, logger }); await reexecCurrentCommand(cwd); return { didInstall: true }; } reportMissingAdminPeerDeps(logger, missing); throw new MissingAdminPeerDepsError(missing); } await validateDeclaredAdminPeerDeps(cwd, logger); if (currentHash) { await writeCachedHash(cwd, currentHash); } return { didInstall: false }; }; /** * Runs {@link ensureAdminDependencies} and exits the process on failure. * * @returns `true` when the caller should continue, `false` when a re-exec was triggered. */ const handleAdminDependencies = async (options)=>{ try { const { didInstall } = await ensureAdminDependencies(options); return !didInstall; } catch (err) { options.logger.error(err instanceof Error ? err.message : String(err)); process.exit(1); } }; export { ensureAdminDependencies, handleAdminDependencies }; //# sourceMappingURL=ensure-admin-dependencies.mjs.map