#!/usr/bin/env node

let spawnSync = require('child_process').execSync;
let spawnOptions = { stdio: 'inherit' };

let spawnIt = (cmd) => {
  try {
    spawnSync(cmd, spawnOptions);
  }
  catch (e) {
    //Doesn't feel write to swallow the exceptions but there doesn't seem
    //to be a built in way to supress exceptions if git add and git push fail
  }
}

const deployBranch = 'gh-pages';
const stage1 = 'deploy-stage-1';
const stage2 = 'deploy-stage-2';

console.log('Getting ready for deployment...hold on!');

// generate meta data for posts
spawnIt(`$(npm bin)/jrp ${__dirname}/_posts`);
spawnIt(`git add -A . && git commit -m "chore: adds meta data for related posts and videos"`);

// perform jekyll build
spawnIt(`jekyll build`);

// cleanup temp branches
spawnIt(`git branch -D ${stage1}`);
spawnIt(`git branch -D ${stage2}`);

// prepare stage 1
spawnIt(`git checkout -b ${stage1}`);
spawnIt(`git add -f _site`);
spawnIt(`git commit -m "${stage1}"`);

// prepare stage 2
spawnIt(`git subtree split -P _site -b ${stage2}`);

// get contents from stage 2 and create a commit
spawnIt(`git checkout ${deployBranch}`);
spawnIt(`git checkout ${stage2} .`);
spawnIt(`git add -A`);
spawnIt(`git commit -m "rebuilt site"`);

spawnIt(`git push origin ${deployBranch}`);

console.log('Everything should be live at http://blog.thoughtram.io');
@@ -351,16 +351,48 @@
 %0A  %7D%0A%7D%0A%0A
+const defaultBranch = 'master';%0A
 const de
@@ -1297,16 +1297,59 @@
 ch%7D%60);%0A%0A
+spawnIt(%60git checkout $%7BdefaultBranch%7D%60);%0A%0A
 console.

switch back to master after deployment