]>
Commit | Line | Data |
---|---|---|
cf630290 BB |
1 | #!/usr/bin/env node |
2 | 'use strict'; | |
3 | ||
fac54389 | 4 | const Package = require('../package.json'); |
cf630290 BB |
5 | const Config = require('../config/config'); |
6 | const Blog = require('..'); | |
7 | const Minimist = require('minimist'); | |
8 | ||
9 | const internals = { | |
10 | blog: new Blog(Config), | |
c5cbbd38 RBR |
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'], | |
cf630290 BB |
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 | ||
fac54389 RBR |
36 | if (argument === 'version') { |
37 | console.log(Package.version); | |
38 | return; | |
39 | } | |
40 | ||
cf630290 BB |
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 | ||
e54f8139 RBR |
51 | if (argument === 'generate') { |
52 | await internals.blog.generate(); | |
53 | return; | |
54 | } | |
55 | ||
cf630290 | 56 | if (argument === 'publish') { |
39744467 | 57 | await internals.blog.publish(value); |
cf630290 BB |
58 | return; |
59 | } | |
65d379f5 RBR |
60 | |
61 | if (argument === 'publish-archive') { | |
62 | await internals.blog.publishArchive(value); | |
63 | return; | |
64 | } | |
c5cbbd38 RBR |
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 | } | |
cf630290 BB |
85 | } |
86 | } | |
d92ac8cc | 87 | |
cf630290 BB |
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'); | |
2acbdb03 RBR |
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)'); | |
e54f8139 | 126 | console.error('blog --generate \t\t\t(generates the blog assets)'); |
65d379f5 RBR |
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)'); | |
c5cbbd38 RBR |
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)'); | |
fac54389 | 133 | console.error('blog --version \t\t\t\t(print the version)'); |
cf630290 BB |
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(); |