@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 5.39 kB
JavaScript
import{startLoggerBuffering as e,stopLoggerBuffering as t}from"../../logger/index.mjs";import n from"../../i18n/prepare-translations.mjs";import r from"../../../services/sync/sync-collections.mjs";import{destroyKVAdapter as i,getInitializedKVAdapter as a}from"../../kv/lifecycle.mjs";import o from"../../../utils/services/create-service-context.mjs";import s from"../../collection/apply-collection-migrations.mjs";import c from"../../collection/migration/assess-migration-plan.mjs";import l from"../../collection/plan-collection-migrations.mjs";import u from"../logger.mjs";import d from"../../config/load-config-file.mjs";import{prepareExternalMigrations as f}from"../../db/load-external-migrations.mjs";import{requiresPostMigrationApproval as p,resolveMigrationApproval as m}from"../services/migration-approval.mjs";import{reportMigrationAssessment as h}from"../services/migration-report.mjs";import g from"../services/run-sync-tasks.mjs";import _ from"../services/validate-env-vars.mjs";import{confirm as v}from"@inquirer/prompts";const y=async e=>{if(e===`proceed`)return!0;if(e===`reject-destructive`)return u.error(`--yes does not authorize destructive collection migrations. Re-run with --allow-destructive if you accept the risk of permanent data loss.`),!1;let t=e===`prompt-destructive`;try{return await v({message:t?`These collection migrations may permanently delete stored data. Do you want to continue?`:`These migrations require review. Do you want to continue?`,default:!1})}catch(e){if(e instanceof Error&&e.name===`ExitPromptError`)return!1;throw e}},b=v=>async b=>{let x,S=v?.env,C=v?.runtimeContext,w,T,E,D=v?.mode??`process`,O=async()=>{x&&await Promise.allSettled([E?.destroy(),i(T,{config:x,env:S,runtimeContext:C})]),E=void 0,T=void 0},k=async e=>(await O(),D===`process`&&(await t(),process.exit(e)),!1);try{e();let i=u.startTimer(),A=b?.skipSyncSteps??!1,j=b?.yes??!1,M=b?.allowDestructive??!1,N=v?.projectRoot;if(v?.config)x=v.config,w=v.translationStore;else{let e=await d({prepareRuntime:!0});if(x=e.config,S=e.env,C=e.runtimeContext,N=e.projectRoot,w=(await n({config:x,projectRoot:e.projectRoot})).translationStore,b?.skipEnvValidation!==!0&&!await _({envSchema:e.envSchema,env:e.env}))return await k(1)}if(!x||!w)throw Error(`Lucid could not resolve its migration configuration.`);E=await x.db.connect(S),await f(x,N);let P=o({config:x,database:E,translationStore:w,env:S,runtimeContext:C});u.info(`Checking the migration status`);let F=await l(P);if(F.error)return u.error(`Could not plan collection migrations:`,P.translate.english(F.error.message)||`Unknown error`),await k(1);let I=c(F.data.collections.map(({migrationPlan:e})=>e)),L=I.reasons.length>0,R=await x.db.needsMigration(E.client);if(R&&u.warn(`Database schema migrations are pending and require approval because their migration bodies cannot be classified.`),L&&h(I),!R&&!L){if(u.success(`No migrations are required`),!A&&!await g({config:x,database:E,translationStore:w,env:S,runtimeContext:C}))return await k(1);let e=i();return u.success(`Migrations completed`,u.color.green(`successfully`),`in`,u.color.green(u.formatMilliseconds(e))),await O(),D===`process`&&(await t(),process.exit(0)),!0}let z=m({assessment:I,hasPendingDatabaseMigrations:R,yes:j,allowDestructive:M});if(!await y(z))return z===`reject-destructive`?await k(1):(u.info(`Exiting without running migrations.`),await k(0));T=await a(x,{env:S,runtimeContext:C});let B={...P,kv:T};R&&(u.info(`Running database schema migrations...`),await x.db.migrateToLatest(E),u.success(`Schema migrations completed`,u.color.green(`successfully`)));let V=R?await l(B):F;if(V.error)return u.error(`Could not re-plan collection migrations after database migrations:`,B.translate.english(V.error.message)||`Unknown error`),await k(1);let H=c(V.data.collections.map(({migrationPlan:e})=>e));if(R&&H.reasons.length>0&&(u.info(`Post-migration collection plan:`),h(H)),R&&p(I,H)){u.warn(`The collection migration risk increased after database migrations ran.`);let e=m({assessment:H,hasPendingDatabaseMigrations:!1,yes:j,allowDestructive:M});if(!await y(e))return await k(+(e===`reject-destructive`))}if(H.reasons.length>0){let e=await r(B);if(e.error)return u.error(`Sync failed during pre-migration collection sync:`,B.translate.english(e.error.message)||`Unknown error`),await k(1);u.info(`Running collection migrations...`);let t=await s(B,V.data);if(t.error)return u.error(`Collection migrations failed:`,B.translate.english(t.error.message)||`Unknown error`),await k(1);u.success(`Collection migrations completed`,u.color.green(`successfully`))}if(!A&&!await g({config:x,database:E,translationStore:w,kv:T,env:S,runtimeContext:C}))return await k(1);u.info(`Clearing KV cache...`),await T.clear(o({config:x,database:E,translationStore:w,env:S,runtimeContext:C,queue:B.queue,kv:T,media:B.media,email:B.email}));let U=i();return await O(),D===`process`&&(u.log(u.createBadge(`LUCID CMS`),`Migrations completed`,u.color.green(`successfully`),`in`,u.color.green(u.formatMilliseconds(U)),{spaceAfter:!0,spaceBefore:!0}),await t(),process.exit(0)),u.success(`Migrations completed`,u.color.green(`successfully`),`in`,u.color.green(u.formatMilliseconds(U))),!0}catch(e){return e instanceof Error?u.errorInstance(e,`Migration failed`):u.error(`Migration failed`,`Unknown error`),await k(1)}};export{b as default};
//# sourceMappingURL=migrate.mjs.map