]>
git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
8f0ab70e3ad192c54a42a2b639971c5ef8cc2e73
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', 'publish-archive', '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
);
51 if (argument
=== 'publish-archive') {
52 await internals
.blog
.publishArchive(value
);
58 console
.log('Not yet implemented');
61 console
.error(err
.message
|| err
);
67 // Parses arguments and returns them if valid. otherwise Throws
71 const parsedArguments
= Minimist(process
.argv
.slice(2));
73 if (!this._areArgumentsValid(parsedArguments
)) {
74 throw new Error(internals
.strings
.invalidArguments
);
77 return parsedArguments
;
80 // Checks if the arguments are valid, returns a boolean value.
82 _areArgumentsValid(parsedArguments
) {
84 const argumentKeys
= Object
.keys(parsedArguments
);
86 return argumentKeys
.some((key
) => internals
.expectedKeys
.indexOf(key
) >= 0);
89 // Prints the usage to stderr
93 console
.error('\nUsage:\n');
94 console
.error('blog --add path/to/blog_post\t\t(creates new blog post)');
95 console
.error('blog --update path/to/blog_post\t\t(updates latest blog post)');
96 console
.error('blog --generate \t\t\t(generates the blog assets)');
97 console
.error('blog --publish <bucket> \t\t(publishes the blog to an S3 bucket)');
98 console
.error('blog --publish-archive <destination> \t(publishes the archive to a remote host)');
99 console
.error('blog --version \t\t\t\t(print the version)');
103 // Add the strings, added after declaration so they can consume the
106 internals
.strings
= {
107 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`