Skip to content

Commit c4dabcf

Browse files
authoredDec 1, 2019
Master deployment to root (#4)
* Deploy to root on master branch * Add default branch value of master * Bump version to v1.0.0-beta.1
1 parent 4b37048 commit c4dabcf

File tree

7 files changed

+82
-81
lines changed

7 files changed

+82
-81
lines changed
 

‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v1.0.0-beta.1
2+
## Deploy to root on `master` branch
3+
4+
- when branch is `master`, the directory won't be deployed to `/branch/${branchName}`, but `/` instead, allowing consumers to only rely on this package and not need `gh-pages` in addition
5+
- default `branch` value of `master` is added
6+
17
# v0.2.8
28
## Fix deployment id error
39

‎README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
[![CircleCI](https://img.shields.io/circleci/project/github/oliverviljamaa/deploy-directory-on-branch-to-gh-pages/beta.svg)](https://circleci.com/gh/oliverviljamaa/deploy-directory-on-branch-to-gh-pages)
66
[![npm](https://img.shields.io/npm/l/deploy-directory-on-branch-to-gh-pages.svg)](https://github.com/oliverviljamaa/deploy-directory-on-branch-to-gh-pages/blob/beta/LICENSE)
77

8-
A Node and CLI tool that makes deploying a directory on a branch to GitHub pages easy and automatic,
9-
to help your peers QA your built docs/demos easily for better feedback.
8+
A Node and CLI tool that makes deploying to GitHub pages **by branch** easy and automatic, best used as part of a CI process.
9+
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).
11+
On other branches, it'll be deployed under `/branch/${branchName}`, allowing your peers to QA your built docs/demos easily for better feedback.
1012

1113
It also sends a status to a Pull request, if one exists:
1214

@@ -42,7 +44,7 @@ deploy(options).catch(err => { console.log(err); })
4244
| `token` | -t | [GitHub token](https://github.com/settings/tokens) | | `GITHUB_TOKEN` | * | * |
4345
| `owner` | -o | GitHub repo owner/org | | | * | |
4446
| `repo` | -r | GitHub repo name | | | * | |
45-
| `branch` | -b | branch name | | | * | |
47+
| `branch` | -b | branch name | `'master'` | | * | |
4648
| `buildUrl` | -u | link displayed when deployment fails | | | | |
4749

4850
Therefore, if ran from CircleCI 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:
+17-28
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,39 @@
11
const shell = require('shelljs');
22
const path = require('path');
33

4-
const DEPLOY_BRANCH_DIRECTORY = 'branch';
4+
const BRANCH_DIRECTORY_NAME = 'branch';
55

66
shell.set('-e');
77

88
module.exports = { prepareDeployDirectory };
99

10-
function prepareDeployDirectory(directory, options) {
11-
checkoutPagesBranchToDeployDirectory(directory);
12-
moveDirectoryContentToDeployDirectory(directory, options);
13-
}
14-
15-
function checkoutPagesBranchToDeployDirectory(directory) {
16-
createDeployDirectoryIfDoesNotExist(directory);
10+
function prepareDeployDirectory(deployDirectory, { directory: sourceDirectory, branch }) {
11+
createBranchDirectoryIfNeeded(deployDirectory, branch);
1712

18-
try {
19-
pullGithubPagesBranchToDirectory(directory);
20-
} catch (err) {
21-
console.error(err);
22-
console.log('gh-pages branch does not exist yet, it will be created automatically...');
23-
}
13+
copyContentWithReplacement(sourceDirectory, getTargetDirectory(deployDirectory, branch));
2414
}
2515

26-
function createDeployDirectoryIfDoesNotExist(directory) {
27-
shell.mkdir('-p', directory);
16+
function createBranchDirectoryIfNeeded(deployDirectory, branch) {
17+
if (!isMaster(branch)) {
18+
shell.mkdir('-p', getBranchDirectory(deployDirectory));
19+
}
2820
}
2921

30-
function pullGithubPagesBranchToDirectory(directory) {
31-
shell.exec(`git --work-tree=./${directory} checkout gh-pages -- .`, { silent: true });
22+
function isMaster(branch) {
23+
return branch === 'master';
3224
}
3325

34-
function moveDirectoryContentToDeployDirectory(directory, options) {
35-
createBranchDirectoryIfDoesNotExist(directory);
36-
37-
const source = options.directory;
38-
const target = path.join(directory, DEPLOY_BRANCH_DIRECTORY, options.branch);
39-
40-
moveContent(source, target);
26+
function getBranchDirectory(deployDirectory) {
27+
return path.join(deployDirectory, BRANCH_DIRECTORY_NAME);
4128
}
4229

43-
function createBranchDirectoryIfDoesNotExist(directory) {
44-
shell.mkdir('-p', path.join(directory, DEPLOY_BRANCH_DIRECTORY));
30+
function getTargetDirectory(deployDirectory, branch) {
31+
return isMaster(branch)
32+
? deployDirectory
33+
: path.join(getBranchDirectory(deployDirectory), branch);
4534
}
4635

47-
function moveContent(source, target) {
36+
function copyContentWithReplacement(source, target) {
4837
shell.rm('-rf', target);
4938
shell.cp('-r', source, target);
5039
}

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

+36-41
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,53 @@ const { prepareDeployDirectory } = require('./');
1414
describe('Preparation', () => {
1515
afterEach(jest.resetAllMocks);
1616

17-
it('sets shell to throw error when an operation fails', () => {
18-
expect(shell.set).toBeCalledWith('-e');
17+
describe('always', () => {
18+
it('sets shell to throw error when an operation fails', () => {
19+
expect(shell.set).toBeCalledWith('-e');
20+
});
1921
});
2022

21-
it('creates deploy directory if it does not exist', () => {
22-
expect(shell.mkdir).not.toBeCalled();
23-
prepareDeployDirectory('a-directory', {});
24-
expect(shell.mkdir).toBeCalledWith('-p', 'a-directory');
25-
});
23+
describe('on master branch', () => {
24+
const options = { directory: 'source', branch: 'master' };
2625

27-
it('pulls gh-pages branch content to deploy directory', () => {
28-
expect(shell.exec).not.toBeCalled();
29-
prepareDeployDirectory('a-directory', {});
30-
expect(shell.exec).toBeCalledWith(`git --work-tree=./a-directory checkout gh-pages -- .`, {
31-
silent: true,
26+
it('does not create branch directory', () => {
27+
expect(shell.mkdir).not.toBeCalled();
28+
prepareDeployDirectory('deploy-directory', options);
29+
expect(shell.mkdir).not.toBeCalled();
3230
});
33-
});
3431

35-
it('logs a message if pulling gh-pages branch content to deploy directory fails', () => {
36-
console.log = jest.fn();
37-
38-
shell.exec.mockImplementation(() => {
39-
throw new Error('An error');
32+
it('removes deploy directory to allow copying with replacement', () => {
33+
expect(shell.rm).not.toBeCalled();
34+
prepareDeployDirectory('deploy-directory', options);
35+
expect(shell.rm).toBeCalledWith('-rf', 'deploy-directory');
4036
});
4137

42-
expect(console.log).not.toBeCalled();
43-
prepareDeployDirectory('a-directory', {});
44-
expect(console.log).toBeCalled();
38+
it('moves content from source directory to deploy directory', () => {
39+
expect(shell.cp).not.toBeCalled();
40+
prepareDeployDirectory('deploy-directory', options);
41+
expect(shell.cp).toBeCalledWith('-r', 'source', 'deploy-directory');
42+
});
4543
});
4644

47-
it('creates branch directory if it does not exist', () => {
48-
expect(shell.mkdir).not.toBeCalled();
49-
prepareDeployDirectory('a-directory', {});
50-
expect(shell.mkdir).toBeCalledWith('-p', 'a-directory/branch');
51-
});
45+
describe('on a non-master branch', () => {
46+
const options = { directory: 'source', branch: 'not-master' };
5247

53-
it('removes branch directory to be certain it is clean', () => {
54-
expect(shell.rm).not.toBeCalled();
55-
prepareDeployDirectory('a-directory', { branch: 'a-branch' });
56-
expect(shell.rm).toBeCalledWith('-rf', 'a-directory/branch/a-branch');
57-
});
48+
it('creates branch directory ', () => {
49+
expect(shell.mkdir).not.toBeCalled();
50+
prepareDeployDirectory('deploy-directory', options);
51+
expect(shell.mkdir).toBeCalledWith('-p', 'deploy-directory/branch');
52+
});
53+
54+
it('removes current branch directory to allow copying with replacement', () => {
55+
expect(shell.rm).not.toBeCalled();
56+
prepareDeployDirectory('deploy-directory', options);
57+
expect(shell.rm).toBeCalledWith('-rf', 'deploy-directory/branch/not-master');
58+
});
5859

59-
it('moves content from directory to be deployed to branch directory', () => {
60-
expect(shell.cp).not.toBeCalled();
61-
prepareDeployDirectory('a-directory', {
62-
directory: 'a-directory-to-be-deployed',
63-
branch: 'a-branch',
60+
it('moves content from source directory to branch directory', () => {
61+
expect(shell.cp).not.toBeCalled();
62+
prepareDeployDirectory('deploy-directory', options);
63+
expect(shell.cp).toBeCalledWith('-r', 'source', 'deploy-directory/branch/not-master');
6464
});
65-
expect(shell.cp).toBeCalledWith(
66-
'-r',
67-
'a-directory-to-be-deployed',
68-
'a-directory/branch/a-branch',
69-
);
7065
});
7166
});

‎lib/options/options.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ function createOptions(passedOptions) {
99
}
1010

1111
function extendPassedOptions(options) {
12-
return extendWithCircleVariablesIfCircle(
13-
extendWithGithubTokenVariable(extendWithDefaultOptions(options)),
12+
return extendWithDefaultOptions(
13+
extendWithGithubTokenVariable(extendWithCircleVariablesIfCircle(options)),
1414
);
1515
}
1616

1717
function extendWithDefaultOptions(options) {
18-
return { directory: 'public', ...options };
18+
return { directory: 'public', branch: 'master', ...options };
1919
}
2020

2121
function extendWithGithubTokenVariable(options) {

‎lib/options/options.spec.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@ const { createOptions } = require('./');
33
describe('Options', () => {
44
beforeEach(cleanEnvironmentVariables);
55

6-
it('extends passed options with default directory to deploy', () => {
6+
it('extends passed options with default directory to deploy and master branch', () => {
7+
const options = {
8+
token: 'a-token',
9+
owner: 'an-owner',
10+
repo: 'a-repo',
11+
};
12+
expect(createOptions(options)).toEqual({ ...options, directory: 'public', branch: 'master' });
13+
});
14+
15+
it('prefers passed options to default options', () => {
716
const options = {
817
token: 'a-token',
918
owner: 'an-owner',
1019
repo: 'a-repo',
1120
branch: 'a-branch',
21+
directory: 'a-directory',
1222
};
13-
expect(createOptions(options)).toEqual({ ...options, directory: 'public' });
23+
expect(createOptions(options)).toEqual(options);
1424
});
1525

1626
it('extends passed options with github token variable if exists', () => {
@@ -45,14 +55,13 @@ describe('Options', () => {
4555
});
4656

4757
it('throws if any required option is missing', () => {
48-
expect.assertions(4);
58+
expect.assertions(3);
4959

50-
const requiredOptions = ['token', 'owner', 'repo', 'branch'];
60+
const requiredOptions = ['token', 'owner', 'repo'];
5161
const fullOptions = () => ({
5262
token: 'a-token',
5363
owner: 'an-owner',
5464
repo: 'a-repo',
55-
branch: 'a-branch',
5665
});
5766

5867
requiredOptions.forEach(name => {

‎package.json

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

0 commit comments

Comments
 (0)
Please sign in to comment.