1 | var __defProp = Object.defineProperty;
|
2 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
3 | var __publicField = (obj, key, value) => {
|
4 | __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
5 | return value;
|
6 | };
|
7 | import { NotFoundError } from "./errors.js";
|
8 | import { createDebugger } from "./lib/createDebugger.js";
|
9 | import { pathToFilePath } from "./lib/pathToFilePath.js";
|
10 | import { toReadonlyMap } from "./lib/toReadonlyMap.js";
|
11 | const debug = createDebugger("akte:files");
|
12 | const debugRender = createDebugger("akte:files:render");
|
13 | const debugCache = createDebugger("akte:files:cache");
|
14 | class AkteFiles {
|
15 | constructor(definition) {
|
16 | __publicField(this, "definition");
|
17 | __publicField(this, "_dataMapCache", new Map());
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | __publicField(this, "getData", (context) => {
|
27 | const maybePromise = this._dataMapCache.get(context.path);
|
28 | if (maybePromise) {
|
29 | debugCache("using cached data %o", context.path);
|
30 | return maybePromise;
|
31 | }
|
32 | debugCache("retrieving data... %o", context.path);
|
33 | let promise;
|
34 | if (this.definition.data) {
|
35 | promise = this.definition.data(context);
|
36 | } else if (this.definition.bulkData) {
|
37 | const dataFromBulkData = async (path) => {
|
38 | const bulkData = await this.getBulkData({
|
39 | globalData: context.globalData
|
40 | });
|
41 | if (path in bulkData) {
|
42 | return bulkData[path];
|
43 | }
|
44 | throw new NotFoundError(path);
|
45 | };
|
46 | promise = dataFromBulkData(context.path);
|
47 | } else {
|
48 | throw new Error(`Cannot render file for path \`${context.path}\`, no \`data\` or \`bulkData\` function available`);
|
49 | }
|
50 | if (promise instanceof Promise) {
|
51 | promise.then(() => {
|
52 | debugCache("retrieved data %o", context.path);
|
53 | }).catch(() => {
|
54 | });
|
55 | } else {
|
56 | debugCache("retrieved data %o", context.path);
|
57 | }
|
58 | this._dataMapCache.set(context.path, promise);
|
59 | return promise;
|
60 | });
|
61 | __publicField(this, "_bulkDataCache");
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | __publicField(this, "getBulkData", (context) => {
|
71 | var _a, _b;
|
72 | if (!this._bulkDataCache) {
|
73 | debugCache("retrieving bulk data... %o", this.path);
|
74 | const bulkDataPromise = ((_b = (_a = this.definition).bulkData) == null ? void 0 : _b.call(_a, context)) || {};
|
75 | if (bulkDataPromise instanceof Promise) {
|
76 | bulkDataPromise.then(() => {
|
77 | debugCache("retrieved bulk data %o", this.path);
|
78 | });
|
79 | } else {
|
80 | debugCache("retrieved bulk data %o", this.path);
|
81 | }
|
82 | this._bulkDataCache = bulkDataPromise;
|
83 | } else {
|
84 | debugCache("using cached bulk data %o", this.path);
|
85 | }
|
86 | return this._bulkDataCache;
|
87 | });
|
88 | this.definition = definition;
|
89 | debug("defined %o", this.path);
|
90 | }
|
91 |
|
92 | get path() {
|
93 | return this.definition.path;
|
94 | }
|
95 |
|
96 | async render(args) {
|
97 | const data = args.data || await this.getData(args);
|
98 | return this.definition.render({
|
99 | path: args.path,
|
100 | globalData: args.globalData,
|
101 | data
|
102 | });
|
103 | }
|
104 |
|
105 | async renderAll(args) {
|
106 | if (!this.definition.bulkData) {
|
107 | debugRender("no files to render %o", this.path);
|
108 | return {};
|
109 | }
|
110 | debugRender("rendering files... %o", this.path);
|
111 | const bulkData = await this.getBulkData(args);
|
112 | const render = async (path, data) => {
|
113 | const content = await this.definition.render({
|
114 | path,
|
115 | globalData: args.globalData,
|
116 | data
|
117 | });
|
118 | debugRender("rendered %o", path);
|
119 | return [pathToFilePath(path), content];
|
120 | };
|
121 | const promises = [];
|
122 | for (const path in bulkData) {
|
123 | const data = bulkData[path];
|
124 | promises.push(render(path, data));
|
125 | }
|
126 | const fileEntries = await Promise.all(Object.values(promises));
|
127 | debugRender(`rendered %o ${fileEntries.length > 1 ? "files" : "file"} %o`, fileEntries.length, this.path);
|
128 | return Object.fromEntries(fileEntries);
|
129 | }
|
130 |
|
131 | clearCache() {
|
132 | this._dataMapCache.clear();
|
133 | this._bulkDataCache = void 0;
|
134 | }
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 | get dataMapCache() {
|
141 | return toReadonlyMap(this._dataMapCache);
|
142 | }
|
143 | |
144 |
|
145 |
|
146 |
|
147 |
|
148 | get bulkDataCache() {
|
149 | return this._bulkDataCache;
|
150 | }
|
151 | }
|
152 | export {
|
153 | AkteFiles
|
154 | };
|
155 |
|