aboutsummaryrefslogtreecommitdiff
path: root/bin/blog.js
blob: a9e2d1eb380d860aa55272d93f84f8351d9b677c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env node
'use strict';

const Package = require('../package.json');
const Config = require('../config/config');
const Blog = require('..');
const Minimist = require('minimist');

const internals = {
  blog: new Blog(Config),
  expectedKeys: [
    'add',
    'generate',
    'update',
    'publish',
    'publish-archive',
    'add-remote',
    'remove-remote',
    'sync-up',
    'sync-down',
    'version'],

  // 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 === 'version') {
            console.log(Package.version);
            return;
          }

          if (argument === 'add') {
            await internals.blog.add(value);
            return;
          }

          if (argument === 'update') {
            await internals.blog.update(value);
            return;
          }

          if (argument === 'generate') {
            await internals.blog.generate();
            return;
          }

          if (argument === 'publish') {
            await internals.blog.publish(value);
            return;
          }

          if (argument === 'publish-archive') {
            await internals.blog.publishArchive(value);
            return;
          }

          if (argument === 'add-remote') {
            await internals.blog.addRemote(value);
            return;
          }

          if (argument === 'remove-remote') {
            await internals.blog.removeRemote();
            return;
          }

          if (argument === 'sync-up') {
            await internals.blog.syncUp();
            return;
          }

          if (argument === 'sync-down') {
            await internals.blog.syncDown();
            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_post>\t\t(creates new blog post)');
    console.error('blog --update <path_to_post>\t\t(updates latest blog post)');
    console.error('blog --generate \t\t\t(generates the blog assets)');
    console.error('blog --publish <bucket> \t\t(publishes the blog to an S3 bucket)');
    console.error('blog --publish-archive <destination> \t(publishes the archive to a remote host)');
    console.error('blog --add-remote <git_url> \t\t(adds or updates a git remote to sync with)');
    console.error('blog --remove-remote \t\t\t(removes the git remote)');
    console.error('blog --sync-up \t\t\t\t(pushes to the git remote if configured)');
    console.error('blog --sync-down \t\t\t(pulls from the git remote if configured)');
    console.error('blog --version \t\t\t\t(print the version)');
  }
};

// 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();