diff --git a/CHANGELOG.md b/CHANGELOG.md index 434f2ef..6081b8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 262520c..f3f59ce 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: @@ -56,7 +59,9 @@ deploy-to-github-pages or ```javascript -deploy().catch(err => { console.log(err); }) +deploy().catch(err => { + console.log(err); +}); ``` ## Contributing diff --git a/bin/deploy-to-github-pages.js b/bin/deploy-to-github-pages.js index 161fd68..db4d2c4 100755 --- a/bin/deploy-to-github-pages.js +++ b/bin/deploy-to-github-pages.js @@ -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({ @@ -26,6 +27,7 @@ async function main() { buildUrl: program.buildUrl, dotfiles: !!program.dotfiles, verbose: !!program.verbose, + defaulBranch: program.defaulBranch, }); try { diff --git a/lib/deployment/preparation/preparation.js b/lib/deployment/preparation/preparation.js index 3701ce9..7997db3 100644 --- a/lib/deployment/preparation/preparation.js +++ b/lib/deployment/preparation/preparation.js @@ -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); } diff --git a/lib/deployment/preparation/preparation.spec.js b/lib/deployment/preparation/preparation.spec.js index 328a482..611e5ce 100644 --- a/lib/deployment/preparation/preparation.spec.js +++ b/lib/deployment/preparation/preparation.spec.js @@ -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); @@ -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(); @@ -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'); }); diff --git a/lib/options/options.js b/lib/options/options.js index f05604e..d31e747 100644 --- a/lib/options/options.js +++ b/lib/options/options.js @@ -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) { diff --git a/lib/options/options.spec.js b/lib/options/options.spec.js index 5ad5564..7dcfa4b 100644 --- a/lib/options/options.spec.js +++ b/lib/options/options.spec.js @@ -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', @@ -13,6 +13,7 @@ describe('Options', () => { ...options, directory: 'public', branch: 'master', + defaultBranch: 'master', dotfiles: false, verbose: false, }); @@ -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, @@ -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, @@ -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, @@ -89,6 +93,7 @@ describe('Options', () => { directory: 'a-directory', dotfiles: false, verbose: false, + defaultBranch: 'master', }); delete process.env.GITHUB_WORKFLOW; diff --git a/package.json b/package.json index e8ea433..daf10ad 100644 --- a/package.json +++ b/package.json @@ -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"