/**
 * React Starter Kit (https://www.reactstarterkit.com/)
 *
 * Copyright © 2014-present Kriasoft, LLC. All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */

import path from 'path';
import fetch from 'node-fetch';
import { spawn } from './lib/cp';
import { makeDir, moveDir, cleanDir } from './lib/fs';
import run from './run';

// GitHub Pages
const remote = {
  name: 'github',
  url: 'https://github.com/<user>/<repo>.git',
  branch: 'gh-pages',
  website: 'https://<user>.github.io/<repo>/',
  static: true,
};

// Heroku
// const remote = {
//   name: 'heroku',
//   url: 'https://git.heroku.com/<app>.git',
//   branch: 'master',
//   website: 'https://<app>.herokuapp.com',
// };

// Azure Web Apps
// const remote = {
//   name: 'azure',
//   url: 'https://<user>@<app>.scm.azurewebsites.net:443/<app>.git',
//   branch: 'master',
//   website: `http://<app>.azurewebsites.net`,
// };

const options = {
  cwd: path.resolve(__dirname, '../build'),
  stdio: ['ignore', 'inherit', 'inherit'],
};

/**
 * Deploy the contents of the `/build` folder to a remote server via Git.
 */
async function deploy() {
  // Initialize a new repository
  await makeDir('build');
  await spawn('git', ['init', '--quiet'], options);

  // Changing a remote's URL
  let isRemoteExists = false;
  try {
    await spawn('git', ['config', '--get', `remote.${remote.name}.url`], options);
    isRemoteExists = true;
  } catch (error) {
    /* skip */
  }
  await spawn('git', ['remote', isRemoteExists ? 'set-url' : 'add', remote.name, remote.url], options);

  // Fetch the remote repository if it exists
  let isRefExists = false;
  try {
    await spawn('git', ['ls-remote', '--quiet', '--exit-code', remote.url, remote.branch], options);
    isRefExists = true;
  } catch (error) {
    await spawn('git', ['update-ref', '-d', 'HEAD'], options);
  }
  if (isRefExists) {
    await spawn('git', ['fetch', remote.name], options);
    await spawn('git', ['reset', `${remote.name}/${remote.branch}`, '--hard'], options);
    await spawn('git', ['clean', '--force'], options);
  }

  // Build the project in RELEASE mode which
  // generates optimized and minimized bundles
  process.argv.push('--release');
  if (remote.static) process.argv.push('--static');
  await run(require('./build').default); // eslint-disable-line global-require
  if (process.argv.includes('--static')) {
    await cleanDir('build/*', {
      nosort: true,
      dot: true,
      ignore: ['build/.git', 'build/public'],
    });
    await moveDir('build/public', 'build');
  }

  // Push the contents of the build folder to the remote server via Git
  await spawn('git', ['add', '.', '--all'], options);
  try {
    await spawn('git', ['diff', '--cached', '--exit-code', '--quiet'], options);
  } catch (error) {
    await spawn('git', ['commit', '--message', `Update ${new Date().toISOString()}`], options);
  }
  await spawn('git', ['push', remote.name, `master:${remote.branch}`, '--set-upstream'], options);

  // Check if the site was successfully deployed
  const response = await fetch(remote.website);
  console.info(`${remote.website} => ${response.status} ${response.statusText}`);
}

export default deploy;

@@ -446,16 +446,19 @@
 b Pages%0A
+// 
 const re
@@ -466,18 +466,21 @@
 ote = %7B%0A
+//
   
+ 
 name: 'g
@@ -487,16 +487,19 @@
 ithub',%0A
+// 
   url: '
@@ -537,16 +537,19 @@
 %3E.git',%0A
+// 
   branch
@@ -562,16 +562,19 @@
 pages',%0A
+// 
   websit
@@ -612,16 +612,19 @@
 epo%3E/',%0A
+// 
   static
@@ -631,16 +631,19 @@
 : true,%0A
+// 
 %7D;%0A%0A// H
@@ -648,19 +648,16 @@
  Heroku%0A
-// 
 const re
@@ -657,35 +657,32 @@
 onst remote = %7B%0A
-// 
   name: 'heroku'
@@ -683,19 +683,16 @@
 eroku',%0A
-// 
   url: '
@@ -714,31 +714,36 @@
 oku.com/
-%3Capp%3E
+cineindustria
 .git',%0A
-// 
   branch
@@ -746,35 +746,32 @@
 anch: 'master',%0A
-// 
   website: 'http
@@ -774,37 +774,31 @@
 http
-s
 ://
-%3Capp%3E.herokuapp
+cineindustria
 .com',%0A
-// 
 %7D;%0A%0A

Update Heroku repo