|
| 1 | +import * as Command from 'ember-cli/lib/models/command'; |
| 2 | +import * as SilentError from 'silent-error'; |
| 3 | +import { exec } from 'child_process'; |
| 4 | +import * as Promise from 'ember-cli/lib/ext/promise'; |
| 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 * as BuildTask from 'ember-cli/lib/tasks/build'; |
| 10 | +import * as win from 'ember-cli/lib/utilities/windows-admin'; |
| 11 | +import * as CreateGithubRepo from '../tasks/create-github-repo'; |
| 12 | + |
| 13 | +const fsReadFile = Promise.denodeify(fs.readFile); |
| 14 | +const fsWriteFile = Promise.denodeify(fs.writeFile); |
| 15 | +const fsReadDir = Promise.denodeify(fs.readdir); |
| 16 | +const fsCopy = Promise.denodeify(fse.copy); |
| 17 | + |
| 18 | +module.exports = Command.extend({ |
| 19 | + name: 'github-pages:deploy', |
| 20 | + aliases: ['gh-pages:deploy'], |
| 21 | + description: 'Build the test app for production, commit it into a git branch, setup GitHub repo and push to it', |
| 22 | + works: 'insideProject', |
| 23 | + |
| 24 | + availableOptions: [ |
| 25 | + { |
| 26 | + name: 'message', |
| 27 | + type: String, |
| 28 | + default: 'new gh-pages version', |
| 29 | + description: 'The commit message to include with the build, must be wrapped in quotes.' |
| 30 | + }, { |
| 31 | + name: 'environment', |
| 32 | + type: String, |
| 33 | + default: 'production', |
| 34 | + description: 'The Angular environment to create a build for' |
| 35 | + }, { |
| 36 | + name: 'branch', |
| 37 | + type: String, |
| 38 | + default: 'gh-pages', |
| 39 | + description: 'The git branch to push your pages to' |
| 40 | + }, { |
| 41 | + name: 'skip-build', |
| 42 | + type: Boolean, |
| 43 | + default: false, |
| 44 | + description: 'Skip building the project before deploying' |
| 45 | + }, { |
| 46 | + name: 'gh-token', |
| 47 | + type: String, |
| 48 | + default: '', |
| 49 | + description: 'Github token' |
| 50 | + }, { |
| 51 | + name: 'gh-username', |
| 52 | + type: String, |
| 53 | + default: '', |
| 54 | + description: 'Github username' |
| 55 | + }], |
| 56 | + |
| 57 | + run: function(options, rawArgs) { |
| 58 | + var ui = this.ui; |
| 59 | + var root = this.project.root; |
| 60 | + var execOptions = { |
| 61 | + cwd: root |
| 62 | + }; |
| 63 | + var projectName = this.project.pkg.name; |
| 64 | + |
| 65 | + let initialBranch; |
| 66 | + |
| 67 | + // declared here so that tests can stub exec |
| 68 | + const execPromise = Promise.denodeify(exec); |
| 69 | + |
| 70 | + var buildTask = new BuildTask({ |
| 71 | + ui: this.ui, |
| 72 | + analytics: this.analytics, |
| 73 | + project: this.project |
| 74 | + }); |
| 75 | + |
| 76 | + var buildOptions = { |
| 77 | + environment: options.environment, |
| 78 | + outputPath: 'dist/' |
| 79 | + }; |
| 80 | + |
| 81 | + var createGithubRepoTask = new CreateGithubRepo({ |
| 82 | + ui: this.ui, |
| 83 | + analytics: this.analytics, |
| 84 | + project: this.project |
| 85 | + }); |
| 86 | + |
| 87 | + var createGithubRepoOptions = { |
| 88 | + projectName, |
| 89 | + ghUsername: options.ghUsername, |
| 90 | + ghToken: options.ghToken |
| 91 | + }; |
| 92 | + |
| 93 | + return checkForPendingChanges() |
| 94 | + .then(build) |
| 95 | + .then(saveStartingBranchName) |
| 96 | + .then(createGitHubRepoIfNeeded) |
| 97 | + .then(checkoutGhPages) |
| 98 | + .then(copyFiles) |
| 99 | + .then(updateBaseHref) |
| 100 | + .then(addAndCommit) |
| 101 | + .then(returnStartingBranch) |
| 102 | + .then(pushToGitRepo) |
| 103 | + .then(printProjectUrl); |
| 104 | + |
| 105 | + function checkForPendingChanges() { |
| 106 | + return execPromise('git status --porcelain') |
| 107 | + .then(stdout => { |
| 108 | + if (/\w+/m.test(stdout)) { |
| 109 | + let msg = 'Uncommitted file changes found! Please commit all changes before deploying.'; |
| 110 | + return Promise.reject(new SilentError(msg)); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + function build() { |
| 116 | + if (options.skipBuild) return Promise.resolve(); |
| 117 | + return win.checkWindowsElevation(ui) |
| 118 | + .then(() => buildTask.run(buildOptions)); |
| 119 | + } |
| 120 | + |
| 121 | + function saveStartingBranchName() { |
| 122 | + return execPromise('git rev-parse --abbrev-ref HEAD') |
| 123 | + .then((stdout) => initialBranch = stdout); |
| 124 | + } |
| 125 | + |
| 126 | + function createGitHubRepoIfNeeded() { |
| 127 | + return execPromise('git remote -v') |
| 128 | + .then(function(stdout) { |
| 129 | + if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) { |
| 130 | + return createGithubRepoTask.run(createGithubRepoOptions); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + function checkoutGhPages() { |
| 136 | + return execPromise(`git checkout ${options.branch}`) |
| 137 | + .catch(createGhPagesBranch) |
| 138 | + } |
| 139 | + |
| 140 | + function createGhPagesBranch() { |
| 141 | + return execPromise(`git checkout --orphan ${options.branch}`) |
| 142 | + .then(() => execPromise('git rm --cached -r .', execOptions)) |
| 143 | + .then(() => execPromise('git add .gitignore', execOptions)) |
| 144 | + .then(() => execPromise('git clean -f -d', execOptions)) |
| 145 | + .then(() => execPromise(`git commit -m \"initial ${options.branch} commit\"`)); |
| 146 | + } |
| 147 | + |
| 148 | + function copyFiles() { |
| 149 | + return fsReadDir('dist') |
| 150 | + .then((files) => Promise.all(files.map((file) => fsCopy(path.join('dist', file), path.join('.', file))))) |
| 151 | + } |
| 152 | + |
| 153 | + function updateBaseHref() { |
| 154 | + let indexHtml = path.join(root, 'index.html'); |
| 155 | + return fsReadFile(indexHtml, 'utf8') |
| 156 | + .then((data) => data.replace(/<base href="\/">/g, `<base href="/${projectName}/>"`)) |
| 157 | + .then((data) => fsWriteFile(indexHtml, data, 'utf8')); |
| 158 | + } |
| 159 | + |
| 160 | + function addAndCommit() { |
| 161 | + return execPromise('git add .', execOptions) |
| 162 | + .then(() => execPromise(`git commit -m "${options.message}"`)) |
| 163 | + .catch(() => Promise.reject(new SilentError('No changes found. Deployment skipped.'))); |
| 164 | + } |
| 165 | + |
| 166 | + function returnStartingBranch() { |
| 167 | + return execPromise(`git checkout ${initialBranch}`); |
| 168 | + } |
| 169 | + |
| 170 | + function pushToGitRepo(committed) { |
| 171 | + return execPromise(`git push origin ${options.branch}`); |
| 172 | + } |
| 173 | + |
| 174 | + function printProjectUrl() { |
| 175 | + return execPromise('git remote -v') |
| 176 | + .then((stdout) => { |
| 177 | + |
| 178 | + let userName = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m)[1].toLowerCase(); |
| 179 | + ui.writeLine(chalk.green(`Deployed! Visit https://${userName}.github.io/${projectName}/`)); |
| 180 | + ui.writeLine('Github pages might take a few minutes to show the deployed site.'); |
| 181 | + }); |
| 182 | + } |
| 183 | + } |
| 184 | +}); |
0 commit comments