@strifeapp/astro 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ import type { AstroIntegration } from 'astro';
2
+ import type { IAuthOptions } from 'ravendb';
3
+ export type IntegrationOptions = IAuthOptions & {
4
+ certificate?: string;
5
+ password?: string;
6
+ urls?: string[];
7
+ database?: string;
8
+ collections?: any[];
9
+ };
10
+ export default function strifeIntegration(options?: IntegrationOptions): AstroIntegration;
package/dist/index.js CHANGED
@@ -1,309 +1,39 @@
1
- import { loadEnv as f } from "/Users/marcus/Library/Mobile Documents/com~apple~CloudDocs/Projects.nosync/ideas/astro-strife/node_modules/vite/dist/node/index.js";
2
- import r from "/Users/marcus/Library/Mobile Documents/com~apple~CloudDocs/Projects.nosync/ideas/astro-strife/node_modules/serialize-javascript/index.js";
3
- const y = `/**
4
- * Creates an indexed item that will be stored in the index.
5
- * @param {string} name - Name of property to index
6
- * @param {any} value - Value to index and store
7
- * @returns a new object to index and store
8
- */
9
- function storeAs(name, value) {
10
- return {
11
- $value: value,
12
- $name: name,
13
- $options: { storage: true }
14
- };
15
- }
16
-
17
- /**
18
- * Create an indexed item without storing value in the index.
19
- * @param {string} name - Name of the property to index
20
- * @param {any} value - The value to index
21
- * @returns a new object to index (no store)
22
- */
23
- function indexAs(name, value) {
24
- return {
25
- $value: value,
26
- $name: name,
27
- $options: { storage: false }
28
- };
29
- }
30
-
31
- /**
32
- * Loads the documents referenced by reference objects in the array
33
- * @param {any} references - Array of document references: { id, collection }
34
- * @returns The list of loaded documents.
35
- */
36
- function loadRelatedDocuments(references) {
37
- if (references == null) return null;
38
- return references
39
- .filter(item => typeof item.id === "string") // Ensure each ID is a string
40
- .map(item => load(item.id, 'Labels'))
41
- .filter(Boolean); // Filter out any null/undefined values
42
- }
43
-
44
-
45
- /**
46
- * Builds an url for a document by traversing its ancestor origins
47
- * @param {*} doc - The leaf document
48
- * @returns The full url of the document
49
- */
50
- function buildUrl(doc) {
51
- const visited = {};
52
- const slugs = [];
53
- let c = doc;
54
-
55
- do {
56
- if (c.slug) {
57
- slugs.unshift(c.slug);
58
- }
59
- if (!c.origin || visited[c.origin.id]) break;
60
- visited[c.origin.id] = true;
61
- c = load(c.origin.id, c.origin.collection);
62
- } while (c);
63
-
64
- return slugs.shift() + slugs.join('/');
65
- }
66
-
67
- /**
68
- * Editor types that contains {collection, id} references that we want to load
69
- */
70
- const RELATION_EDITORS = ["related", "references"];
71
-
72
- /**
73
- * Loads the template document of the specified document
74
- * @param {any} document - The document to index
75
- * @returns The document's template, loaded
76
- */
77
- function loadTemplate(document) {
78
- const collection = document['@metadata']['@collection'];
79
- const template = load('templates/' + collection, 'templates');
80
- return template;
81
- }
82
-
83
- /**
84
- * Iterates the template's editors and copies the respective values to the
85
- * result object. Properties containing references are resolved recursively.
86
- * Mutually recursive with \`loadRelatedDocumentsRecurse\`.
87
- * @param {*} document - The document to extract values from
88
- * @param {*} template - The document's template
89
- * @param {*} result - The populated result
90
- */
91
- function populateValuesByEditor(document, template, result) {
92
- // Add dynamic fields and handle "related" type
93
- template.editors.forEach(editor => {
94
- if (editor.editor && editor.editor.propertyName) {
95
- const propertyName = editor.editor.propertyName;
96
-
97
- if (RELATION_EDITORS.includes(editor.editor.type) && Array.isArray(document[propertyName])) {
98
- // Load related documents in the array of document references
99
- const relatedDocs = loadRelatedDocumentsRecurse(document[propertyName])
100
- result[propertyName] = relatedDocs;
101
- } else if (document[propertyName] !== undefined) {
102
- // For other types, index the field value directly
103
- result[propertyName] = document[propertyName];
104
- }
105
- }
106
- });
107
- if (document['@strife']) {
108
- result['@strife'] = document['@strife'];
109
- }
110
- }
111
-
112
- /**
113
- * Loads the documents referenced by the array of references. Mutually recursive
114
- * with \`populateValuesByEditor\`.
115
- * @param {*} references - List of references to load.
116
- * @returns
117
- */
118
- function loadRelatedDocumentsRecurse(references) {
119
- if (references == null) return null;
120
- const documents = references
121
- //.filter(id => typeof id === "string" || (typeof id === "object" && id.id)) // Ensure each ID is a string or object with an id
122
- .map(id => (typeof id === "string" ? id : id.id))
123
- .map(id => (load(id, 'Contacts') || load(id, 'Articles') || load(id, 'ContentTexts') || load(id, 'Notices') || load(id, 'EducationPages')))
124
- .filter(Boolean); // Filter out any null/undefined values
125
-
126
- const docResults = [];
127
- documents.forEach(doc => {
128
- const template = loadTemplate(doc);
129
- const docResult = {
130
- id: Id(doc),
131
- url: buildUrl(doc),
132
- collection: doc['@metadata']['@collection'],
133
- labels: loadRelatedDocuments(
134
- doc.labels.map(l => { return { id: l, collection: "Labels" } })),
135
- };
136
- populateValuesByEditor(doc, template, docResult);
137
- docResults.push(docResult)
138
- });
139
-
140
- return docResults;
141
- }
142
-
143
- /**
144
- * Maps a document to an index entry for Strife's content index
145
- * @param {any} document - The document to index
146
- * @returns An object to be indexed by RavenDB
147
- */
148
- function mapDocument(document) {
149
- const collection = document['@metadata']['@collection'];
150
- const template = load('templates/' + collection, 'templates');
151
-
152
- if (!template || !template.editors || document.deleted || document.archived) {
153
- return null;
154
- }
155
-
156
- // Initialize result with static fields
157
- const result = {
158
- id: Id(document),
159
- displayName: document.displayName,
160
- url: storeAs('url', buildUrl(document)),
161
- origin: document.origin,
162
- collection: storeAs('collection', collection),
163
- publishedDate: document.publishedDate,
164
- createdAt: document.createdAt,
165
- labels: storeAs('labels', loadRelatedDocuments(
166
- document.labels.map(l => { return { id: l, collection: "Labels" } }))),
167
- deleted: document.deleted,
168
- };
169
-
170
- // Add dynamic fields and handle "related" type
171
- template.editors.forEach(editor => {
172
- if (editor.editor && editor.editor.propertyName) {
173
- const propertyName = editor.editor.propertyName;
174
-
175
- if (RELATION_EDITORS.includes(editor.editor.type) && Array.isArray(document[propertyName])) {
176
- // Load related documents in the array of document references
177
- const relatedDocs = loadRelatedDocumentsRecurse(document[propertyName]);
178
- result[propertyName] = storeAs(propertyName, relatedDocs);
179
- } else if (editor.editor.type === 'content-template') {
180
- const content = document[propertyName];
181
- if (content) {
182
- const template = load(editor.editor.attributes.templateId, 'templates');
183
- const docResult = {};
184
- populateValuesByEditor(content, template, docResult);
185
- result[propertyName] = storeAs(propertyName, docResult);
186
- }
187
- } else if (editor.editor.type === 'chapters') {
188
- const chapters = document[propertyName];
189
- if (chapters && Array.isArray(chapters)) {
190
- const docResults = [];
191
- chapters.forEach(chapter => {
192
- if (chapter['@strife'] && chapter['@strife'].templateId) {
193
- const docResult = {};
194
- const template = load(chapter['@strife'].templateId, 'templates');
195
- populateValuesByEditor(chapter, template, docResult);
196
- docResults.push(docResult);
197
- }
198
- });
199
- result[propertyName] = storeAs(propertyName, docResults);
200
- }
201
- }
202
- else if (document[propertyName] !== undefined) {
203
- // For other types, index the field value directly
204
- result[propertyName] = indexAs(propertyName, document[propertyName]);
205
- }
206
- }
207
- });
208
-
209
- return result;
210
- }
211
- `, d = "strife:store", s = "\0" + d;
212
- function h(e) {
213
- return {
214
- name: "strife:store",
215
- resolveId(n) {
216
- if (n === d)
217
- return s;
218
- },
219
- load(n) {
220
- var t;
221
- if (n === s)
222
- return `
223
- import { DocumentStore, AbstractJavaScriptMultiMapIndexCreationTask, IndexCreation } from "ravendb";
224
-
225
- const authOptions = {
226
- type: 'pfx',
227
- certificate: Buffer.from(${r(e.certificate)}, 'base64'),
228
- password: ${r(e.password)},
229
- };
230
-
231
- const store = new DocumentStore(
232
- ${r(e.urls ? e.urls : [])},
233
- ${r(e.database || "")},
234
- authOptions
235
- ).initialize()
236
-
237
- class Content_ByUrlzz extends AbstractJavaScriptMultiMapIndexCreationTask {
238
- constructor() {
239
- super();
240
-
241
- this.additionalSources = {"Helper": ${r(y)}}
242
-
243
- ${(e.collections || ["Posts"]).map(
244
- (o) => `this.map("${o.name}", (doc) => mapDocument(doc));`
245
- ).join(`
246
- `)}
247
-
248
- this.deploymentMode = 'Rolling';
249
- this.searchEngineType = 'Lucene';
250
- }
251
- }
252
-
253
- console.log('Deploying index...');
254
- await IndexCreation.createIndexes([new Content_ByUrlzz()], store);
255
-
256
- const bulkInsert = store.bulkInsert();
257
-
258
- ${(t = e.collections) == null ? void 0 : t.map((o) => `
259
- bulkInsert.store(${r(o)}, 'templates/${o.name}', { '@collection': 'Templates' });
260
- `).join(`
261
- `)}
262
-
263
- await bulkInsert.finish();
264
-
265
- export { store };
266
- `;
267
- }
268
- };
269
- }
270
- const v = {
271
- type: "pfx"
1
+ import { loadEnv } from "vite";
2
+ // Import the vite plugin directly from the file
3
+ import { vitePluginStrifeStore } from './vite-plugin-strife-store.js';
4
+ const defaultClientConfig = {
5
+ type: 'pfx',
272
6
  };
273
- function A(e = {}) {
274
- let n, t;
275
- return {
276
- name: "@strife/astro",
277
- hooks: {
278
- "astro:config:setup": ({ command: o, config: i, updateConfig: l }) => {
279
- var a;
280
- n = o, t = f(n, i.vite.envDir ?? "", "");
281
- const c = {
282
- urls: (a = t.STRIFE_DATABASE_URLS) == null ? void 0 : a.split(","),
283
- database: t.STRIFE_DATABASE,
284
- certificate: t.STRIFE_CERTIFICATE,
285
- password: t.STRIFE_CERTIFICATE_PASSWORD
286
- }, u = Object.fromEntries(
287
- Object.entries(c).filter(([R, m]) => m !== void 0)
288
- ), p = {
289
- ...v,
290
- // Base defaults
291
- ...u,
292
- // Environment variables override defaults
293
- ...e
294
- // Explicit options override everything
295
- };
296
- l({
297
- vite: {
298
- plugins: [
299
- h(p)
300
- ]
301
- }
302
- });
303
- }
304
- }
305
- };
7
+ export default function strifeIntegration(options = {}) {
8
+ let mode;
9
+ let env;
10
+ return {
11
+ name: '@strife/astro',
12
+ hooks: {
13
+ 'astro:config:setup': ({ command, config, updateConfig }) => {
14
+ mode = command;
15
+ env = loadEnv(mode, config.vite.envDir ?? '', '');
16
+ const envOptions = {
17
+ urls: env.STRIFE_DATABASE_URLS?.split(','),
18
+ database: env.STRIFE_DATABASE,
19
+ certificate: env.STRIFE_CERTIFICATE,
20
+ password: env.STRIFE_CERTIFICATE_PASSWORD
21
+ };
22
+ // Filter out undefined values from env
23
+ const filteredEnvOptions = Object.fromEntries(Object.entries(envOptions).filter(([_, value]) => value !== undefined));
24
+ const mergedOptions = {
25
+ ...defaultClientConfig, // Base defaults
26
+ ...filteredEnvOptions, // Environment variables override defaults
27
+ ...options // Explicit options override everything
28
+ };
29
+ updateConfig({
30
+ vite: {
31
+ plugins: [
32
+ vitePluginStrifeStore(mergedOptions)
33
+ ],
34
+ }
35
+ });
36
+ },
37
+ }
38
+ };
306
39
  }
307
- export {
308
- A as default
309
- };
@@ -0,0 +1 @@
1
+ export * from './vite-plugin-strife-store';
@@ -0,0 +1,2 @@
1
+ // This file serves as an entry point for the vite-plugin-strife-store module
2
+ export * from './vite-plugin-strife-store';
@@ -0,0 +1,3 @@
1
+ import type { IntegrationOptions } from './index';
2
+ import type { PluginOption } from 'vite';
3
+ export declare function vitePluginStrifeStore(config: IntegrationOptions): PluginOption;
@@ -0,0 +1,59 @@
1
+ import serialize from 'serialize-javascript';
2
+ import contentIndex from './contentIndex.js?raw';
3
+ const virtualModuleId = 'strife:store';
4
+ const resolvedVirtualModuleId = '\0' + virtualModuleId;
5
+ export function vitePluginStrifeStore(config) {
6
+ return {
7
+ name: 'strife:store',
8
+ resolveId(id) {
9
+ if (id === virtualModuleId) {
10
+ return resolvedVirtualModuleId;
11
+ }
12
+ },
13
+ load(id) {
14
+ if (id === resolvedVirtualModuleId) {
15
+ return `
16
+ import { DocumentStore, AbstractJavaScriptMultiMapIndexCreationTask, IndexCreation } from "ravendb";
17
+
18
+ const authOptions = {
19
+ type: 'pfx',
20
+ certificate: Buffer.from(${serialize(config.certificate)}, 'base64'),
21
+ password: ${serialize(config.password)},
22
+ };
23
+
24
+ const store = new DocumentStore(
25
+ ${serialize(config.urls ? config.urls : [])},
26
+ ${serialize(config.database || '')},
27
+ authOptions
28
+ ).initialize()
29
+
30
+ class Content_ByUrlzz extends AbstractJavaScriptMultiMapIndexCreationTask {
31
+ constructor() {
32
+ super();
33
+
34
+ this.additionalSources = {"Helper": ${serialize(contentIndex)}}
35
+
36
+ ${(config.collections || ['Posts']).map(collection => `this.map("${collection.name}", (doc) => mapDocument(doc));`).join('\n')}
37
+
38
+ this.deploymentMode = 'Rolling';
39
+ this.searchEngineType = 'Lucene';
40
+ }
41
+ }
42
+
43
+ console.log('Deploying index...');
44
+ await IndexCreation.createIndexes([new Content_ByUrlzz()], store);
45
+
46
+ const bulkInsert = store.bulkInsert();
47
+
48
+ ${config.collections?.map(collection => `
49
+ bulkInsert.store(${serialize(collection)}, 'templates/${collection.name}', { '@collection': 'Templates' });
50
+ `).join('\n')}
51
+
52
+ await bulkInsert.finish();
53
+
54
+ export { store };
55
+ `;
56
+ }
57
+ },
58
+ };
59
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strifeapp/astro",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Official Strife Astro integration",
5
5
  "keywords": [
6
6
  "astro-integration",
@@ -11,7 +11,8 @@
11
11
  "types": "dist/index.d.ts",
12
12
  "type": "module",
13
13
  "scripts": {
14
- "build": "vite build"
14
+ "build": "vite build && tsc",
15
+ "prepublishOnly": "npm run build"
15
16
  },
16
17
  "exports": {
17
18
  ".": "./dist/index.js",
@@ -30,7 +31,8 @@
30
31
  "devDependencies": {
31
32
  "@types/dotenv": "^8.2.0",
32
33
  "typescript": "^5.7.3",
33
- "vite": "^6.2.1"
34
+ "vite": "^6.2.1",
35
+ "vite-plugin-dts": "^4.5.3"
34
36
  },
35
37
  "peerDependencies": {
36
38
  "astro": "^4.0.0 || ^5.0.0",