diff options
| author | Ben Beltran <ben@nsovocal.com> | 2017-07-03 00:55:32 -0500 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2017-07-03 00:55:32 -0500 |
| commit | 5e265f9d81bcce30949e88850892ea5dacec7386 (patch) | |
| tree | 056dfe345174027335cc033359dfbe14a8d53250 /bin | |
| parent | 4ae55e0673a61c50f1f7f378f6461d0388f66bd5 (diff) | |
| parent | be33bb82f6276b05a3861df8a7c5138ac320a00b (diff) | |
Merge branch 'release/1.0.0'
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/blog.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/bin/blog.js b/bin/blog.js new file mode 100755 index 0000000..5d27d4a --- /dev/null +++ b/bin/blog.js @@ -0,0 +1,92 @@ +#!/usr/bin/env node +'use strict'; + +const Config = require('../config/config'); +const Blog = require('..'); +const Minimist = require('minimist'); + +const internals = { + blog: new Blog(Config), + expectedKeys: ['add', 'update', 'publish'], + + // Application entry point. Reads arguments and calls the + // corresponding method from the blog lib + + async main() { + + try { + const parsedArguments = this._parseArguments(); + + for (const argument in parsedArguments) { + if (parsedArguments.hasOwnProperty(argument)) { + + const value = parsedArguments[argument]; + + if (argument === 'add') { + await internals.blog.add(value); + return; + } + + if (argument === 'update') { + await internals.blog.update(value); + return; + } + + if (argument === 'publish') { + await internals.blog.publish(value); + return; + } + } + } + console.log('Not yet implemented'); + } + catch (err) { + console.error(err.message || err); + this._printUsage(); + process.exit(1); + } + }, + + // Parses arguments and returns them if valid. otherwise Throws + + _parseArguments() { + + const parsedArguments = Minimist(process.argv.slice(2)); + + if (!this._areArgumentsValid(parsedArguments)) { + throw new Error(internals.strings.invalidArguments); + } + + return parsedArguments; + }, + + // Checks if the arguments are valid, returns a boolean value. + + _areArgumentsValid(parsedArguments) { + + const argumentKeys = Object.keys(parsedArguments); + + return argumentKeys.some((key) => internals.expectedKeys.indexOf(key) >= 0); + }, + + // Prints the usage to stderr + + _printUsage() { + + console.error('\nUsage:\n'); + console.error('blog --add path/to/blog_post\t\t(creates new blog post)'); + console.error('blog --update path/to/blog_post\t(updates latest blog post)'); + console.error('blog --publish \t\t\t(publishes the blog)'); + } +}; + +// Add the strings, added after declaration so they can consume the +// internals object. + +internals.strings = { + invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}` +}; + + + +internals.main(); |