]>
git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
7864f9ba96b6be61e63d8d423f04bcc9260de791
4 const Config
= require('../config/config');
5 const Blog
= require('..');
6 const Minimist
= require('minimist');
9 blog: new Blog(Config
),
10 expectedKeys: ['add', 'generate', '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
=== 'generate') {
36 await internals
.blog
.generate();
40 if (argument
=== 'publish') {
41 await internals
.blog
.publish(value
);
47 console
.log('Not yet implemented');
50 console
.error(err
.message
|| err
);
56 // Parses arguments and returns them if valid. otherwise Throws
60 const parsedArguments
= Minimist(process
.argv
.slice(2));
62 if (!this._areArgumentsValid(parsedArguments
)) {
63 throw new Error(internals
.strings
.invalidArguments
);
66 return parsedArguments
;
69 // Checks if the arguments are valid, returns a boolean value.
71 _areArgumentsValid(parsedArguments
) {
73 const argumentKeys
= Object
.keys(parsedArguments
);
75 return argumentKeys
.some((key
) => internals
.expectedKeys
.indexOf(key
) >= 0);
78 // Prints the usage to stderr
82 console
.error('\nUsage:\n');
83 console
.error('blog --add path/to/blog_post\t\t(creates new blog post)');
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)');
90 // Add the strings, added after declaration so they can consume the
94 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`