alinea
Version:
Headless git-based CMS
143 lines (141 loc) • 4.96 kB
JavaScript
import "../chunks/chunk-NZLE2WMY.js";
// src/core/Config.ts
import { CloudAuthView } from "alinea/cloud/view/CloudAuth";
import { MediaFile, MediaLibrary } from "alinea/core/media/MediaTypes";
import { getWorkspace } from "./Internal.js";
import { Root } from "./Root.js";
import { Schema } from "./Schema.js";
import { getScope } from "./Scope.js";
import { Type } from "./Type.js";
import { Workspace } from "./Workspace.js";
import { isValidIdentifier } from "./util/Identifiers.js";
import { entries, values } from "./util/Objects.js";
import * as paths from "./util/Paths.js";
var Config;
((Config2) => {
function baseUrl(config, env = process.env.NODE_ENV ?? "production") {
const result = typeof config.baseUrl === "object" ? config.baseUrl[env] : config.baseUrl;
if (!result) return result;
if (result.includes("://")) return result;
return `https://${result}`;
}
Config2.baseUrl = baseUrl;
function mainWorkspace(config) {
const key = Object.keys(config.workspaces)[0];
return getWorkspace(config.workspaces[key]);
}
Config2.mainWorkspace = mainWorkspace;
function type(config, name) {
return config.schema[name];
}
Config2.type = type;
function rootContains(config, root, childType) {
const allowed = Root.contains(root);
if (allowed.length === 0) return false;
const scope = getScope(config);
const typeName = scope.nameOf(childType);
for (const type2 of allowed) {
if (typeof type2 === "string") {
if (type2 === typeName) return true;
} else {
const name = scope.nameOf(type2);
if (name === typeName) return true;
}
}
return false;
}
Config2.rootContains = rootContains;
function typeContains(config, parentType, childType) {
const allowed = Type.contains(parentType);
if (allowed.length === 0) return false;
const scope = getScope(config);
const typeName = scope.nameOf(childType);
for (const type2 of allowed) {
if (typeof type2 === "string") {
if (type2 === typeName) return true;
} else {
const name = scope.nameOf(type2);
if (name === typeName) return true;
}
}
return false;
}
Config2.typeContains = typeContains;
function hasAuth(config) {
return Boolean(config.auth);
}
Config2.hasAuth = hasAuth;
function multipleWorkspaces(config) {
return Object.keys(config.workspaces).length > 1;
}
Config2.multipleWorkspaces = multipleWorkspaces;
function contentDir(config) {
const workspace = mainWorkspace(config);
if (multipleWorkspaces(config)) return paths.dirname(workspace.source);
return workspace.source;
}
Config2.contentDir = contentDir;
function filePath(config, workspace, root, locale, ...rest) {
const hasMultipleWorkspaces = multipleWorkspaces(config);
let result = "";
if (hasMultipleWorkspaces) result = paths.join(result, workspace);
result = paths.join(result, root);
if (locale) result = paths.join(result, locale.toLowerCase());
return paths.join(result, ...rest);
}
Config2.filePath = filePath;
function validate(config) {
Schema.validate(config.schema);
const all = entries(config.workspaces);
let sourceDir;
for (const [key, workspace] of all) {
if (!isValidIdentifier(key))
throw new Error(
`Invalid Workspace name "${key}", use only a-z, A-Z, 0-9, and _`
);
Workspace.validate(workspace, config.schema);
if (all.length > 1) {
const source = getWorkspace(workspace).source;
const dir = paths.dirname(source);
const name = paths.basename(source);
if (name !== key)
throw new Error(
`Workspace "${key}" source directory "${name}" must be named "${key}"`
);
sourceDir ??= dir;
if (sourceDir !== dir)
throw new Error(
`Workspaces "${key}" must be a directory of "${sourceDir}"`
);
}
}
}
Config2.validate = validate;
function referencedViews(config) {
return Schema.referencedViews(config.schema).concat(
values(config.workspaces).flatMap(Workspace.referencedViews)
);
}
Config2.referencedViews = referencedViews;
})(Config || (Config = {}));
var normalized = /* @__PURE__ */ new WeakSet();
function createConfig(definition) {
if (normalized.has(definition)) return definition;
if (definition.schema.MediaFile && definition.schema.MediaFile !== MediaFile)
throw new Error(`"MediaFile" is a reserved Type name`);
if (definition.schema.MediaLibrary && definition.schema.MediaLibrary !== MediaLibrary)
throw new Error(`"MediaLibrary" is a reserved Type name`);
const res = {
auth: CloudAuthView,
...definition,
publicDir: definition.publicDir ?? "/public",
schema: { ...definition.schema, MediaLibrary, MediaFile }
};
Config.validate(res);
normalized.add(res);
return res;
}
export {
Config,
createConfig
};