]> git.r.bdr.sh - rbdr/blog/blame - bin/blog.js
Allow regeneration of assets
[rbdr/blog] / bin / blog.js
CommitLineData
cf630290
BB
1#!/usr/bin/env node
2'use strict';
3
4const Config = require('../config/config');
5const Blog = require('..');
6const Minimist = require('minimist');
7
8const internals = {
9 blog: new Blog(Config),
e54f8139 10 expectedKeys: ['add', 'generate', 'update', 'publish'],
cf630290
BB
11
12 // Application entry point. Reads arguments and calls the
13 // corresponding method from the blog lib
14
15 async main() {
16
17 try {
18 const parsedArguments = this._parseArguments();
19
20 for (const argument in parsedArguments) {
21 if (parsedArguments.hasOwnProperty(argument)) {
22
23 const value = parsedArguments[argument];
24
25 if (argument === 'add') {
26 await internals.blog.add(value);
27 return;
28 }
29
30 if (argument === 'update') {
31 await internals.blog.update(value);
32 return;
33 }
34
e54f8139
RBR
35 if (argument === 'generate') {
36 await internals.blog.generate();
37 return;
38 }
39
cf630290 40 if (argument === 'publish') {
39744467 41 await internals.blog.publish(value);
cf630290
BB
42 return;
43 }
44 }
45 }
d92ac8cc 46
cf630290
BB
47 console.log('Not yet implemented');
48 }
49 catch (err) {
50 console.error(err.message || err);
51 this._printUsage();
52 process.exit(1);
53 }
54 },
55
56 // Parses arguments and returns them if valid. otherwise Throws
57
58 _parseArguments() {
59
60 const parsedArguments = Minimist(process.argv.slice(2));
61
62 if (!this._areArgumentsValid(parsedArguments)) {
63 throw new Error(internals.strings.invalidArguments);
64 }
65
66 return parsedArguments;
67 },
68
69 // Checks if the arguments are valid, returns a boolean value.
70
71 _areArgumentsValid(parsedArguments) {
72
73 const argumentKeys = Object.keys(parsedArguments);
74
75 return argumentKeys.some((key) => internals.expectedKeys.indexOf(key) >= 0);
76 },
77
78 // Prints the usage to stderr
79
80 _printUsage() {
81
82 console.error('\nUsage:\n');
83 console.error('blog --add path/to/blog_post\t\t(creates new blog post)');
e54f8139
RBR
84 console.error('blog --update path/to/blog_post\t\t(updates latest blog post)');
85 console.error('blog --generate \t\t\t(generates the blog assets)');
86 console.error('blog --publish \t\t\t\t(publishes the blog)');
cf630290
BB
87 }
88};
89
90// Add the strings, added after declaration so they can consume the
91// internals object.
92
93internals.strings = {
94 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`
95};
96
97
98
99internals.main();