]>
git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
2 import Config
from '../config/config.js';
3 import Blog
from '../lib/blog.js';
4 import Minimist
from 'minimist';
7 blog: new Blog(Config
),
20 // Application entry point. Reads arguments and calls the
21 // corresponding method from the blog lib
26 const parsedArguments
= this._parseArguments();
28 for (const argument
in parsedArguments
) {
29 if (parsedArguments
.hasOwnProperty(argument
)) {
31 const value
= parsedArguments
[argument
];
33 if (argument
=== 'version') {
38 if (argument
=== 'add') {
39 await internals
.blog
.add(value
);
43 if (argument
=== 'update') {
44 await internals
.blog
.update(value
);
48 if (argument
=== 'generate') {
49 await internals
.blog
.generate();
53 if (argument
=== 'publish') {
54 await internals
.blog
.publish(value
);
58 if (argument
=== 'publish-archive') {
59 await internals
.blog
.publishArchive(value
);
63 if (argument
=== 'add-remote') {
64 await internals
.blog
.addRemote(value
);
68 if (argument
=== 'remove-remote') {
69 await internals
.blog
.removeRemote();
73 if (argument
=== 'sync-up') {
74 await internals
.blog
.syncUp();
78 if (argument
=== 'sync-down') {
79 await internals
.blog
.syncDown();
85 console
.log('Not yet implemented');
88 console
.error(err
.message
|| err
);
94 // Parses arguments and returns them if valid. otherwise Throws
98 const parsedArguments
= Minimist(process
.argv
.slice(2));
100 if (!this._areArgumentsValid(parsedArguments
)) {
101 throw new Error(internals
.strings
.invalidArguments
);
104 return parsedArguments
;
107 // Checks if the arguments are valid, returns a boolean value.
109 _areArgumentsValid(parsedArguments
) {
111 const argumentKeys
= Object
.keys(parsedArguments
);
113 return argumentKeys
.some((key
) => internals
.expectedKeys
.indexOf(key
) >= 0);
116 // Prints the usage to stderr
120 console
.error('\nUsage:\n');
121 console
.error('blog --add <path_to_post>\t\t(creates new blog post)');
122 console
.error('blog --update <path_to_post>\t\t(updates latest blog post)');
123 console
.error('blog --generate \t\t\t(generates the blog assets)');
124 console
.error('blog --publish <bucket> \t\t(publishes the blog to an S3 bucket)');
125 console
.error('blog --publish-archive <destination> \t(publishes the archive to a remote host)');
126 console
.error('blog --add-remote <git_url> \t\t(adds or updates a git remote to sync with)');
127 console
.error('blog --remove-remote \t\t\t(removes the git remote)');
128 console
.error('blog --sync-up \t\t\t\t(pushes to the git remote if configured)');
129 console
.error('blog --sync-down \t\t\t(pulls from the git remote if configured)');
130 console
.error('blog --version \t\t\t\t(print the version)');
134 // Add the strings, added after declaration so they can consume the
137 internals
.strings
= {
138 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`