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