Skip to content
Permalink
Browse files

Updates script to write metadata

  • Loading branch information
davidmerfield committed Dec 14, 2019
1 parent 4ceeef7 commit 5c9e9c445a3a5ae9c5681e4f07cc52f7a43699cb
Showing with 39 additions and 8 deletions.
  1. +39 −8 scripts/blog/write-data-metadata.js
@@ -1,23 +1,54 @@
var Entry = require("entry");
var Entries = require("entries");
var Blog = require("blog");
var fs = require('fs-extra');
var get = require("../get/blog");
var each = require("../each/entry");

var moment = require('moment');
var yesno = require('yesno');
var localPath = require('helper').localPath;
var path = require('path');
var SUPPORTED_EXTENSIONS = ['.txt', '.md'];
require("moment-timezone");
get(process.argv[2], function(err, user, blog) {
if (err) throw err;
console.log('')
var client = require("../../app/clients")[blog.client];
Entries.each(
blog.id,
function(entry, next) {
if (entry.metadata.date) {
var contents, hasMetadata, dateMetadata;
if (entry.metadata.date) return next();
if (SUPPORTED_EXTENSIONS.indexOf(path.extname(entry.path)) === -1) {
console.log(entry.path, 'Cannot add metadata to unsupported file type')
return next();
}

if ()
console.log(entry.path, 'reading');
contents = fs.readFileSync(localPath(blog.id, entry.path), 'utf8');
hasMetadata = Object.keys(entry.metadata).length > 0;
dateMetadata = 'Date: ' + moment
.utc(entry.dateStamp)
.tz(blog.timeZone)
.format('MMMM D, Y');
console.log('hasMetadata', hasMetadata);
if (hasMetadata) {
contents = dateMetadata + '\n' + ltrim(contents);
} else {
contents = dateMetadata + '\n\n' + ltrim(contents);
}
console.log('--------------------------');
console.log(contents);
console.log('--------------------------');
yesno.ask('Write? (y/n)', false, function(ok) {
if (!ok) return next();
console.log(entry.path, 'writing');
client.write(blog.id, entry.path, contents, next);
});
},
function() {
console.log("Done!");
process.exit();
}
);
});

function ltrim(str) {
if (!str) return str;
return str.replace(/^\s+/g, '');
}

0 comments on commit 5c9e9c4

Please sign in to comment.
You can’t perform that action at this time.