kIndexName: 'index.html',
kFileNotFoundError: 'ENOENT',
kMarkdownRe: /\.md$/i,
- kRemoveCommand: 'rm -rf',
+ kMetadataFilename: 'metadata.json',
// Strings
*/
async add(postLocation) {
+ await this._ensurePostsDirectoryExists();
await this._shift();
await this.update(postLocation);
}
*/
async update(postLocation) {
+ const metadata = await this._getMetadata();
+ await this._ensurePostsDirectoryExists();
await this._copyPost(postLocation);
+ await this._writeMetadata(metadata);
+
await this._generate();
}
async _shift() {
- await this._ensurePostsDirectoryExists();
- for (let i = this.maxPosts - 1; i > 0; --i) {
+ for (let i = this.maxPosts - 1; i >= 0; --i) {
const targetPath = join(this.postsDirectory, `${i}`);
const sourcePath = join(this.postsDirectory, `${i - 1}`);
try {
- await access(sourcePath);
-
internals.debuglog(`Removing ${targetPath}`);
await rmdir(targetPath, { recursive: true });
+ await access(sourcePath); // check the source path
+
internals.debuglog(`Shifting blog post ${sourcePath} to ${targetPath}`);
await internals.ncp(sourcePath, targetPath);
}
}
}
+ // Attempts to read existing metadata. Otherwise generates new set.
+
+ async _getMetadata() {
+
+ const metadataTarget = join(this.postsDirectory, '0', internals.kMetadataFilename);
+
+ try {
+ internals.debuglog(`Looking for metadata at ${metadataTarget}`);
+ return await readFile(metadataTarget);
+ }
+ catch (e) {
+ internals.debuglog(`Metadata not found or unreadable. Generating new set.`);
+ const createdOn = Date.now();
+ const metadata = {
+ id: String(createdOn),
+ createdOn
+ };
+
+ return JSON.stringify(metadata, null, 2);
+ }
+ }
+
+ // Writes metadata. Assumes post 0 since it only gets written
+ // on create
+
+ async _writeMetadata(metadata) {
+
+ const metadataTarget = join(this.postsDirectory, '0', internals.kMetadataFilename);
+ internals.debuglog(`Writing ${metadataTarget}`);
+ await writeFile(metadataTarget, metadata);
+ }
+
// Copies a post directory to the latest slot.
async _copyPost(postLocation) {
- await this._ensurePostsDirectoryExists();
-
const targetPath = join(this.postsDirectory, '0');
internals.debuglog(`Removing ${targetPath}`);