39 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-02-29 16:28:11 -08:00
const argv = require('minimist')(process.argv.slice(2));
const format = require('date-fns/format');
const parse = require('date-fns/parse');
const random = require('./lib/random');
const path = require('path');
const fs = require('fs-extra');
const log = require('fancy-log');
const template = require('./_post-template');
const { resolve } = require('./resolve');
module.exports = exports = async function newPost () {
var date = argv.date ? parse(argv.date, 'yyyy-MM-dd', new Date()) : new Date();
if (!date.getHours()) {
const now = new Date();
date.setHours(now.getHours());
date.setMinutes(now.getMinutes());
}
// console.log(date);return;
2020-04-07 10:41:13 -07:00
var id = random.id(10).toUpperCase();
2020-02-29 16:28:11 -08:00
var fname = format(date, 'yyyy-MM-dd.HHmm.') + id;
var target = resolve('posts', fname);
var contents = template({ id, date });
if (argv.folder === undefined) {
target += '.md';
} else {
await fs.ensureDir(target);
2020-04-07 10:41:13 -07:00
target = path.join(target, 'index.md');
2020-02-29 16:28:11 -08:00
}
await fs.writeFile(target, contents);
2020-03-05 10:05:33 -08:00
log('Created new post at ' + target);
2020-02-29 16:28:11 -08:00
};