]> git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
Remove one more v
[rbdr/blog] / bin / blog.js
1 #!/usr/bin/env node
2 'use strict';
3
4 const Config = require('../config/config');
5 const Blog = require('..');
6 const Minimist = require('minimist');
7
8 const internals = {
9 blog: new Blog(Config),
10 expectedKeys: ['add', 'update', 'publish'],
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
35 if (argument === 'publish') {
36 await internals.blog.publish(value);
37 return;
38 }
39 }
40 }
41 console.log('Not yet implemented');
42 }
43 catch (err) {
44 console.error(err.message || err);
45 this._printUsage();
46 process.exit(1);
47 }
48 },
49
50 // Parses arguments and returns them if valid. otherwise Throws
51
52 _parseArguments() {
53
54 const parsedArguments = Minimist(process.argv.slice(2));
55
56 if (!this._areArgumentsValid(parsedArguments)) {
57 throw new Error(internals.strings.invalidArguments);
58 }
59
60 return parsedArguments;
61 },
62
63 // Checks if the arguments are valid, returns a boolean value.
64
65 _areArgumentsValid(parsedArguments) {
66
67 const argumentKeys = Object.keys(parsedArguments);
68
69 return argumentKeys.some((key) => internals.expectedKeys.indexOf(key) >= 0);
70 },
71
72 // Prints the usage to stderr
73
74 _printUsage() {
75
76 console.error('\nUsage:\n');
77 console.error('blog --add path/to/blog_post\t\t(creates new blog post)');
78 console.error('blog --update path/to/blog_post\t(updates latest blog post)');
79 console.error('blog --publish \t\t\t(publishes the blog)');
80 }
81 };
82
83 // Add the strings, added after declaration so they can consume the
84 // internals object.
85
86 internals.strings = {
87 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`
88 };
89
90
91
92 internals.main();