#!/usr/bin/env node /** * Copyright (c) 2017-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ const shell = require("shelljs"); if (!shell.which("git")) { shell.echo("Sorry, this script requires git"); shell.exit(1); } const siteConfig = require(process.cwd() + "/siteConfig.js"); const GIT_USER = process.env.GIT_USER; const CURRENT_BRANCH = process.env.CIRCLE_BRANCH || shell.exec("git rev-parse --abbrev-ref HEAD"); const ORGANIZATION_NAME = siteConfig.organizationName || process.env.ORGANIZATION_NAME || process.env.CIRCLE_PROJECT_USERNAME; const PROJECT_NAME = siteConfig.projectName || process.env.PROJECT_NAME || process.env.CIRCLE_PROJECT_REPONAME; const IS_PULL_REQUEST = process.env.CI_PULL_REQUEST || process.env.CIRCLE_PULL_REQUEST; const USE_SSH = process.env.USE_SSH; if (!ORGANIZATION_NAME) { shell.echo( "Missing project organization name. Did you forget to define 'organizationName' in siteConfig.js? You may also export it via the ORGANIZATION_NAME environment variable." ); shell.exit(0); } if (!PROJECT_NAME) { shell.echo( "Missing project name. Did you forget to define 'projectName' in siteConfig.js? You may also export it via the PROJECT_NAME environment variable." ); shell.exit(0); } let remoteBranch; if (USE_SSH === "true") { remoteBranch = `git@github.com:${ORGANIZATION_NAME}/${PROJECT_NAME}.git`; } else { remoteBranch = `https://${GIT_USER}@github.com/${ORGANIZATION_NAME}/${PROJECT_NAME}.git`; } // build static html files, then push to gh-pages branch of specified repo if (IS_PULL_REQUEST) { shell.echo("Skipping deploy on a pull request"); shell.exit(0); } if (CURRENT_BRANCH === "gh-pages") { shell.echo("Cannot deploy from a gh-pages branch. Only to it"); shell.exit(1); } if (shell.exec(`node ${__dirname}/build-files.js`).code) { shell.echo("Error: generating html failed"); shell.exit(1); } shell.cd(process.cwd()); shell.cd("build"); if ( shell.exec(`git clone ${remoteBranch} ${PROJECT_NAME}-gh-pages`).code !== 0 ) { shell.echo("Error: git clone failed"); shell.exit(1); } shell.cd(`${PROJECT_NAME}-gh-pages`); if (shell.exec("git checkout origin/gh-pages").code !== 0) { if (shell.exec("git checkout --orphan gh-pages").code !== 0) { shell.echo("Error: Git checkout gh-pages failed"); shell.exit(1); } } else { if ( shell.exec("git checkout -b gh-pages").code + shell.exec("git branch --set-upstream-to=origin/gh-pages").code !== 0 ) { shell.echo("Error: Git checkout gh-pages failed"); shell.exit(1); } } shell.exec("git rm -rf ."); shell.cd("../.."); shell.cp("-R", `build/${PROJECT_NAME}/*`, `build/${PROJECT_NAME}-gh-pages/`); shell.cd(`build/${PROJECT_NAME}-gh-pages`); const currentCommit = shell.exec("git rev-parse HEAD").stdout.trim(); shell.exec("git add --all"); shell.exec( `git commit -m "Deploy website" -m "Deploy website version based on ${currentCommit}"` ); if (shell.exec("git push origin gh-pages").code !== 0) { shell.echo("Error: Git push failed"); shell.exit(1); } else { shell.echo( `Website is live at: https://${ORGANIZATION_NAME}.github.io/${PROJECT_NAME}/` ); shell.exit(0); }