alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
96 lines (94 loc) • 3.46 kB
JavaScript
import "../chunks/chunk-U5RRZUYZ.js";
// src/core/EntryFilenames.ts
import { JsonLoader } from "alinea/backend";
import { ALT_STATUS, EntryPhase } from "./EntryRow.js";
import { Type } from "./Type.js";
import { Workspace } from "./Workspace.js";
import { values } from "./util/Objects.js";
import { join } from "./util/Paths.js";
function workspaceMediaDir(config, workspace) {
return Workspace.data(config.workspaces[workspace])?.mediaDir ?? "";
}
function entryInfo(fileName) {
const status = ALT_STATUS.find((s) => fileName.endsWith(`.${s}`));
if (status)
return [fileName.slice(0, -status.length - 1), status];
return [fileName, EntryPhase.Published];
}
function entryChildrenDir(config, entry, parentPaths) {
const workspace = config.workspaces[entry.workspace];
if (!workspace)
throw new Error(`Workspace "${entry.workspace}" does not exist`);
const root = Workspace.roots(workspace)[entry.root];
if (!root)
throw new Error(`Root "${entry.root}" does not exist`);
const hasI18n = Boolean(root.i18n);
const { locale, path, phase } = entry;
if (hasI18n && !locale)
throw new Error(`Entry is missing locale`);
if (!values(EntryPhase).includes(phase))
throw new Error(`Entry has unknown phase: ${phase}`);
return "/" + (locale ? [locale] : []).concat(
parentPaths.concat(path).map((segment) => segment === "" ? "index" : segment)
).join("/");
}
function entryFilepath(config, entry, parentPaths) {
const { phase } = entry;
if (!values(EntryPhase).includes(phase))
throw new Error(`Entry has unknown phase: ${phase}`);
const phaseSegment = phase === EntryPhase.Published ? "" : `.${phase}`;
const location = (entryChildrenDir(config, entry, parentPaths) + phaseSegment + JsonLoader.extension).toLowerCase();
return location;
}
function entryFileName(config, entry, parentPaths) {
const workspace = config.workspaces[entry.workspace];
if (!workspace)
throw new Error(`Workspace "${entry.workspace}" does not exist`);
const { source: contentDir } = Workspace.data(workspace);
const root = Workspace.roots(workspace)[entry.root];
if (!root)
throw new Error(`Root "${entry.root}" does not exist`);
return join(contentDir, entry.root, entryFilepath(config, entry, parentPaths));
}
function entryFile(config, entry) {
const workspace = config.workspaces[entry.workspace];
if (!workspace)
throw new Error(`Workspace "${entry.workspace}" does not exist`);
const filePath = entry.filePath;
const { source: contentDir } = Workspace.data(workspace);
const root = Workspace.roots(workspace)[entry.root];
if (!root)
throw new Error(`Root "${entry.root}" does not exist`);
return join(contentDir, entry.root, filePath);
}
function entryUrl(type, meta) {
const { entryUrl: entryUrl2 } = Type.meta(type);
if (entryUrl2)
return entryUrl2(meta);
const segments = meta.locale ? [meta.locale] : [];
return "/" + segments.concat(
meta.parentPaths.concat(meta.path).filter((segment) => segment !== "index")
).join("/");
}
function pathSuffix(path, conflictingPaths) {
if (conflictingPaths.includes(path)) {
let suffix = 0;
while (true)
if (!conflictingPaths.includes(`${path}-${++suffix}`))
return suffix;
}
}
function applySuffix(path, suffix) {
return `${path}-${suffix}`;
}
export {
applySuffix,
entryChildrenDir,
entryFile,
entryFileName,
entryFilepath,
entryInfo,
entryUrl,
pathSuffix,
workspaceMediaDir
};