// LICENSE : MIT
"use strict";
// https://gist.github.com/domenic/ec8b0fc8ab45f39403dd
var shell = require('shelljs');
var ghParse = require('parse-github-url');
var Acho = require('acho');
var path = require("path");
var gitConfig = require("./git-config");
var Command = require("command-promise");
var acho = new Acho();
function remoteURLWithToken(token) {
    if (token == null) {
        return "https://github.com/" + getSlug() + ".git"
    }
    return "https://" + token + "@github.com/" + getSlug() + ".git"
}
function getSlug() {
    // Travis CI
    // http://docs.travis-ci.com/user/environment-variables/
    if (process.env.TRAVIS_REPO_SLUG) {
        return process.env.TRAVIS_REPO_SLUG;
    }
    try {
        var pkg = require(path.join(process.cwd(), "package.json"));
        var gh = ghParse(pkg.repository.url);
        if (gh.repopath) {
            return gh.repopath;
        }
    } catch (error) {
        console.error(error);
    }
    throw new Error("Not found git repository.url");
}
function isTRAVIS() {
    return process.env.CI === "true" || process.env.TRAVIS === "true";
}
function isNotDeploy() {
    // when pull-request
    if (process.env.TRAVIS_PULL_REQUEST && process.env.TRAVIS_PULL_REQUEST !== 'false') {
        return true;
    }

    return false;
}
function deploy(directory) {
    if (!isTRAVIS()) {
        throw new Error("gh-pages-deploy-sh is for Travis CI.");
    }
    if (isNotDeploy()) {
        console.log("Don't deploy if it is pull-request");
        return Promise.resolve();
    }
    var deployDir = directory || path.join(process.cwd(), ".");
    acho.info("deploy directory " + deployDir);
    var tmpDir = process.env.TMPDIR;
    return Command("cp -rf " + deployDir + " " + tmpDir)
        .then(function () {
            var workDir = path.join(tmpDir, path.basename(deployDir));
            acho.info("tmp directory " + workDir);
            return Command("cd " + workDir);
        }).then(function () {
            return Command("rm -f .gitignore");
        }).then(function () {
            return gitConfig.resolveGitName().then(function () {
                return gitConfig.resolveGitEmail()
            })
        }).then(function () {
            acho.info("git checkout -B gh-pages");
            return Command("git checkout -B gh-pages");
        }).then(function () {
            acho.info("git add && git commit");
            return Command("git add .").then(function () {
                return Command("git diff --quiet --exit-code --cached || git commit -m 'Update " + new Date().toISOString() + "'");
            });
        }).then(function () {
            acho.info("git push to gh-pages");
            var repositoryURL = remoteURLWithToken(process.env.GH_TOKEN);
            return Command("git push --force --quiet '" + repositoryURL + "' master:gh-pages > /dev/null 2>&1")
        }).then(function () {
            return Command("git checkout -");
        })
}

module.exports = deploy;

@@ -1673,28 +1673,129 @@
 r = 
-process.env.TMPDIR;%0A
+null;%0A    return Command(%22dirname $(mktemp 2%3E/dev/null%7C%7Cmktemp -t tmp)%22).then(function (dir) %7B%0A        tmpDir = dir;%0A    
     
@@ -1851,20 +1851,18 @@
 ir)%0A    
-    
+%7D)
 .then(fu
@@ -1865,36 +1865,32 @@
 n(function () %7B%0A
-    
         var work
@@ -1940,36 +1940,32 @@
 yDir));%0A        
-    
 acho.info(%22tmp d
@@ -1983,28 +1983,24 @@
 + workDir);%0A
-    
         retu
@@ -2024,28 +2024,24 @@
 + workDir);%0A
-    
     %7D).then(
@@ -2054,36 +2054,32 @@
 on () %7B%0A        
-   
- 
 return Command(%22
@@ -2094,28 +2094,24 @@
 itignore%22);%0A
-    
     %7D).then(
@@ -2124,36 +2124,32 @@
 on () %7B%0A        
-  
-  
 return gitConfig
@@ -2189,36 +2189,32 @@
 ) %7B%0A            
-    
 return gitConfig
@@ -2244,19 +2244,11 @@
     
-    %7D)%0A    
+%7D)%0A
     
@@ -2269,36 +2269,32 @@
 on () %7B%0A        
-    
 acho.info(%22git c
@@ -2316,36 +2316,32 @@
 ages%22);%0A        
-   
- 
 return Command(%22
@@ -2364,28 +2364,24 @@
 gh-pages%22);%0A
-    
     %7D).then(
@@ -2394,36 +2394,32 @@
 on () %7B%0A        
-  
-  
 acho.info(%22git a
@@ -2434,28 +2434,24 @@
 t commit%22);%0A
-    
         retu
@@ -2485,36 +2485,32 @@
 n(function () %7B%0A
-    
             retu
@@ -2617,28 +2617,24 @@
 g() + %22'%22);%0A
-    
         %7D);%0A
@@ -2633,20 +2633,16 @@
     %7D);%0A
-    
     %7D).t
@@ -2663,28 +2663,24 @@
 ) %7B%0A        
-   
- 
 acho.info(%22g
@@ -2698,28 +2698,24 @@
 gh-pages%22);%0A
-    
         var 
@@ -2772,36 +2772,32 @@
 TOKEN);%0A        
-  
-  
 return Command(%22
@@ -2880,20 +2880,16 @@
  2%3E&1%22)%0A
-    
     %7D).t
@@ -2898,36 +2898,32 @@
 n(function () %7B%0A
-    
         return C
@@ -2944,28 +2944,24 @@
 eckout -%22);%0A
-    
     %7D)%0A%7D%0A%0Amo

use mktemp