alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
114 lines (112 loc) • 3.65 kB
JavaScript
import {
package_default
} from "../chunks/chunk-KWP47LDR.js";
import "../chunks/chunk-U5RRZUYZ.js";
// src/cli/Serve.ts
import { JWTPreviews } from "alinea/backend";
import { Handler } from "alinea/backend/Handler";
import { createCloudDebugHandler } from "alinea/cloud/server/CloudDebugHandler";
import { createCloudHandler } from "alinea/cloud/server/CloudHandler";
import path from "node:path";
import { generate } from "./Generate.js";
import { buildOptions } from "./build/BuildOptions.js";
import { createLocalServer } from "./serve/CreateLocalServer.js";
import { GitHistory } from "./serve/GitHistory.js";
import { LiveReload } from "./serve/LiveReload.js";
import { MemoryDrafts } from "./serve/MemoryDrafts.js";
import { startNodeServer } from "./serve/StartNodeServer.js";
import { dirname } from "./util/Dirname.js";
import { findConfigFile } from "./util/FindConfigFile.js";
var __dirname = dirname(import.meta.url);
async function serve(options) {
const {
cwd = process.cwd(),
configFile,
staticDir = path.join(__dirname, "static"),
alineaDev = false,
production = false
} = options;
const configLocation = configFile ? path.join(path.resolve(cwd), configFile) : findConfigFile(cwd);
if (!configLocation)
throw new Error(`No config file specified`);
const preferredPort = options.port ? Number(options.port) : 4500;
const server = startNodeServer(preferredPort);
const dashboardUrl = server.then((server2) => `http://localhost:${server2.port}`);
const rootDir = path.resolve(cwd);
const context = {
rootDir,
staticDir,
alineaDev,
buildOptions: {
...buildOptions,
...options.buildOptions,
plugins: (buildOptions.plugins || []).concat(
options.buildOptions?.plugins || []
)
},
production,
liveReload: new LiveReload()
};
server.then(async () => {
console.log(` \x1B[36mAlinea ${package_default.version}\x1B[39m`);
console.log(` - Local CMS: ${await dashboardUrl}
`);
});
const gen = generate({
...options,
dashboardUrl,
watch: true,
async onAfterGenerate() {
options.onAfterGenerate?.({
ALINEA_DEV_SERVER: await dashboardUrl
});
}
})[Symbol.asyncIterator]();
const drafts = new MemoryDrafts();
let nextGen = gen.next();
let cms;
let handle;
while (true) {
const current = await nextGen;
if (!current?.value)
return;
const { cms: currentCMS, localData: fileData, db } = current.value;
if (currentCMS === cms) {
context.liveReload.reload("refetch");
} else {
let createBackend2 = function() {
if (process.env.ALINEA_CLOUD_DEBUG)
return createCloudDebugHandler(currentCMS.config, db, rootDir);
if (process.env.ALINEA_CLOUD_URL)
return createCloudHandler(
currentCMS.config,
db,
process.env.ALINEA_API_KEY
);
return new Handler({
config: currentCMS.config,
db,
target: fileData,
media: fileData,
drafts,
history: new GitHistory(currentCMS.config, rootDir),
previews: new JWTPreviews("dev"),
previewAuthToken: "dev"
});
};
var createBackend = createBackend2;
const backend = createBackend2();
handle = createLocalServer(context, backend);
cms = currentCMS;
context.liveReload.reload("refresh");
}
nextGen = gen.next();
const { serve: serve2 } = await server;
for await (const { request, respondWith } of serve2(nextGen)) {
handle(request).then(respondWith);
}
}
}
export {
serve
};