]>
git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
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
),
23 // Application entry point. Reads arguments and calls the
24 // corresponding method from the blog lib
29 const parsedArguments
= this._parseArguments();
31 for (const argument
in parsedArguments
) {
32 if (parsedArguments
.hasOwnProperty(argument
)) {
34 const value
= parsedArguments
[argument
];
36 if (argument
=== 'version') {
37 console
.log(Package
.version
);
41 if (argument
=== 'add') {
42 await internals
.blog
.add(value
);
46 if (argument
=== 'update') {
47 await internals
.blog
.update(value
);
51 if (argument
=== 'generate') {
52 await internals
.blog
.generate();
56 if (argument
=== 'publish') {
57 await internals
.blog
.publish(value
);
61 if (argument
=== 'publish-archive') {
62 await internals
.blog
.publishArchive(value
);
66 if (argument
=== 'add-remote') {
67 await internals
.blog
.addRemote(value
);
71 if (argument
=== 'remove-remote') {
72 await internals
.blog
.removeRemote();
76 if (argument
=== 'sync-up') {
77 await internals
.blog
.syncUp();
81 if (argument
=== 'sync-down') {
82 await internals
.blog
.syncDown();
88 console
.log('Not yet implemented');
91 console
.error(err
.message
|| err
);
97 // Parses arguments and returns them if valid. otherwise Throws
101 const parsedArguments
= Minimist(process
.argv
.slice(2));
103 if (!this._areArgumentsValid(parsedArguments
)) {
104 throw new Error(internals
.strings
.invalidArguments
);
107 return parsedArguments
;
110 // Checks if the arguments are valid, returns a boolean value.
112 _areArgumentsValid(parsedArguments
) {
114 const argumentKeys
= Object
.keys(parsedArguments
);
116 return argumentKeys
.some((key
) => internals
.expectedKeys
.indexOf(key
) >= 0);
119 // Prints the usage to stderr
123 console
.error('\nUsage:\n');
124 console
.error('blog --add <path_to_post>\t\t(creates new blog post)');
125 console
.error('blog --update <path_to_post>\t\t(updates latest blog post)');
126 console
.error('blog --generate \t\t\t(generates the blog assets)');
127 console
.error('blog --publish <bucket> \t\t(publishes the blog to an S3 bucket)');
128 console
.error('blog --publish-archive <destination> \t(publishes the archive to a remote host)');
129 console
.error('blog --add-remote <git_url> \t\t(adds or updates a git remote to sync with)');
130 console
.error('blog --remove-remote \t\t\t(removes the git remote)');
131 console
.error('blog --sync-up \t\t\t\t(pushes to the git remote if configured)');
132 console
.error('blog --sync-down \t\t\t(pulls from the git remote if configured)');
133 console
.error('blog --version \t\t\t\t(print the version)');
137 // Add the strings, added after declaration so they can consume the
140 internals
.strings
= {
141 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`