UNPKG

995 BJavaScriptView Raw
1'use strict';
2
3const tildify = require('tildify');
4const chalk = require('chalk');
5
6const reservedKeys = {
7 _: true,
8 title: true,
9 layout: true,
10 slug: true,
11 path: true,
12 replace: true,
13 // Global options
14 config: true,
15 debug: true,
16 safe: true,
17 silent: true
18};
19
20function newConsole(args) {
21 // Display help message if user didn't input any arguments
22 if (!args._.length) {
23 return this.call('help', {_: ['new']});
24 }
25
26 const data = {
27 title: args._.pop(),
28 layout: args._.length ? args._[0] : this.config.default_layout,
29 slug: args.s || args.slug,
30 path: args.p || args.path
31 };
32
33 const keys = Object.keys(args);
34 let key = '';
35 const self = this;
36
37 for (let i = 0, len = keys.length; i < len; i++) {
38 key = keys[i];
39 if (!reservedKeys[key]) data[key] = args[key];
40 }
41
42 return this.post.create(data, args.r || args.replace).then(post => {
43 self.log.info('Created: %s', chalk.magenta(tildify(post.path)));
44 });
45}
46
47module.exports = newConsole;