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