]> git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
a9e2d1eb380d860aa55272d93f84f8351d9b677c
[rbdr/blog] / bin / blog.js
1 #!/usr/bin/env node
2 'use strict';
3
4 const Package = require('../package.json');
5 const Config = require('../config/config');
6 const Blog = require('..');
7 const Minimist = require('minimist');
8
9 const internals = {
10 blog: new Blog(Config),
11 expectedKeys: [
12 'add',
13 'generate',
14 'update',
15 'publish',
16 'publish-archive',
17 'add-remote',
18 'remove-remote',
19 'sync-up',
20 'sync-down',
21 'version'],
22
23 // Application entry point. Reads arguments and calls the
24 // corresponding method from the blog lib
25
26 async main() {
27
28 try {
29 const parsedArguments = this._parseArguments();
30
31 for (const argument in parsedArguments) {
32 if (parsedArguments.hasOwnProperty(argument)) {
33
34 const value = parsedArguments[argument];
35
36 if (argument === 'version') {
37 console.log(Package.version);
38 return;
39 }
40
41 if (argument === 'add') {
42 await internals.blog.add(value);
43 return;
44 }
45
46 if (argument === 'update') {
47 await internals.blog.update(value);
48 return;
49 }
50
51 if (argument === 'generate') {
52 await internals.blog.generate();
53 return;
54 }
55
56 if (argument === 'publish') {
57 await internals.blog.publish(value);
58 return;
59 }
60
61 if (argument === 'publish-archive') {
62 await internals.blog.publishArchive(value);
63 return;
64 }
65
66 if (argument === 'add-remote') {
67 await internals.blog.addRemote(value);
68 return;
69 }
70
71 if (argument === 'remove-remote') {
72 await internals.blog.removeRemote();
73 return;
74 }
75
76 if (argument === 'sync-up') {
77 await internals.blog.syncUp();
78 return;
79 }
80
81 if (argument === 'sync-down') {
82 await internals.blog.syncDown();
83 return;
84 }
85 }
86 }
87
88 console.log('Not yet implemented');
89 }
90 catch (err) {
91 console.error(err.message || err);
92 this._printUsage();
93 process.exit(1);
94 }
95 },
96
97 // Parses arguments and returns them if valid. otherwise Throws
98
99 _parseArguments() {
100
101 const parsedArguments = Minimist(process.argv.slice(2));
102
103 if (!this._areArgumentsValid(parsedArguments)) {
104 throw new Error(internals.strings.invalidArguments);
105 }
106
107 return parsedArguments;
108 },
109
110 // Checks if the arguments are valid, returns a boolean value.
111
112 _areArgumentsValid(parsedArguments) {
113
114 const argumentKeys = Object.keys(parsedArguments);
115
116 return argumentKeys.some((key) => internals.expectedKeys.indexOf(key) >= 0);
117 },
118
119 // Prints the usage to stderr
120
121 _printUsage() {
122
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)');
134 }
135 };
136
137 // Add the strings, added after declaration so they can consume the
138 // internals object.
139
140 internals.strings = {
141 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`
142 };
143
144
145
146 internals.main();