]> git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
8f0ab70e3ad192c54a42a2b639971c5ef8cc2e73
[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: ['add', 'generate', 'update', 'publish', 'publish-archive', 'version'],
12
13 // Application entry point. Reads arguments and calls the
14 // corresponding method from the blog lib
15
16 async main() {
17
18 try {
19 const parsedArguments = this._parseArguments();
20
21 for (const argument in parsedArguments) {
22 if (parsedArguments.hasOwnProperty(argument)) {
23
24 const value = parsedArguments[argument];
25
26 if (argument === 'version') {
27 console.log(Package.version);
28 return;
29 }
30
31 if (argument === 'add') {
32 await internals.blog.add(value);
33 return;
34 }
35
36 if (argument === 'update') {
37 await internals.blog.update(value);
38 return;
39 }
40
41 if (argument === 'generate') {
42 await internals.blog.generate();
43 return;
44 }
45
46 if (argument === 'publish') {
47 await internals.blog.publish(value);
48 return;
49 }
50
51 if (argument === 'publish-archive') {
52 await internals.blog.publishArchive(value);
53 return;
54 }
55 }
56 }
57
58 console.log('Not yet implemented');
59 }
60 catch (err) {
61 console.error(err.message || err);
62 this._printUsage();
63 process.exit(1);
64 }
65 },
66
67 // Parses arguments and returns them if valid. otherwise Throws
68
69 _parseArguments() {
70
71 const parsedArguments = Minimist(process.argv.slice(2));
72
73 if (!this._areArgumentsValid(parsedArguments)) {
74 throw new Error(internals.strings.invalidArguments);
75 }
76
77 return parsedArguments;
78 },
79
80 // Checks if the arguments are valid, returns a boolean value.
81
82 _areArgumentsValid(parsedArguments) {
83
84 const argumentKeys = Object.keys(parsedArguments);
85
86 return argumentKeys.some((key) => internals.expectedKeys.indexOf(key) >= 0);
87 },
88
89 // Prints the usage to stderr
90
91 _printUsage() {
92
93 console.error('\nUsage:\n');
94 console.error('blog --add path/to/blog_post\t\t(creates new blog post)');
95 console.error('blog --update path/to/blog_post\t\t(updates latest blog post)');
96 console.error('blog --generate \t\t\t(generates the blog assets)');
97 console.error('blog --publish <bucket> \t\t(publishes the blog to an S3 bucket)');
98 console.error('blog --publish-archive <destination> \t(publishes the archive to a remote host)');
99 console.error('blog --version \t\t\t\t(print the version)');
100 }
101 };
102
103 // Add the strings, added after declaration so they can consume the
104 // internals object.
105
106 internals.strings = {
107 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`
108 };
109
110
111
112 internals.main();