]>
git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
4 const Config
= require('../config/config');
5 const Blog
= require('..');
6 const Minimist
= require('minimist');
9 blog: new Blog(Config
),
10 expectedKeys: ['add', 'update', 'publish'],
12 // Application entry point. Reads arguments and calls the
13 // corresponding method from the blog lib
18 const parsedArguments
= this._parseArguments();
20 for (const argument
in parsedArguments
) {
21 if (parsedArguments
.hasOwnProperty(argument
)) {
23 const value
= parsedArguments
[argument
];
25 if (argument
=== 'add') {
26 await internals
.blog
.add(value
);
30 if (argument
=== 'update') {
31 await internals
.blog
.update(value
);
35 if (argument
=== 'publish') {
36 await internals
.blog
.publish(value
);
42 console
.log('Not yet implemented');
45 console
.error(err
.message
|| err
);
51 // Parses arguments and returns them if valid. otherwise Throws
55 const parsedArguments
= Minimist(process
.argv
.slice(2));
57 if (!this._areArgumentsValid(parsedArguments
)) {
58 throw new Error(internals
.strings
.invalidArguments
);
61 return parsedArguments
;
64 // Checks if the arguments are valid, returns a boolean value.
66 _areArgumentsValid(parsedArguments
) {
68 const argumentKeys
= Object
.keys(parsedArguments
);
70 return argumentKeys
.some((key
) => internals
.expectedKeys
.indexOf(key
) >= 0);
73 // Prints the usage to stderr
77 console
.error('\nUsage:\n');
78 console
.error('blog --add path/to/blog_post\t\t(creates new blog post)');
79 console
.error('blog --update path/to/blog_post\t(updates latest blog post)');
80 console
.error('blog --publish \t\t\t(publishes the blog)');
84 // Add the strings, added after declaration so they can consume the
88 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`