]> git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
Add logic skeleton for rust version
[rbdr/blog] / bin / blog.js
1 #!/usr/bin/env node
2 import Config from '../config/config.js';
3 import Blog from '../lib/blog.js';
4 import Minimist from 'minimist';
5
6 const internals = {
7 blog: new Blog(Config),
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'],
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
33 if (argument === 'version') {
34 console.log('6.0.0');
35 return;
36 }
37
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
48 if (argument === 'generate') {
49 await internals.blog.generate();
50 return;
51 }
52
53 if (argument === 'publish') {
54 await internals.blog.publish(value);
55 return;
56 }
57
58 if (argument === 'publish-archive') {
59 await internals.blog.publishArchive(value);
60 return;
61 }
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 }
82 }
83 }
84
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');
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)');
131 }
132 };
133
134 // Add the strings, added after declaration so they can consume the
135 // internals object.
136
137 internals.strings = {
138 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`
139 };
140
141
142
143 internals.main();