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