Skip to content

Commit d105c64

Browse files
authoredSep 1, 2020
Add default branch flag (#10)
* feat: add defaultBranch flag * chore: refactored logic to accept different default branch * docs: add documentation * chore: reverted formatting * chore: inlined check and remove redundant test
1 parent 747e434 commit d105c64

File tree

8 files changed

+56
-31
lines changed

8 files changed

+56
-31
lines changed
 

‎CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v1.0.0-beta.6
2+
## Add --defaultBranch flag
3+
4+
- when the `--defaultBranch` flag is passed, users can specify a different default branch other than master
5+
16
# v1.0.0-beta.5
27
## Log options when verbose
38

‎README.md

+16-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A Node and CLI tool that makes deploying to GitHub pages **by branch** easy and automatic, best used as part of a CI process.
99

10-
On `master`, your directory will be deployed to your GitHub page root similarly to other libraries, such as the wonderful [`gh-pages`](https://www.npmjs.com/package/gh-pages).
10+
On `master` (or on your specified `defaultBranch`) your directory will be deployed to your GitHub page root similarly to other libraries, such as the wonderful [`gh-pages`](https://www.npmjs.com/package/gh-pages).
1111
On other branches, it'll be deployed under `/branch/${branchName}`, allowing your peers to QA your built docs/demos easily for better feedback.
1212

1313
It also sends a status to a Pull request, if one exists:
@@ -33,19 +33,22 @@ deploy-to-github-pages [...options]
3333
```javascript
3434
const deploy = require('deploy-to-github-pages');
3535

36-
deploy(options).catch(err => { console.log(err); })
36+
deploy(options).catch(err => {
37+
console.log(err);
38+
});
3739
```
3840

3941
### Options
4042

41-
| Option | flag | description | default | env variable | required | required in CI |
42-
|-------------|------:|----------------------------------------------------|------------|----------------|---------:|---------------:|
43-
| `directory` | -d | directory you wish to deploy | `'public'` | | * | * |
44-
| `token` | -t | [GitHub token](https://github.com/settings/tokens) | | `GITHUB_TOKEN` | * | * |
45-
| `owner` | -o | GitHub repo owner/org | | | * | |
46-
| `repo` | -r | GitHub repo name | | | * | |
47-
| `branch` | -b | branch name | `'master'` | | * | |
48-
| `buildUrl` | -u | link displayed when deployment fails | | | | |
43+
| Option | flag | description | default | env variable | required | required in CI |
44+
| --------------- | ---: | -------------------------------------------------- | ---------- | -------------- | -------: | -------------: |
45+
| `directory` | -d | directory you wish to deploy | `'public'` | | \* | \* |
46+
| `token` | -t | [GitHub token](https://github.com/settings/tokens) | | `GITHUB_TOKEN` | \* | \* |
47+
| `owner` | -o | GitHub repo owner/org | | | \* | |
48+
| `repo` | -r | GitHub repo name | | | \* | |
49+
| `branch` | -b | branch name | `'master'` | | \* | |
50+
| `buildUrl` | -u | link displayed when deployment fails | | | | |
51+
| `defaultBranch` | | Your github default branch | `'master'` | | | |
4952

5053
Therefore, if ran from CircleCI or GitHub Actions workflows with a `GITHUB_TOKEN` environment variable present and the directory to be deployed is named `public`, _no configuration options are needed_, so just the following is enough:
5154

@@ -56,7 +59,9 @@ deploy-to-github-pages
5659
or
5760

5861
```javascript
59-
deploy().catch(err => { console.log(err); })
62+
deploy().catch(err => {
63+
console.log(err);
64+
});
6065
```
6166

6267
## Contributing

‎bin/deploy-to-github-pages.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function main() {
1515
.option('-u, --build-url [build-url]', 'Link displayed when deployment fails')
1616
.option('--dotfiles', 'Include dotfiles')
1717
.option('--verbose', 'Log verbose information from gh-pages')
18+
.option('--defaultBranch', 'Specify the default branch for your repo')
1819
.parse(process.argv);
1920

2021
const options = cleanOptions({
@@ -26,6 +27,7 @@ async function main() {
2627
buildUrl: program.buildUrl,
2728
dotfiles: !!program.dotfiles,
2829
verbose: !!program.verbose,
30+
defaulBranch: program.defaulBranch,
2931
});
3032

3133
try {

‎lib/deployment/preparation/preparation.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@ shell.set('-e');
77

88
module.exports = { prepareDeployDirectory };
99

10-
function prepareDeployDirectory(deployDirectory, { directory: sourceDirectory, branch }) {
11-
createBranchDirectoryIfNeeded(deployDirectory, branch);
12-
13-
copyContentWithReplacement(sourceDirectory, getTargetDirectory(deployDirectory, branch));
10+
function prepareDeployDirectory(
11+
deployDirectory,
12+
{ directory: sourceDirectory, branch, defaultBranch },
13+
) {
14+
createBranchDirectoryIfNeeded(deployDirectory, branch, defaultBranch);
15+
16+
copyContentWithReplacement(
17+
sourceDirectory,
18+
getTargetDirectory(deployDirectory, branch, defaultBranch),
19+
);
1420
}
1521

16-
function createBranchDirectoryIfNeeded(deployDirectory, branch) {
17-
if (!isMaster(branch)) {
22+
function createBranchDirectoryIfNeeded(deployDirectory, branch, defaultBranch) {
23+
if (branch !== defaultBranch) {
1824
shell.mkdir('-p', getBranchDirectory(deployDirectory));
1925
}
2026
}
2127

22-
function isMaster(branch) {
23-
return branch === 'master';
24-
}
25-
2628
function getBranchDirectory(deployDirectory) {
2729
return path.join(deployDirectory, BRANCH_DIRECTORY_NAME);
2830
}
2931

30-
function getTargetDirectory(deployDirectory, branch) {
31-
return isMaster(branch)
32+
function getTargetDirectory(deployDirectory, branch, defaultBranch) {
33+
return branch === defaultBranch
3234
? deployDirectory
3335
: path.join(getBranchDirectory(deployDirectory), branch);
3436
}

‎lib/deployment/preparation/preparation.spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jest.mock('shelljs', () => ({
99
}));
1010
jest.mock('path', () => ({ join: (...parts) => parts.join('/') }));
1111

12-
const { prepareDeployDirectory } = require('./');
12+
const { prepareDeployDirectory } = require('.');
1313

1414
describe('Preparation', () => {
1515
afterEach(jest.resetAllMocks);
@@ -21,7 +21,7 @@ describe('Preparation', () => {
2121
});
2222

2323
describe('on master branch', () => {
24-
const options = { directory: 'source', branch: 'master' };
24+
const options = { directory: 'source', branch: 'master', defaultBranch: 'master' };
2525

2626
it('does not create branch directory', () => {
2727
expect(shell.mkdir).not.toBeCalled();
@@ -30,7 +30,6 @@ describe('Preparation', () => {
3030
});
3131

3232
it('removes deploy directory to allow copying with replacement', () => {
33-
expect(shell.rm).not.toBeCalled();
3433
prepareDeployDirectory('deploy-directory', options);
3534
expect(shell.rm).toBeCalledWith('-rf', 'deploy-directory');
3635
});

‎lib/options/options.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ function extendPassedOptions(options) {
1717
}
1818

1919
function extendWithDefaultOptions(options) {
20-
return { directory: 'public', branch: 'master', dotfiles: false, verbose: false, ...options };
20+
return {
21+
directory: 'public',
22+
branch: 'master',
23+
defaultBranch: 'master',
24+
dotfiles: false,
25+
verbose: false,
26+
...options,
27+
};
2128
}
2229

2330
function extendWithGithubTokenVariable(options) {

‎lib/options/options.spec.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { createOptions } = require('./');
1+
const { createOptions } = require('.');
22

33
describe('Options', () => {
44
beforeEach(cleanEnvironmentVariables);
55

6-
it('extends passed options with default directory to deploy, master branch, and no dotfiles', () => {
6+
it('extends passed options with default directory to deploy, master branch, defaultBranch master, and no dotfiles', () => {
77
const options = {
88
token: 'a-token',
99
owner: 'an-owner',
@@ -13,6 +13,7 @@ describe('Options', () => {
1313
...options,
1414
directory: 'public',
1515
branch: 'master',
16+
defaultBranch: 'master',
1617
dotfiles: false,
1718
verbose: false,
1819
});
@@ -24,6 +25,7 @@ describe('Options', () => {
2425
owner: 'an-owner',
2526
repo: 'a-repo',
2627
branch: 'a-branch',
28+
defaultBranch: 'a-branch',
2729
directory: 'a-directory',
2830
dotfiles: true,
2931
verbose: true,
@@ -38,6 +40,7 @@ describe('Options', () => {
3840
owner: 'an-owner',
3941
repo: 'a-repo',
4042
branch: 'a-branch',
43+
defaultBranch: 'a-branch',
4144
directory: 'a-directory',
4245
dotfiles: true,
4346
verbose: true,
@@ -52,13 +55,14 @@ describe('Options', () => {
5255
process.env.CIRCLE_BRANCH = 'a-branch';
5356
process.env.CIRCLE_BUILD_URL = '/a-build-url';
5457

55-
const options = { token: 'a-token', directory: 'a-directory' };
58+
const options = { token: 'a-token', directory: 'a-directory', defaultBranch: 'a-branch' };
5659

5760
expect(createOptions(options)).toEqual({
5861
token: 'a-token',
5962
owner: 'an-owner',
6063
repo: 'a-repo',
6164
branch: 'a-branch',
65+
defaultBranch: 'a-branch',
6266
buildUrl: '/a-build-url',
6367
directory: 'a-directory',
6468
dotfiles: false,
@@ -89,6 +93,7 @@ describe('Options', () => {
8993
directory: 'a-directory',
9094
dotfiles: false,
9195
verbose: false,
96+
defaultBranch: 'master',
9297
});
9398

9499
delete process.env.GITHUB_WORKFLOW;

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deploy-to-github-pages",
3-
"version": "1.0.0-beta.5",
3+
"version": "1.0.0-beta.6",
44
"description": "A Node library that makes deploying a directory on a branch to GitHub pages easy and automatic.",
55
"bin": {
66
"deploy-to-github-pages": "bin/deploy-to-github-pages.js"

0 commit comments

Comments
 (0)
Please sign in to comment.