UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

2 lines 2.6 kB
import{startLoggerBuffering as e,stopLoggerBuffering as t}from"../../logger/index.mjs";import n from"../../i18n/prepare-translations.mjs";import{destroyKVAdapter as r,getInitializedKVAdapter as i}from"../../kv/lifecycle.mjs";import a from"../../../utils/services/create-service-context.mjs";import o from"../logger.mjs";import s from"../../config/load-config-file.mjs";import c from"../services/validate-env-vars.mjs";import{confirm as l}from"@inquirer/prompts";const u=u=>async d=>{let f,p,m=u?.env,h=u?.runtimeContext,g,_,v=async()=>{p&&(await Promise.allSettled([_?.destroy(),r(f,{config:p,env:m,runtimeContext:h})]),_=void 0,f=void 0)};try{e();let r=o.startTimer(),y=u?.mode??`process`,b=d?.force??!1;if(u?.config)p=u.config,g=u.translationStore;else{let e=await s({prepareRuntime:!0});if(p=e.config,m=e.env,h=e.runtimeContext,g=(await n({config:p,projectRoot:e.projectRoot})).translationStore,!await c({envSchema:e.envSchema,env:e.env}))if(y===`process`)await t(),process.exit(1);else return!1}if(!g)throw Error(`Lucid could not resolve the translation store.`);if(o.warn(`This will drop all database tables`),!b){let e;try{e=await l({message:`Are you sure you want to reset the database? This will drop ALL tables and cannot be undone.`,default:!1})}catch(e){if(e instanceof Error&&e.name===`ExitPromptError`)if(y===`process`)await t(),process.exit(0);else return!1;throw e}if(!e)if(o.info(`Reset cancelled`),y===`process`)await t(),process.exit(0);else return!1}o.info(`Dropping all database tables...`);try{_=await p.db.connect(m),await p.db.dropAllTables(_),o.success(`All tables dropped`,o.color.green(`successfully`))}catch(e){if(e instanceof Error?o.errorInstance(e,`Failed to drop tables`):o.error(`Failed to drop tables`,`Unknown error`),await v(),y===`process`)await t(),process.exit(1);else return!1}o.info(`Clearing KV cache...`),f=await i(p,{env:m,runtimeContext:h}),await f.clear(a({config:p,database:_,translationStore:g,env:m,runtimeContext:h,kv:f})),await v();let x=r();if(y===`process`)o.log(o.createBadge(`LUCID CMS`),`Database reset completed`,o.color.green(`successfully`),`in`,o.color.green(o.formatMilliseconds(x)),{spaceAfter:!0,spaceBefore:!0}),await t(),process.exit(0);else return o.success(`Database reset completed`,o.color.green(`successfully`),`in`,o.color.green(o.formatMilliseconds(x))),!0}catch(e){if(await v(),e instanceof Error?o.errorInstance(e,`Database reset failed`):o.error(`Database reset failed`,`Unknown error`),u?.mode===`process`||!u?.mode)await t(),process.exit(1);else return!1}};export{u as default}; //# sourceMappingURL=migrate-reset.mjs.map