]>
Commit | Line | Data |
---|---|---|
6cd62e79 RBR |
1 | import { access, cp, readdir, readFile, writeFile } from 'fs/promises'; |
2 | import { exec } from 'child_process'; | |
3 | import { basename, resolve, join } from 'path'; | |
4 | import ParseGemini from 'gemini-to-html/parse.js'; | |
5 | import RenderGemini from 'gemini-to-html/render.js'; | |
6 | import { debuglog, promisify } from 'util'; | |
7 | import { ensureDirectoryExists, rmIfExists } from './utils.js'; | |
8 | import { kFileNotFoundError } from './constants.js'; | |
cf630290 | 9 | |
fac54389 RBR |
10 | // Generators for the Blog |
11 | ||
6cd62e79 RBR |
12 | import StaticGenerator from './generators/static.js'; |
13 | import HTMLGenerator from './generators/html.js'; | |
14 | import RSSGenerator from './generators/rss.js'; | |
15 | import TXTGenerator from './generators/txt.js'; | |
67fdfa7c | 16 | |
fac54389 RBR |
17 | // Archiving Methods |
18 | ||
6cd62e79 | 19 | import GemlogArchiver from './archivers/gemlog.js'; |
fac54389 | 20 | |
c5cbbd38 RBR |
21 | // Remote Handler |
22 | ||
6cd62e79 | 23 | import Remote from './remote.js'; |
c5cbbd38 | 24 | |
cf630290 BB |
25 | const internals = { |
26 | ||
27 | // Promisified functions | |
fac54389 | 28 | exec: promisify(exec), |
cf630290 | 29 | |
d92ac8cc | 30 | debuglog: debuglog('blog'), |
cf630290 BB |
31 | |
32 | // constants | |
33 | ||
fac54389 | 34 | kGeminiRe: /\.gmi$/i, |
6f72ad0f | 35 | kMetadataFilename: 'metadata.json', |
cf630290 BB |
36 | |
37 | // Strings | |
38 | ||
39 | strings: { | |
fac54389 | 40 | geminiNotFound: 'Gemini file was not found in blog directory. Please update.' |
cf630290 BB |
41 | } |
42 | }; | |
43 | ||
44 | /** | |
45 | * The Blog class is the blog generator, it's in charge of adding and | |
46 | * updating posts, and handling the publishing. | |
47 | * | |
48 | * @class Blog | |
67fdfa7c | 49 | * @param {Blog.tConfiguration} config the initialization options to |
cf630290 BB |
50 | * extend the instance |
51 | */ | |
6cd62e79 | 52 | export default class Blog { |
cf630290 BB |
53 | |
54 | constructor(config) { | |
55 | ||
56 | Object.assign(this, config); | |
57 | } | |
58 | ||
59 | /** | |
2acbdb03 | 60 | * Shifts the blog posts, adds the passed file to slot 0, and |
cf630290 BB |
61 | * generates files. |
62 | * | |
63 | * @function add | |
64 | * @memberof Blog | |
2acbdb03 | 65 | * @param {string} postLocation the path to the blog post file |
cf630290 BB |
66 | * @return {Promise<undefined>} empty promise, returns no value |
67 | * @instance | |
68 | */ | |
69 | async add(postLocation) { | |
70 | ||
d3f282a1 | 71 | await ensureDirectoryExists(this.postsDirectory); |
c5cbbd38 RBR |
72 | try { |
73 | await this.syncDown(); | |
74 | } | |
75 | catch {}; | |
cf630290 | 76 | await this._shift(); |
bd8c1fa2 | 77 | const firstDirectory = join(this.postsDirectory, '0'); |
d3f282a1 RBR |
78 | await rmIfExists(firstDirectory); |
79 | await ensureDirectoryExists(firstDirectory); | |
bd8c1fa2 | 80 | await this._update(postLocation); |
cf630290 BB |
81 | } |
82 | ||
83 | /** | |
2acbdb03 | 84 | * Update slot 0 with the passed gmi file, and generates files. |
cf630290 BB |
85 | * |
86 | * @function update | |
87 | * @memberof Blog | |
2acbdb03 | 88 | * @param {string} postLocation the path to the blog post file |
cf630290 BB |
89 | * @return {Promise<undefined>} empty promise, returns no value |
90 | * @instance | |
91 | */ | |
92 | async update(postLocation) { | |
93 | ||
c5cbbd38 RBR |
94 | try { |
95 | await this.syncDown(); | |
96 | } | |
97 | catch {}; | |
97b33932 | 98 | const metadata = await this._update(postLocation); |
cf630290 BB |
99 | } |
100 | ||
101 | /** | |
102 | * Publishes the files to a static host. | |
103 | * | |
104 | * @function publish | |
105 | * @memberof Blog | |
106 | * @return {Promise<undefined>} empty promise, returns no value | |
107 | * @instance | |
108 | */ | |
d50db372 | 109 | async publish(host) { |
fac54389 | 110 | |
d50db372 | 111 | internals.debuglog(`Publishing to ${host}`); |
fac54389 | 112 | try { |
6cd62e79 | 113 | await internals.exec('which rsync'); |
fac54389 RBR |
114 | } |
115 | catch (err) { | |
6cd62e79 | 116 | console.error('Please install and configure rsync to publish.'); |
fac54389 RBR |
117 | } |
118 | ||
119 | try { | |
6cd62e79 RBR |
120 | internals.debuglog(`Copying ephemeral blog from ${this.blogOutputDirectory}`); |
121 | await internals.exec(`rsync -r ${this.blogOutputDirectory}/ ${host}`); | |
fac54389 RBR |
122 | } |
123 | catch (err) { | |
124 | console.error('Failed to publish'); | |
125 | console.error(err.stderr); | |
126 | } | |
cf630290 | 127 | |
fac54389 | 128 | internals.debuglog('Finished publishing'); |
cf630290 BB |
129 | } |
130 | ||
65d379f5 RBR |
131 | /** |
132 | * Publishes the archive to a host using rsync. Currently assumes | |
133 | * gemlog archive. | |
134 | * | |
135 | * @function publishArchive | |
136 | * @memberof Blog | |
137 | * @return {Promise<undefined>} empty promise, returns no value | |
138 | * @instance | |
139 | */ | |
140 | async publishArchive(host) { | |
141 | ||
142 | internals.debuglog(`Publishing archive to ${host}`); | |
143 | try { | |
144 | await internals.exec('which rsync'); | |
145 | } | |
146 | catch (err) { | |
147 | console.error('Please install rsync to publish the archive.'); | |
148 | } | |
149 | ||
150 | try { | |
6cd62e79 RBR |
151 | internals.debuglog(`Copying archive from ${this.archiveOutputDirectory}`); |
152 | await internals.exec(`rsync -r ${this.archiveOutputDirectory}/ ${host}`); | |
65d379f5 RBR |
153 | } |
154 | catch (err) { | |
155 | console.error('Failed to publish archive'); | |
156 | console.error(err.stderr); | |
157 | } | |
158 | ||
159 | internals.debuglog('Finished publishing'); | |
160 | } | |
161 | ||
c5cbbd38 RBR |
162 | /** |
163 | * Adds a remote | |
164 | * | |
165 | * @function addRemote | |
166 | * @memberof Blog | |
167 | * @return {Promise<undefined>} empty promise, returns no value | |
168 | * @instance | |
169 | */ | |
170 | async addRemote(remote) { | |
6cd62e79 | 171 | await ensureDirectoryExists(this.configDirectory); |
c5cbbd38 RBR |
172 | await Remote.add(this.remoteConfig, remote) |
173 | } | |
174 | ||
175 | /** | |
176 | * Removes a remote | |
177 | * | |
178 | * @function removeRemote | |
179 | * @memberof Blog | |
180 | * @return {Promise<undefined>} empty promise, returns no value | |
181 | * @instance | |
182 | */ | |
183 | async removeRemote() { | |
184 | await Remote.remove(this.remoteConfig) | |
185 | } | |
186 | ||
187 | ||
188 | /** | |
189 | * Pulls the posts and archive from the remote | |
190 | * | |
191 | * @function syncDown | |
192 | * @memberof Blog | |
193 | * @return {Promise<undefined>} empty promise, returns no value | |
194 | * @instance | |
195 | */ | |
196 | async syncDown() { | |
bd8c1fa2 | 197 | internals.debuglog('Pulling remote state'); |
6cd62e79 RBR |
198 | await ensureDirectoryExists(this.dataDirectory); |
199 | await Remote.syncDown(this.remoteConfig, this.dataDirectory) | |
bd8c1fa2 | 200 | internals.debuglog('Pulled remote state'); |
c5cbbd38 RBR |
201 | } |
202 | ||
203 | /** | |
204 | * Pushes the posts and archive to the remote | |
205 | * | |
206 | * @function syncUp | |
207 | * @memberof Blog | |
208 | * @return {Promise<undefined>} empty promise, returns no value | |
209 | * @instance | |
210 | */ | |
211 | async syncUp() { | |
bd8c1fa2 | 212 | internals.debuglog('Pushing remote state'); |
6cd62e79 RBR |
213 | await ensureDirectoryExists(this.dataDirectory); |
214 | await Remote.syncUp(this.remoteConfig, this.dataDirectory) | |
bd8c1fa2 | 215 | internals.debuglog('Pushed remote state'); |
c5cbbd38 RBR |
216 | } |
217 | ||
bd8c1fa2 RBR |
218 | // Adds the passed path to slot 0, and generates files. |
219 | ||
220 | async _update(postLocation) { | |
221 | ||
222 | const metadata = await this._getMetadata(); | |
d3f282a1 | 223 | await ensureDirectoryExists(this.postsDirectory); |
bd8c1fa2 RBR |
224 | await this._copyPost(postLocation); |
225 | await this._writeMetadata(metadata); | |
226 | ||
227 | await this._archive(postLocation); | |
228 | ||
229 | await this.generate(); | |
230 | try { | |
231 | await this.syncUp(); | |
232 | } | |
233 | catch {}; | |
234 | } | |
235 | ||
236 | ||
fac54389 | 237 | // Parses Gemini for each page, copies assets and generates index. |
cf630290 | 238 | |
e54f8139 | 239 | async generate() { |
cf630290 | 240 | |
67fdfa7c BB |
241 | internals.debuglog('Generating output'); |
242 | ||
fac54389 | 243 | const posts = await this._readPosts(); |
67fdfa7c | 244 | |
6cd62e79 RBR |
245 | // Start from a clean slate. |
246 | await rmIfExists(this.blogOutputDirectory); | |
247 | await ensureDirectoryExists(this.blogOutputDirectory); | |
248 | ||
249 | // Run each generator | |
250 | await StaticGenerator(this.staticDirectory, this.blogOutputDirectory, posts); | |
251 | await HTMLGenerator(await this._templateDirectoryFor('index.html'), this.blogOutputDirectory, posts); | |
252 | await RSSGenerator(await this._templateDirectoryFor('feed.xml'), this.blogOutputDirectory, posts); | |
253 | await TXTGenerator(await this._templateDirectoryFor('index.txt'), this.blogOutputDirectory, posts); | |
254 | ||
255 | // Start from a clean slate. | |
256 | await rmIfExists(this.archiveOutputDirectory); | |
257 | await ensureDirectoryExists(this.archiveOutputDirectory); | |
258 | await ensureDirectoryExists(this.archiveDirectory); | |
fac54389 | 259 | |
6cd62e79 RBR |
260 | // Run each archiver |
261 | await GemlogArchiver(await this._templateDirectoryFor('index.gmi'), this.archiveDirectory, this.archiveOutputDirectory); | |
262 | // TODO: GopherArchiver | |
67fdfa7c BB |
263 | } |
264 | ||
265 | // Reads the posts into an array | |
cf630290 | 266 | |
fac54389 | 267 | async _readPosts() { |
67fdfa7c BB |
268 | |
269 | internals.debuglog('Reading posts'); | |
270 | const posts = []; | |
cf630290 BB |
271 | |
272 | for (let i = 0; i < this.maxPosts; ++i) { | |
67fdfa7c | 273 | try { |
fac54389 | 274 | posts.push(await this._readPost(i)); |
eccb3cc4 BB |
275 | } |
276 | catch (error) { | |
d3f282a1 | 277 | if (error.code === kFileNotFoundError) { |
eccb3cc4 BB |
278 | internals.debuglog(`Skipping ${i}`); |
279 | continue; | |
280 | } | |
281 | ||
282 | throw error; | |
283 | } | |
cf630290 BB |
284 | } |
285 | ||
67fdfa7c | 286 | return posts; |
cf630290 BB |
287 | } |
288 | ||
fac54389 RBR |
289 | // Reads an individual post |
290 | ||
291 | async _readPost(index=0) { | |
292 | const postSourcePath = join(this.postsDirectory, `${index}`); | |
293 | ||
294 | internals.debuglog(`Reading ${postSourcePath}`); | |
295 | ||
296 | await access(postSourcePath); | |
297 | ||
298 | const metadata = await this._getMetadata(index); | |
299 | ||
300 | const postContentPath = await this._findBlogContent(postSourcePath); | |
301 | internals.debuglog(`Reading ${postContentPath}`); | |
302 | const postContent = await readFile(postContentPath, { encoding: 'utf8' }); | |
303 | ||
304 | internals.debuglog('Parsing Gemini'); | |
305 | return { | |
306 | ...metadata, | |
307 | location: postSourcePath, | |
308 | index, | |
309 | html: RenderGemini(ParseGemini(postContent)), | |
310 | raw: postContent | |
311 | }; | |
312 | } | |
313 | ||
cf630290 BB |
314 | // Shift the posts, delete any remainder. |
315 | ||
316 | async _shift() { | |
317 | ||
cf630290 | 318 | |
24de2f06 | 319 | for (let i = this.maxPosts - 1; i >= 1; --i) { |
d92ac8cc BB |
320 | const targetPath = join(this.postsDirectory, `${i}`); |
321 | const sourcePath = join(this.postsDirectory, `${i - 1}`); | |
cf630290 BB |
322 | |
323 | try { | |
fac54389 | 324 | internals.debuglog(`Archiving ${targetPath}`); |
d3f282a1 | 325 | await rmIfExists(targetPath); |
6f72ad0f BB |
326 | await access(sourcePath); // check the source path |
327 | ||
cf630290 | 328 | internals.debuglog(`Shifting blog post ${sourcePath} to ${targetPath}`); |
c5cbbd38 | 329 | await cp(sourcePath, targetPath, { recursive: true }); |
cf630290 BB |
330 | } |
331 | catch (error) { | |
d3f282a1 | 332 | if (error.code === kFileNotFoundError) { |
cf630290 BB |
333 | internals.debuglog(`Skipping ${sourcePath}: Does not exist.`); |
334 | continue; | |
335 | } | |
336 | ||
337 | throw error; | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
fac54389 RBR |
342 | // Moves older posts to the archive |
343 | ||
344 | async _archive() { | |
345 | internals.debuglog('Archiving post'); | |
346 | const post = await this._readPost(0); | |
d3f282a1 | 347 | await ensureDirectoryExists(this.archiveDirectory); |
fac54389 RBR |
348 | |
349 | const targetPath = join(this.archiveDirectory, post.id); | |
350 | ||
c5cbbd38 | 351 | internals.debuglog(`Removing ${targetPath}`); |
d3f282a1 | 352 | await rmIfExists(targetPath); |
c5cbbd38 | 353 | internals.debuglog(`Adding ${post.location} to ${targetPath}`); |
d3f282a1 | 354 | await ensureDirectoryExists(targetPath); |
c5cbbd38 RBR |
355 | await cp(post.location, targetPath, { recursive: true }); |
356 | internals.debuglog(`Added ${post.location} to ${targetPath}`); | |
fac54389 RBR |
357 | } |
358 | ||
6f72ad0f BB |
359 | // Attempts to read existing metadata. Otherwise generates new set. |
360 | ||
67fdfa7c | 361 | async _getMetadata(index = 0) { |
6f72ad0f | 362 | |
67fdfa7c | 363 | const metadataTarget = join(this.postsDirectory, String(index), internals.kMetadataFilename); |
6f72ad0f BB |
364 | |
365 | try { | |
366 | internals.debuglog(`Looking for metadata at ${metadataTarget}`); | |
67fdfa7c | 367 | return JSON.parse(await readFile(metadataTarget, { encoding: 'utf8' })); |
6f72ad0f BB |
368 | } |
369 | catch (e) { | |
370 | internals.debuglog(`Metadata not found or unreadable. Generating new set.`); | |
371 | const createdOn = Date.now(); | |
372 | const metadata = { | |
373 | id: String(createdOn), | |
374 | createdOn | |
375 | }; | |
376 | ||
67fdfa7c | 377 | return metadata; |
6f72ad0f BB |
378 | } |
379 | } | |
380 | ||
381 | // Writes metadata. Assumes post 0 since it only gets written | |
382 | // on create | |
383 | ||
384 | async _writeMetadata(metadata) { | |
385 | ||
386 | const metadataTarget = join(this.postsDirectory, '0', internals.kMetadataFilename); | |
387 | internals.debuglog(`Writing ${metadataTarget}`); | |
67fdfa7c | 388 | await writeFile(metadataTarget, JSON.stringify(metadata, null, 2)); |
6f72ad0f BB |
389 | } |
390 | ||
2acbdb03 | 391 | // Copies a post file to the latest slot. |
cf630290 BB |
392 | |
393 | async _copyPost(postLocation) { | |
394 | ||
2acbdb03 | 395 | internals.debuglog(`Copying ${postLocation}`); |
d92ac8cc | 396 | const targetPath = join(this.postsDirectory, '0'); |
24de2f06 RBR |
397 | const postName = basename(postLocation); |
398 | const targetPost = join(targetPath, postName); | |
cf630290 | 399 | |
d3f282a1 RBR |
400 | await rmIfExists(targetPath); |
401 | await ensureDirectoryExists(targetPath); | |
c5cbbd38 RBR |
402 | await cp(postLocation, targetPost, { recursive: true }); |
403 | internals.debuglog(`Added ${postLocation} to ${targetPath}`); | |
cf630290 BB |
404 | } |
405 | ||
fac54389 | 406 | // Looks for a `.gmi` file in the blog directory, and returns the path |
cf630290 BB |
407 | |
408 | async _findBlogContent(directory) { | |
409 | ||
d92ac8cc | 410 | const entries = await readdir(directory); |
cf630290 | 411 | |
fac54389 RBR |
412 | const geminiEntries = entries |
413 | .filter((entry) => internals.kGeminiRe.test(entry)) | |
d92ac8cc | 414 | .map((entry) => join(directory, entry)); |
cf630290 | 415 | |
fac54389 RBR |
416 | if (geminiEntries.length > 0) { |
417 | internals.debuglog(`Found gemini file: ${geminiEntries[0]}`); | |
418 | return geminiEntries[0]; | |
cf630290 BB |
419 | } |
420 | ||
fac54389 | 421 | throw new Error(internals.strings.geminiNotFound); |
cf630290 | 422 | } |
6cd62e79 RBR |
423 | |
424 | // Gets the template directory for a given template. | |
425 | async _templateDirectoryFor(template) { | |
426 | try { | |
427 | await access(join(this.templatesDirectory, template)); | |
428 | return this.templatesDirectory; | |
429 | } | |
430 | catch (error) { | |
431 | if (error.code === kFileNotFoundError) { | |
432 | internals.debuglog(`No custom template for ${template}`); | |
433 | return this.defaultTemplatesDirectory; | |
434 | } | |
435 | ||
436 | throw error; | |
437 | } | |
438 | } | |
cf630290 | 439 | }; |