Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default branch flag #10

Merged
merged 6 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v1.0.0-beta.6
## Add --defaultBranch flag

- when the `--defaultBranch` flag is passed, users can specify a different default branch other than master

# v1.0.0-beta.5
## Log options when verbose

Expand Down
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

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).
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).
On other branches, it'll be deployed under `/branch/${branchName}`, allowing your peers to QA your built docs/demos easily for better feedback.

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

deploy(options).catch(err => { console.log(err); })
deploy(options).catch(err => {
console.log(err);
});
```

### Options

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

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:

Expand All @@ -56,7 +59,9 @@ deploy-to-github-pages
or

```javascript
deploy().catch(err => { console.log(err); })
deploy().catch(err => {
console.log(err);
});
```

## Contributing
Expand Down
2 changes: 2 additions & 0 deletions bin/deploy-to-github-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function main() {
.option('-u, --build-url [build-url]', 'Link displayed when deployment fails')
.option('--dotfiles', 'Include dotfiles')
.option('--verbose', 'Log verbose information from gh-pages')
.option('--defaultBranch', 'Specify the default branch for your repo')
.parse(process.argv);

const options = cleanOptions({
Expand All @@ -26,6 +27,7 @@ async function main() {
buildUrl: program.buildUrl,
dotfiles: !!program.dotfiles,
verbose: !!program.verbose,
defaulBranch: program.defaulBranch,
});

try {
Expand Down
26 changes: 14 additions & 12 deletions lib/deployment/preparation/preparation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,30 @@ shell.set('-e');

module.exports = { prepareDeployDirectory };

function prepareDeployDirectory(deployDirectory, { directory: sourceDirectory, branch }) {
createBranchDirectoryIfNeeded(deployDirectory, branch);

copyContentWithReplacement(sourceDirectory, getTargetDirectory(deployDirectory, branch));
function prepareDeployDirectory(
deployDirectory,
{ directory: sourceDirectory, branch, defaultBranch },
) {
createBranchDirectoryIfNeeded(deployDirectory, branch, defaultBranch);

copyContentWithReplacement(
sourceDirectory,
getTargetDirectory(deployDirectory, branch, defaultBranch),
);
}

function createBranchDirectoryIfNeeded(deployDirectory, branch) {
if (!isMaster(branch)) {
function createBranchDirectoryIfNeeded(deployDirectory, branch, defaultBranch) {
if (branch !== defaultBranch) {
shell.mkdir('-p', getBranchDirectory(deployDirectory));
}
}

function isMaster(branch) {
return branch === 'master';
}

function getBranchDirectory(deployDirectory) {
return path.join(deployDirectory, BRANCH_DIRECTORY_NAME);
}

function getTargetDirectory(deployDirectory, branch) {
return isMaster(branch)
function getTargetDirectory(deployDirectory, branch, defaultBranch) {
return branch === defaultBranch
? deployDirectory
: path.join(getBranchDirectory(deployDirectory), branch);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/deployment/preparation/preparation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jest.mock('shelljs', () => ({
}));
jest.mock('path', () => ({ join: (...parts) => parts.join('/') }));

const { prepareDeployDirectory } = require('./');
const { prepareDeployDirectory } = require('.');

describe('Preparation', () => {
afterEach(jest.resetAllMocks);
Expand All @@ -21,7 +21,7 @@ describe('Preparation', () => {
});

describe('on master branch', () => {
const options = { directory: 'source', branch: 'master' };
const options = { directory: 'source', branch: 'master', defaultBranch: 'master' };

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

it('removes deploy directory to allow copying with replacement', () => {
expect(shell.rm).not.toBeCalled();
prepareDeployDirectory('deploy-directory', options);
expect(shell.rm).toBeCalledWith('-rf', 'deploy-directory');
});
Expand Down
9 changes: 8 additions & 1 deletion lib/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ function extendPassedOptions(options) {
}

function extendWithDefaultOptions(options) {
return { directory: 'public', branch: 'master', dotfiles: false, verbose: false, ...options };
return {
directory: 'public',
branch: 'master',
defaultBranch: 'master',
dotfiles: false,
verbose: false,
...options,
};
}

function extendWithGithubTokenVariable(options) {
Expand Down
11 changes: 8 additions & 3 deletions lib/options/options.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { createOptions } = require('./');
const { createOptions } = require('.');

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

it('extends passed options with default directory to deploy, master branch, and no dotfiles', () => {
it('extends passed options with default directory to deploy, master branch, defaultBranch master, and no dotfiles', () => {
const options = {
token: 'a-token',
owner: 'an-owner',
Expand All @@ -13,6 +13,7 @@ describe('Options', () => {
...options,
directory: 'public',
branch: 'master',
defaultBranch: 'master',
dotfiles: false,
verbose: false,
});
Expand All @@ -24,6 +25,7 @@ describe('Options', () => {
owner: 'an-owner',
repo: 'a-repo',
branch: 'a-branch',
defaultBranch: 'a-branch',
directory: 'a-directory',
dotfiles: true,
verbose: true,
Expand All @@ -38,6 +40,7 @@ describe('Options', () => {
owner: 'an-owner',
repo: 'a-repo',
branch: 'a-branch',
defaultBranch: 'a-branch',
directory: 'a-directory',
dotfiles: true,
verbose: true,
Expand All @@ -52,13 +55,14 @@ describe('Options', () => {
process.env.CIRCLE_BRANCH = 'a-branch';
process.env.CIRCLE_BUILD_URL = '/a-build-url';

const options = { token: 'a-token', directory: 'a-directory' };
const options = { token: 'a-token', directory: 'a-directory', defaultBranch: 'a-branch' };

expect(createOptions(options)).toEqual({
token: 'a-token',
owner: 'an-owner',
repo: 'a-repo',
branch: 'a-branch',
defaultBranch: 'a-branch',
buildUrl: '/a-build-url',
directory: 'a-directory',
dotfiles: false,
Expand Down Expand Up @@ -89,6 +93,7 @@ describe('Options', () => {
directory: 'a-directory',
dotfiles: false,
verbose: false,
defaultBranch: 'master',
});

delete process.env.GITHUB_WORKFLOW;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deploy-to-github-pages",
"version": "1.0.0-beta.5",
"version": "1.0.0-beta.6",
"description": "A Node library that makes deploying a directory on a branch to GitHub pages easy and automatic.",
"bin": {
"deploy-to-github-pages": "bin/deploy-to-github-pages.js"
Expand Down