UNPKG

alinea

Version:

[![npm](https://img.shields.io/npm/v/alinea.svg)](https://npmjs.org/package/alinea) [![install size](https://packagephobia.com/badge?p=alinea)](https://packagephobia.com/result?p=alinea)

193 lines (191 loc) 5.93 kB
import "../../chunks/chunk-U5RRZUYZ.js"; // src/backend/data/ChangeSet.ts import { EntryPhase, Type, Workspace } from "alinea/core"; import { META_KEY, createRecord } from "alinea/core/EntryRecord"; import { MutationType } from "alinea/core/Mutation"; import { join } from "alinea/core/util/Paths"; import { JsonLoader } from "../loader/JsonLoader.js"; var ChangeType = /* @__PURE__ */ ((ChangeType2) => { ChangeType2["Write"] = "write"; ChangeType2["Rename"] = "rename"; ChangeType2["Patch"] = "patch"; ChangeType2["Delete"] = "delete"; ChangeType2["Upload"] = "upload"; return ChangeType2; })(ChangeType || {}); var decoder = new TextDecoder(); var loader = JsonLoader; var ChangeSetCreator = class { constructor(config) { this.config = config; } entryLocation({ locale, parentPaths, path, phase }, extension) { const segments = (locale ? [locale] : []).concat( parentPaths.concat(path).map((segment) => segment === "" ? "index" : segment) ).join("/"); const phaseSegment = phase === EntryPhase.Published ? "" : `.${phase}`; return (segments + phaseSegment + extension).toLowerCase(); } editChanges({ previousFile, file, entry }) { const type = this.config.schema[entry.type]; if (!type) throw new Error(`Cannot publish entry of unknown type: ${entry.type}`); const record = createRecord(entry); const res = []; if (previousFile && previousFile !== file) { res.push({ type: "rename" /* Rename */, from: previousFile, to: file }); res.push({ type: "rename" /* Rename */, from: previousFile.slice(0, -".json".length), to: file.slice(0, -".json".length) }); } return res.concat({ type: "write" /* Write */, file, contents: decoder.decode(loader.format(this.config.schema, record)) }); } createChanges({ file, entry }) { const record = createRecord(entry); return [ { type: "write" /* Write */, file, contents: decoder.decode(loader.format(this.config.schema, record)) } ]; } publishChanges({ file }) { const draftFile = `.${EntryPhase.Draft}.json`; const archivedFiled = `.${EntryPhase.Archived}.json`; if (file.endsWith(draftFile)) return [ { type: "rename" /* Rename */, from: file, to: file.slice(0, -draftFile.length) + ".json" } ]; if (file.endsWith(archivedFiled)) return [ { type: "rename" /* Rename */, from: file, to: file.slice(0, -archivedFiled.length) + ".json" } ]; throw new Error(`Cannot publish file: ${file}`); } archiveChanges({ file }) { const fileEnd = ".json"; if (!file.endsWith(fileEnd)) throw new Error(`File extension does not match json: ${file}`); return [ { type: "rename" /* Rename */, from: file, to: file.slice(0, -fileEnd.length) + `.${EntryPhase.Archived}.json` } ]; } removeChanges({ file }) { if (!file.endsWith(`.${EntryPhase.Archived}.json`)) return []; return [ { type: "delete" /* Delete */, file }, { type: "delete" /* Delete */, file: file.slice(0, -`.${EntryPhase.Archived}.json`.length) } ]; } discardChanges({ file }) { const fileEnd = `.${EntryPhase.Draft}.json`; if (!file.endsWith(fileEnd)) throw new Error(`Cannot discard non-draft file: ${file}`); return [{ type: "delete" /* Delete */, file }]; } orderChanges({ file, index }) { return [{ type: "patch" /* Patch */, file, patch: { [META_KEY]: { index } } }]; } moveChanges({ entryId, entryType, fromFile, toFile, index }) { const result = []; const isContainer = Type.isContainer(this.config.schema[entryType]); result.push({ type: "rename" /* Rename */, from: fromFile, to: toFile }); if (!isContainer) return result; const fromFolder = fromFile.slice(0, -".json".length); const toFolder = toFile.slice(0, -".json".length); result.push({ type: "rename" /* Rename */, from: fromFolder, to: toFolder }); result.push( ...this.orderChanges({ type: MutationType.Order, entryId, file: toFile, index }) ); return result; } fileUploadChanges(mutation) { return [{ type: "upload" /* Upload */, file: mutation.file, url: mutation.url }]; } fileRemoveChanges(mutation) { const mediaDir = Workspace.data(this.config.workspaces[mutation.workspace])?.mediaDir ?? ""; const binaryLocation = join(mediaDir, mutation.location); const removeBinary = { type: "delete" /* Delete */, file: binaryLocation }; if (mutation.replace) return [removeBinary]; return [{ type: "delete" /* Delete */, file: mutation.file }, removeBinary]; } mutationChanges(mutation) { switch (mutation.type) { case MutationType.Edit: return this.editChanges(mutation); case MutationType.Create: return this.createChanges(mutation); case MutationType.Publish: return this.publishChanges(mutation); case MutationType.Archive: return this.archiveChanges(mutation); case MutationType.Remove: return this.removeChanges(mutation); case MutationType.Discard: return this.discardChanges(mutation); case MutationType.Order: return this.orderChanges(mutation); case MutationType.Move: return this.moveChanges(mutation); case MutationType.Upload: return this.fileUploadChanges(mutation); case MutationType.FileRemove: return this.fileRemoveChanges(mutation); } } create(mutations) { return mutations.map((meta) => { return { changes: this.mutationChanges(meta), meta }; }); } }; export { ChangeSetCreator, ChangeType };