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