]>
Commit | Line | Data |
---|---|---|
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', '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 | } | |
52 | ||
53 | console.log('Not yet implemented'); | |
54 | } | |
55 | catch (err) { | |
56 | console.error(err.message || err); | |
57 | this._printUsage(); | |
58 | process.exit(1); | |
59 | } | |
60 | }, | |
61 | ||
62 | // Parses arguments and returns them if valid. otherwise Throws | |
63 | ||
64 | _parseArguments() { | |
65 | ||
66 | const parsedArguments = Minimist(process.argv.slice(2)); | |
67 | ||
68 | if (!this._areArgumentsValid(parsedArguments)) { | |
69 | throw new Error(internals.strings.invalidArguments); | |
70 | } | |
71 | ||
72 | return parsedArguments; | |
73 | }, | |
74 | ||
75 | // Checks if the arguments are valid, returns a boolean value. | |
76 | ||
77 | _areArgumentsValid(parsedArguments) { | |
78 | ||
79 | const argumentKeys = Object.keys(parsedArguments); | |
80 | ||
81 | return argumentKeys.some((key) => internals.expectedKeys.indexOf(key) >= 0); | |
82 | }, | |
83 | ||
84 | // Prints the usage to stderr | |
85 | ||
86 | _printUsage() { | |
87 | ||
88 | console.error('\nUsage:\n'); | |
89 | console.error('blog --add path/to/blog_post\t\t(creates new blog post)'); | |
90 | console.error('blog --update path/to/blog_post\t\t(updates latest blog post)'); | |
91 | console.error('blog --generate \t\t\t(generates the blog assets)'); | |
92 | console.error('blog --publish <bucket> \t\t\t\t(publishes the blog to an S3 bucket)'); | |
93 | console.error('blog --version \t\t\t\t(print the version)'); | |
94 | } | |
95 | }; | |
96 | ||
97 | // Add the strings, added after declaration so they can consume the | |
98 | // internals object. | |
99 | ||
100 | internals.strings = { | |
101 | invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}` | |
102 | }; | |
103 | ||
104 | ||
105 | ||
106 | internals.main(); |