]>
git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
3c37f47a9b06046b77512ae177fde3719f732c2a
4 const Package
= require('../package.json');
5 const Config
= require('../config/config');
6 const Blog
= require('..');
7 const Minimist
= require('minimist');
10 blog: new Blog(Config
),
11 expectedKeys: ['add', 'generate', 'update', 'publish', 'version'],
13 // Application entry point. Reads arguments and calls the
14 // corresponding method from the blog lib
19 const parsedArguments
= this._parseArguments();
21 for (const argument
in parsedArguments
) {
22 if (parsedArguments
.hasOwnProperty(argument
)) {
24 const value
= parsedArguments
[argument
];
26 if (argument
=== 'version') {
27 console
.log(Package
.version
);
31 if (argument
=== 'add') {
32 await internals
.blog
.add(value
);
36 if (argument
=== 'update') {
37 await internals
.blog
.update(value
);
41 if (argument
=== 'generate') {
42 await internals
.blog
.generate();
46 if (argument
=== 'publish') {
47 await internals
.blog
.publish(value
);
53 console
.log('Not yet implemented');
56 console
.error(err
.message
|| err
);
62 // Parses arguments and returns them if valid. otherwise Throws
66 const parsedArguments
= Minimist(process
.argv
.slice(2));
68 if (!this._areArgumentsValid(parsedArguments
)) {
69 throw new Error(internals
.strings
.invalidArguments
);
72 return parsedArguments
;
75 // Checks if the arguments are valid, returns a boolean value.
77 _areArgumentsValid(parsedArguments
) {
79 const argumentKeys
= Object
.keys(parsedArguments
);
81 return argumentKeys
.some((key
) => internals
.expectedKeys
.indexOf(key
) >= 0);
84 // Prints the usage to stderr
88 console
.error('\nUsage:\n');
89 console
.error('blog --add path/to/blog_post\t\t(creates new blog post)');
90 console
.error('blog --update path/to/blog_post\t\t(updates latest blog post)');
91 console
.error('blog --generate \t\t\t(generates the blog assets)');
92 console
.error('blog --publish <bucket> \t\t\t\t(publishes the blog to an S3 bucket)');
93 console
.error('blog --version \t\t\t\t(print the version)');
97 // Add the strings, added after declaration so they can consume the
100 internals
.strings
= {
101 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`