Skip to content

Commit a34aca8

Browse files
committed
fix(deploy): fix file copy, index tag rewrite (#772)
* fix(deploy): fix file copy, index tag rewrite * feat(deploy): allow gh-pages deploy to user/org page
1 parent 285db13 commit a34aca8

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The generated project has dependencies that require **Node 4 or greater**.
3131
* [Running Unit Tests](#running-unit-tests)
3232
* [Running End-to-End Tests](#running-end-to-end-tests)
3333
* [Deploying the App via GitHub Pages](#deploying-the-app-via-github-pages)
34+
* [Linting and formatting code](#linting-and-formatting-code)
3435
* [Support for offline applications](#support-for-offline-applications)
3536
* [Commands autocompletion](#commands-autocompletion)
3637
* [CSS preprocessor integration](#css-preprocessor-integration)
@@ -163,7 +164,7 @@ ng github-pages:deploy --message "Optional commit message"
163164
This will do the following:
164165

165166
- creates GitHub repo for the current project if one doesn't exist
166-
- rebuilds the app at the current `HEAD`
167+
- rebuilds the app in production mode at the current `HEAD`
167168
- creates a local `gh-pages` branch if one doesn't exist
168169
- moves your app to the `gh-pages` branch and creates a commit
169170
- edit the base tag in index.html to support github pages
@@ -174,6 +175,16 @@ Creating the repo requires a token from github, and the remaining functionality
174175
relies on ssh authentication for all git operations that communicate with github.com.
175176
To simplify the authentication, be sure to [setup your ssh keys](https://help.github.com/articles/generating-ssh-keys/).
176177

178+
If you are deploying a [user or organization page](https://help.github.com/articles/user-organization-and-project-pages/), you can instead use the following command:
179+
180+
```
181+
ng github-pages:deploy --user-page --message "Optional commit message"
182+
```
183+
184+
This command pushes the app to the `master` branch on the github repo instead
185+
of pushing to `gh-pages`, since user and organization pages require this.
186+
187+
177188
### Linting and formatting code
178189

179190
You can lint or format your app code by running `ng lint` or `ng format` respectively.

addon/ng2/commands/github-pages-deploy.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ module.exports = Command.extend({
3333
default: 'production',
3434
description: 'The Angular environment to create a build for'
3535
}, {
36-
name: 'branch',
37-
type: String,
38-
default: 'gh-pages',
39-
description: 'The git branch to push your pages to'
36+
name: 'user-page',
37+
type: Boolean,
38+
default: false,
39+
description: 'Deploy as a user/org page'
4040
}, {
4141
name: 'skip-build',
4242
type: Boolean,
@@ -62,6 +62,8 @@ module.exports = Command.extend({
6262
};
6363
var projectName = this.project.pkg.name;
6464

65+
let ghPagesBranch = 'gh-pages';
66+
let destinationBranch = options.userPage ? 'master' : ghPagesBranch;
6567
let initialBranch;
6668

6769
// declared here so that tests can stub exec
@@ -129,33 +131,46 @@ module.exports = Command.extend({
129131
.then(function(stdout) {
130132
if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) {
131133
return createGithubRepoTask.run(createGithubRepoOptions)
132-
.then(() => execPromise(`git push -u origin ${initialBranch}`));
134+
.then(() => {
135+
// only push starting branch if it's not the destinationBranch
136+
// this happens commonly when using github user pages, since
137+
// they require the destination branch to be 'master'
138+
if (destinationBranch !== initialBranch) {
139+
execPromise(`git push -u origin ${initialBranch}`);
140+
}
141+
});
133142
}
134143
});
135144
}
136145

137146
function checkoutGhPages() {
138-
return execPromise(`git checkout ${options.branch}`)
147+
return execPromise(`git checkout ${ghPagesBranch}`)
139148
.catch(createGhPagesBranch)
140149
}
141150

142151
function createGhPagesBranch() {
143-
return execPromise(`git checkout --orphan ${options.branch}`)
152+
return execPromise(`git checkout --orphan ${ghPagesBranch}`)
144153
.then(() => execPromise('git rm --cached -r .', execOptions))
145154
.then(() => execPromise('git add .gitignore', execOptions))
146155
.then(() => execPromise('git clean -f -d', execOptions))
147-
.then(() => execPromise(`git commit -m \"initial ${options.branch} commit\"`));
156+
.then(() => execPromise(`git commit -m \"initial ${ghPagesBranch} commit\"`));
148157
}
149158

150159
function copyFiles() {
151160
return fsReadDir('dist')
152-
.then((files) => Promise.all(files.map((file) => fsCopy(path.join('dist', file), path.join('.', file)))))
161+
.then((files) => Promise.all(files.map((file) => {
162+
if (file === '.gitignore'){
163+
// don't overwrite the .gitignore file
164+
return Promise.resolve();
165+
}
166+
return fsCopy(path.join('dist', file), path.join('.', file))
167+
})));
153168
}
154169

155170
function updateBaseHref() {
156171
let indexHtml = path.join(root, 'index.html');
157172
return fsReadFile(indexHtml, 'utf8')
158-
.then((data) => data.replace(/<base href="\/">/g, `<base href="/${projectName}/>"`))
173+
.then((data) => data.replace(/<base href="\/">/g, `<base href="/${projectName}/">`))
159174
.then((data) => fsWriteFile(indexHtml, data, 'utf8'));
160175
}
161176

@@ -169,8 +184,8 @@ module.exports = Command.extend({
169184
return execPromise(`git checkout ${initialBranch}`);
170185
}
171186

172-
function pushToGitRepo(committed) {
173-
return execPromise(`git push origin ${options.branch}`);
187+
function pushToGitRepo() {
188+
return execPromise(`git push origin ${ghPagesBranch}:${destinationBranch}`);
174189
}
175190

176191
function printProjectUrl() {

tests/acceptance/github-pages-deploy.spec.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
2222
let execStub;
2323
let project = 'foo',
2424
initialBranch = 'master',
25-
branch = 'gh-pages',
25+
ghPagesBranch = 'gh-pages',
2626
message = 'new gh-pages version',
2727
remote = 'origin [email protected]:username/project.git (fetch)';
2828

@@ -71,66 +71,66 @@ describe('Acceptance: ng github-pages:deploy', function() {
7171
execStub.addExecSuccess('git status --porcelain')
7272
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
7373
.addExecSuccess('git remote -v', remote)
74-
.addExecSuccess(`git checkout ${branch}`)
74+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
7575
.addExecSuccess('git add .')
7676
.addExecSuccess(`git commit -m "${message}"`)
7777
.addExecSuccess(`git checkout ${initialBranch}`)
78-
.addExecSuccess(`git push origin ${branch}`)
78+
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
7979
.addExecSuccess('git remote -v', remote);
8080

8181
return ng(['github-pages:deploy', '--skip-build'])
8282
.then(() => {
8383
let indexHtml = path.join(process.cwd(), 'index.html');
8484
return fsReadFile(indexHtml, 'utf8');
8585
})
86-
.then((data) => expect(data.search(`<base href="/${project}/>"`)).to.not.equal(-1));
86+
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
8787
});
8888

8989
it('should deploy with changed defaults', function() {
90-
let branch = 'not-gh-pages',
90+
let userPageBranch = 'master',
9191
message = 'not new gh-pages version';
9292

9393
execStub.addExecSuccess('git status --porcelain')
9494
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
9595
.addExecSuccess('git remote -v', remote)
96-
.addExecSuccess(`git checkout ${branch}`)
96+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
9797
.addExecSuccess('git add .')
9898
.addExecSuccess(`git commit -m "${message}"`)
9999
.addExecSuccess(`git checkout ${initialBranch}`)
100-
.addExecSuccess(`git push origin ${branch}`)
100+
.addExecSuccess(`git push origin ${ghPagesBranch}:${userPageBranch}`)
101101
.addExecSuccess('git remote -v', remote);
102102

103103
return ng(['github-pages:deploy', '--skip-build', `--message=${message}`,
104-
`--branch=${branch}`])
104+
'--user-page'])
105105
.then(() => {
106106
let indexHtml = path.join(process.cwd(), 'index.html');
107107
return fsReadFile(indexHtml, 'utf8');
108108
})
109-
.then((data) => expect(data.search(`<base href="/${project}/>"`)).to.not.equal(-1));
109+
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
110110
});
111111

112112
it('should create branch if needed', function() {
113113
execStub.addExecSuccess('git status --porcelain')
114114
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
115115
.addExecSuccess('git remote -v', remote)
116-
.addExecError(`git checkout ${branch}`)
117-
.addExecSuccess(`git checkout --orphan ${branch}`)
116+
.addExecError(`git checkout ${ghPagesBranch}`)
117+
.addExecSuccess(`git checkout --orphan ${ghPagesBranch}`)
118118
.addExecSuccess('git rm --cached -r .')
119119
.addExecSuccess('git add .gitignore')
120120
.addExecSuccess('git clean -f -d')
121-
.addExecSuccess(`git commit -m \"initial ${branch} commit\"`)
121+
.addExecSuccess(`git commit -m \"initial ${ghPagesBranch} commit\"`)
122122
.addExecSuccess('git add .')
123123
.addExecSuccess(`git commit -m "${message}"`)
124124
.addExecSuccess(`git checkout ${initialBranch}`)
125-
.addExecSuccess(`git push origin ${branch}`)
125+
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
126126
.addExecSuccess('git remote -v', remote);
127127

128128
return ng(['github-pages:deploy', '--skip-build'])
129129
.then(() => {
130130
let indexHtml = path.join(process.cwd(), 'index.html');
131131
return fsReadFile(indexHtml, 'utf8');
132132
})
133-
.then((data) => expect(data.search(`<base href="/${project}/>"`)).to.not.equal(-1));
133+
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
134134
});
135135

136136
it('should create repo if needed', function() {
@@ -143,11 +143,11 @@ describe('Acceptance: ng github-pages:deploy', function() {
143143
.addExecSuccess('git remote -v', noRemote)
144144
.addExecSuccess(`git remote add origin [email protected]:${username}/${project}.git`)
145145
.addExecSuccess(`git push -u origin ${initialBranch}`)
146-
.addExecSuccess(`git checkout ${branch}`)
146+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
147147
.addExecSuccess('git add .')
148148
.addExecSuccess(`git commit -m "${message}"`)
149149
.addExecSuccess(`git checkout ${initialBranch}`)
150-
.addExecSuccess(`git push origin ${branch}`)
150+
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
151151
.addExecSuccess('git remote -v', remote);
152152

153153
var httpsStub = sinon.stub(https, 'request', httpsRequestStubFunc);
@@ -187,7 +187,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
187187
let indexHtml = path.join(process.cwd(), 'index.html');
188188
return fsReadFile(indexHtml, 'utf8');
189189
})
190-
.then((data) => expect(data.search(`<base href="/${project}/>"`)).to.not.equal(-1))
190+
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1))
191191
.then(() => httpsStub.restore());
192192
});
193193

@@ -246,7 +246,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
246246
execStub.addExecSuccess('git status --porcelain')
247247
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
248248
.addExecSuccess('git remote -v', remote)
249-
.addExecSuccess(`git checkout ${branch}`)
249+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
250250
.addExecSuccess('git add .')
251251
.addExecSuccess(`git commit -m "${message}"`)
252252
.addExecError(`git checkout ${initialBranch}`, 'error: cannot stat \'src/client\': Permission denied');

0 commit comments

Comments
 (0)