|
| 1 | +const SilentError = require('silent-error'); |
| 2 | +import denodeify = require('denodeify'); |
| 3 | + |
| 4 | +import { exec } from 'child_process'; |
| 5 | +import * as chalk from 'chalk'; |
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as fse from 'fs-extra'; |
| 8 | +import * as path from 'path'; |
| 9 | +import WebpackBuild from '../tasks/build-webpack'; |
| 10 | +import CreateGithubRepo from '../tasks/create-github-repo'; |
| 11 | +import { CliConfig } from '../models/config'; |
| 12 | +import { GithubPagesDeployOptions } from './github-pages-deploy'; |
| 13 | + |
| 14 | +const fsReadDir = <any>denodeify(fs.readdir); |
| 15 | +const fsCopy = <any>denodeify(fse.copy); |
| 16 | + |
| 17 | +export default function githubPagesDeployRun(options: GithubPagesDeployOptions, rawArgs: string[]) { |
| 18 | + const ui = this.ui; |
| 19 | + const root = this.project.root; |
| 20 | + const execOptions = { |
| 21 | + cwd: root |
| 22 | + }; |
| 23 | + |
| 24 | + if (options.environment === '') { |
| 25 | + if (options.target === 'development') { |
| 26 | + options.environment = 'dev'; |
| 27 | + } |
| 28 | + if (options.target === 'production') { |
| 29 | + options.environment = 'prod'; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + const projectName = this.project.pkg.name; |
| 34 | + |
| 35 | + const outDir = CliConfig.fromProject().config.apps[0].outDir; |
| 36 | + const indexFilename = CliConfig.fromProject().config.apps[0].index; |
| 37 | + |
| 38 | + let ghPagesBranch = 'gh-pages'; |
| 39 | + let destinationBranch = options.userPage ? 'master' : ghPagesBranch; |
| 40 | + let initialBranch: string; |
| 41 | + let branchErrMsg = ' You might also need to return to the initial branch manually.'; |
| 42 | + |
| 43 | + // declared here so that tests can stub exec |
| 44 | + const execPromise = <(cmd: string, options?: any) => Promise<string>>denodeify(exec); |
| 45 | + |
| 46 | + const buildTask = new WebpackBuild({ |
| 47 | + ui: this.ui, |
| 48 | + cliProject: this.project, |
| 49 | + target: options.target, |
| 50 | + environment: options.environment, |
| 51 | + outputPath: outDir |
| 52 | + }); |
| 53 | + |
| 54 | + /** |
| 55 | + * BaseHref tag setting logic: |
| 56 | + * First, use --base-href flag value if provided. |
| 57 | + * Else if --user-page is true, then keep baseHref default as declared in index.html. |
| 58 | + * Otherwise auto-replace with `/${projectName}/`. |
| 59 | + */ |
| 60 | + const baseHref = options.baseHref || (options.userPage ? null : `/${projectName}/`); |
| 61 | + |
| 62 | + const buildOptions = { |
| 63 | + target: options.target, |
| 64 | + environment: options.environment, |
| 65 | + outputPath: outDir, |
| 66 | + baseHref: baseHref, |
| 67 | + }; |
| 68 | + |
| 69 | + const createGithubRepoTask = new CreateGithubRepo({ |
| 70 | + ui: this.ui, |
| 71 | + project: this.project |
| 72 | + }); |
| 73 | + |
| 74 | + const createGithubRepoOptions = { |
| 75 | + projectName, |
| 76 | + ghUsername: options.ghUsername, |
| 77 | + ghToken: options.ghToken |
| 78 | + }; |
| 79 | + |
| 80 | + return checkForPendingChanges() |
| 81 | + .then(build) |
| 82 | + .then(saveStartingBranchName) |
| 83 | + .then(createGitHubRepoIfNeeded) |
| 84 | + .then(checkoutGhPages) |
| 85 | + .then(cleanGhPagesBranch) |
| 86 | + .then(copyFiles) |
| 87 | + .then(createNotFoundPage) |
| 88 | + .then(addAndCommit) |
| 89 | + .then(returnStartingBranch) |
| 90 | + .then(pushToGitRepo) |
| 91 | + .then(printProjectUrl) |
| 92 | + .catch(failGracefully); |
| 93 | + |
| 94 | + function checkForPendingChanges() { |
| 95 | + return execPromise('git status --porcelain') |
| 96 | + .then((stdout: string) => { |
| 97 | + if (/\w+/m.test(stdout)) { |
| 98 | + let msg = 'Uncommitted file changes found! Please commit all changes before deploying.'; |
| 99 | + return Promise.reject(new SilentError(msg)); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + function build() { |
| 105 | + if (options.skipBuild) { return Promise.resolve(); } |
| 106 | + return buildTask.run(buildOptions); |
| 107 | + } |
| 108 | + |
| 109 | + function saveStartingBranchName() { |
| 110 | + return execPromise('git rev-parse --abbrev-ref HEAD') |
| 111 | + .then((stdout: string) => initialBranch = stdout.replace(/\s/g, '')); |
| 112 | + } |
| 113 | + |
| 114 | + function createGitHubRepoIfNeeded() { |
| 115 | + return execPromise('git remote -v') |
| 116 | + .then(function (stdout) { |
| 117 | + if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) { |
| 118 | + return createGithubRepoTask.run(createGithubRepoOptions) |
| 119 | + .then(() => { |
| 120 | + // only push starting branch if it's not the destinationBranch |
| 121 | + // this happens commonly when using github user pages, since |
| 122 | + // they require the destination branch to be 'master' |
| 123 | + if (destinationBranch !== initialBranch) { |
| 124 | + execPromise(`git push -u origin ${initialBranch}`); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + function checkoutGhPages() { |
| 132 | + return execPromise(`git checkout ${ghPagesBranch}`) |
| 133 | + .catch(createGhPagesBranch); |
| 134 | + } |
| 135 | + |
| 136 | + function createGhPagesBranch() { |
| 137 | + return execPromise(`git checkout --orphan ${ghPagesBranch}`) |
| 138 | + .then(() => execPromise('git rm --cached -r .', execOptions)) |
| 139 | + .then(() => execPromise('git add .gitignore', execOptions)) |
| 140 | + .then(() => execPromise('git clean -f -d', execOptions)) |
| 141 | + .then(() => execPromise(`git commit -m \"initial ${ghPagesBranch} commit\"`)); |
| 142 | + } |
| 143 | + |
| 144 | + function cleanGhPagesBranch() { |
| 145 | + return execPromise('git ls-files') |
| 146 | + .then(function (stdout) { |
| 147 | + let files = ''; |
| 148 | + stdout.split(/\n/).forEach(function (f) { |
| 149 | + // skip .gitignore & 404.html |
| 150 | + if ((f != '') && (f != '.gitignore') && (f != '404.html')) { |
| 151 | + files = files.concat(`"${f}" `); |
| 152 | + } |
| 153 | + }); |
| 154 | + return execPromise(`git rm -r ${files}`) |
| 155 | + .catch(() => { |
| 156 | + // Ignoring errors when trying to erase files. |
| 157 | + }); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + function copyFiles() { |
| 162 | + return fsReadDir(outDir) |
| 163 | + .then((files: string[]) => Promise.all(files.map((file) => { |
| 164 | + if (file === '.gitignore') { |
| 165 | + // don't overwrite the .gitignore file |
| 166 | + return Promise.resolve(); |
| 167 | + } |
| 168 | + return fsCopy(path.join(outDir, file), path.join('.', file)); |
| 169 | + }))); |
| 170 | + } |
| 171 | + |
| 172 | + function createNotFoundPage() { |
| 173 | + const indexHtml = path.join(root, indexFilename); |
| 174 | + const notFoundPage = path.join(root, '404.html'); |
| 175 | + return fsCopy(indexHtml, notFoundPage); |
| 176 | + } |
| 177 | + |
| 178 | + function addAndCommit() { |
| 179 | + return execPromise('git add .', execOptions) |
| 180 | + .then(() => execPromise(`git commit -m "${options.message}"`)) |
| 181 | + .catch(() => { |
| 182 | + let msg = 'No changes found. Deployment skipped.'; |
| 183 | + return returnStartingBranch() |
| 184 | + .then(() => Promise.reject(new SilentError(msg))) |
| 185 | + .catch(() => Promise.reject(new SilentError(msg.concat(branchErrMsg)))); |
| 186 | + }); |
| 187 | + } |
| 188 | + |
| 189 | + function returnStartingBranch() { |
| 190 | + return execPromise(`git checkout ${initialBranch}`); |
| 191 | + } |
| 192 | + |
| 193 | + function pushToGitRepo() { |
| 194 | + return execPromise(`git push origin ${ghPagesBranch}:${destinationBranch}`) |
| 195 | + .catch((err) => returnStartingBranch() |
| 196 | + .catch(() => Promise.reject(err))); |
| 197 | + } |
| 198 | + |
| 199 | + function printProjectUrl() { |
| 200 | + return execPromise('git remote -v') |
| 201 | + .then((stdout) => { |
| 202 | + let match = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m); |
| 203 | + let userName = match[1].toLowerCase(); |
| 204 | + let url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`; |
| 205 | + ui.writeLine(chalk.green(`Deployed! Visit ${url}`)); |
| 206 | + ui.writeLine('Github pages might take a few minutes to show the deployed site.'); |
| 207 | + }); |
| 208 | + } |
| 209 | + |
| 210 | + function failGracefully(error: Error) { |
| 211 | + if (error && (/git clean/.test(error.message) || /Permission denied/.test(error.message))) { |
| 212 | + ui.writeLine(error.message); |
| 213 | + let msg = 'There was a permissions error during git file operations, ' + |
| 214 | + 'please close any open project files/folders and try again.'; |
| 215 | + return Promise.reject(new SilentError(msg.concat(branchErrMsg))); |
| 216 | + } else { |
| 217 | + return Promise.reject(error); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments