|
| 1 | +import*asCommandfrom'ember-cli/lib/models/command'; |
| 2 | +import*asSilentErrorfrom'silent-error'; |
| 3 | +import{exec}from'child_process'; |
| 4 | +import*asPromisefrom'ember-cli/lib/ext/promise'; |
| 5 | +import*aschalkfrom'chalk'; |
| 6 | +import*asfsfrom'fs'; |
| 7 | +import*asfsefrom'fs-extra'; |
| 8 | +import*aspathfrom'path'; |
| 9 | +import*asBuildTaskfrom'ember-cli/lib/tasks/build'; |
| 10 | +import*aswinfrom'ember-cli/lib/utilities/windows-admin'; |
| 11 | +import*asCreateGithubRepofrom'../tasks/create-github-repo'; |
| 12 | + |
| 13 | +constfsReadFile=Promise.denodeify(fs.readFile); |
| 14 | +constfsWriteFile=Promise.denodeify(fs.writeFile); |
| 15 | +constfsReadDir=Promise.denodeify(fs.readdir); |
| 16 | +constfsCopy=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 | +varui=this.ui; |
| 59 | +varroot=this.project.root; |
| 60 | +varexecOptions={ |
| 61 | +cwd: root |
| 62 | +}; |
| 63 | +varprojectName=this.project.pkg.name; |
| 64 | + |
| 65 | +letinitialBranch; |
| 66 | + |
| 67 | +// declared here so that tests can stub exec |
| 68 | +constexecPromise=Promise.denodeify(exec); |
| 69 | + |
| 70 | +varbuildTask=newBuildTask({ |
| 71 | +ui: this.ui, |
| 72 | +analytics: this.analytics, |
| 73 | +project: this.project |
| 74 | +}); |
| 75 | + |
| 76 | +varbuildOptions={ |
| 77 | +environment: options.environment, |
| 78 | +outputPath: 'dist/' |
| 79 | +}; |
| 80 | + |
| 81 | +varcreateGithubRepoTask=newCreateGithubRepo({ |
| 82 | +ui: this.ui, |
| 83 | +analytics: this.analytics, |
| 84 | +project: this.project |
| 85 | +}); |
| 86 | + |
| 87 | +varcreateGithubRepoOptions={ |
| 88 | + projectName, |
| 89 | +ghUsername: options.ghUsername, |
| 90 | +ghToken: options.ghToken |
| 91 | +}; |
| 92 | + |
| 93 | +returncheckForPendingChanges() |
| 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 | +functioncheckForPendingChanges(){ |
| 106 | +returnexecPromise('git status --porcelain') |
| 107 | +.then(stdout=>{ |
| 108 | +if(/\w+/m.test(stdout)){ |
| 109 | +letmsg='Uncommitted file changes found! Please commit all changes before deploying.'; |
| 110 | +returnPromise.reject(newSilentError(msg)); |
| 111 | +} |
| 112 | +}); |
| 113 | +} |
| 114 | + |
| 115 | +functionbuild(){ |
| 116 | +if(options.skipBuild)returnPromise.resolve(); |
| 117 | +returnwin.checkWindowsElevation(ui) |
| 118 | +.then(()=>buildTask.run(buildOptions)); |
| 119 | +} |
| 120 | + |
| 121 | +functionsaveStartingBranchName(){ |
| 122 | +returnexecPromise('git rev-parse --abbrev-ref HEAD') |
| 123 | +.then((stdout)=>initialBranch=stdout); |
| 124 | +} |
| 125 | + |
| 126 | +functioncreateGitHubRepoIfNeeded(){ |
| 127 | +returnexecPromise('git remote -v') |
| 128 | +.then(function(stdout){ |
| 129 | +if(!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)){ |
| 130 | +returncreateGithubRepoTask.run(createGithubRepoOptions); |
| 131 | +} |
| 132 | +}); |
| 133 | +} |
| 134 | + |
| 135 | +functioncheckoutGhPages(){ |
| 136 | +returnexecPromise(`git checkout ${options.branch}`) |
| 137 | +.catch(createGhPagesBranch) |
| 138 | +} |
| 139 | + |
| 140 | +functioncreateGhPagesBranch(){ |
| 141 | +returnexecPromise(`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 | +functioncopyFiles(){ |
| 149 | +returnfsReadDir('dist') |
| 150 | +.then((files)=>Promise.all(files.map((file)=>fsCopy(path.join('dist',file),path.join('.',file))))) |
| 151 | +} |
| 152 | + |
| 153 | +functionupdateBaseHref(){ |
| 154 | +letindexHtml=path.join(root,'index.html'); |
| 155 | +returnfsReadFile(indexHtml,'utf8') |
| 156 | +.then((data)=>data.replace(/<basehref="\/">/g,`<base href="/${projectName}/>"`)) |
| 157 | +.then((data)=>fsWriteFile(indexHtml,data,'utf8')); |
| 158 | +} |
| 159 | + |
| 160 | +functionaddAndCommit(){ |
| 161 | +returnexecPromise('git add .',execOptions) |
| 162 | +.then(()=>execPromise(`git commit -m "${options.message}"`)) |
| 163 | +.catch(()=>Promise.reject(newSilentError('No changes found. Deployment skipped.'))); |
| 164 | +} |
| 165 | + |
| 166 | +functionreturnStartingBranch(){ |
| 167 | +returnexecPromise(`git checkout ${initialBranch}`); |
| 168 | +} |
| 169 | + |
| 170 | +functionpushToGitRepo(committed){ |
| 171 | +returnexecPromise(`git push origin ${options.branch}`); |
| 172 | +} |
| 173 | + |
| 174 | +functionprintProjectUrl(){ |
| 175 | +returnexecPromise('git remote -v') |
| 176 | +.then((stdout)=>{ |
| 177 | + |
| 178 | +letuserName=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