Skip to content

Commit

Permalink
Merge pull request #533 from tschaub/nojekyll-cname
Browse files Browse the repository at this point in the history
Add --nojekyll and --cname options
  • Loading branch information
tschaub authored Nov 18, 2023
2 parents df07fd4 + 8181619 commit 55501ff
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 2 deletions.
6 changes: 6 additions & 0 deletions bin/gh-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ function main(args) {
.option('-g, --tag <tag>', 'add tag to commit')
.option('--git <git>', 'Path to git executable', ghpages.defaults.git)
.option('-t, --dotfiles', 'Include dotfiles')
.option('--nojekyll', 'Add a .nojekyll file to disable Jekyll')
.option(
'--cname <CNAME>',
'Add a CNAME file with the name of your custom domain'
)
.option('-r, --repo <repo>', 'URL of the repository you are pushing to')
.option('-p, --depth <depth>', 'depth for clone', ghpages.defaults.depth)
.option(
Expand Down Expand Up @@ -121,6 +126,7 @@ function main(args) {
git: options.git,
depth: options.depth,
dotfiles: !!options.dotfiles,
nojekyll: !!options.nojekyll,
add: !!options.add,
remove: options.remove,
remote: options.remote,
Expand Down
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ exports.publish = function publish(basePath, config, callback) {
})
.then((git) => {
log('Copying files');
if (options.nojekyll) {
fs.createFileSync(path.join(git.cwd, '.nojekyll'));
}
if (options.cname) {
fs.writeFileSync(path.join(git.cwd, 'CNAME'), options.cname);
}
return copy(files, basePath, path.join(git.cwd, options.dest)).then(
function () {
return git;
Expand Down
33 changes: 31 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ Example use of the `dotfiles` option:
ghpages.publish('dist', {dotfiles: true}, callback);
```

#### <a id="optionsnojekyll">options.nojekyll</a>
* type: `boolean`
* default: `false`

Write out a `.nojekyll` file to [bypass Jekyll on GitHub Pages](https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/).

Example use of the `nojekyll` option:

```js
/**
* The usage below will add a `.nojekyll` file to the output.
*/
ghpages.publish('dist', {nojekyll: true}, callback);
```

#### <a id="optionscname">options.cname</a>
* type: `string`

Write out a `CNAME` file with a custom domain name.

Example use of the `cname` option:

```js
/**
* The usage below will add a `CNAME` file to the output.
*/
ghpages.publish('dist', {cname: 'custom-domain.com'}, callback);
```


#### <a id="optionsadd">options.add</a>
* type: `boolean`
Expand Down Expand Up @@ -402,10 +431,10 @@ If `gh-pages` fails, you may find that you need to manually clean up the cache d

### Deploying to github pages with custom domain

Modify the deployment line to your deploy script if you use custom domain. This will prevent the deployment from removing the domain settings in GitHub.
Use the `--cname` option to create a `CNAME` file with the name of your custom domain. See [the GitHub docs](https://docs.github.com/en/pages/configuring-a-custom-domain-for-your-github-pages-site/managing-a-custom-domain-for-your-github-pages-site) for more detail.

```
echo your_cutom_domain.online > ./build/CNAME && gh-pages -d build"
gh-pages -d build --cname custom-domain.com"
```

### Deploying with GitHub Actions
Expand Down
5 changes: 5 additions & 0 deletions test/bin/gh-pages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ describe('gh-pages', () => {
dist: 'lib',
config: {dotfiles: true},
},
{
args: ['--dist', 'lib', '--nojekyll'],
dist: 'lib',
config: {nojekyll: true},
},
{
args: ['--dist', 'lib', '--dest', 'target'],
dist: 'lib',
Expand Down
38 changes: 38 additions & 0 deletions test/integration/cname.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const helper = require('../helper.js');
const ghPages = require('../../lib/index.js');
const path = require('path');

const fixtures = path.join(__dirname, 'fixtures');
const fixtureName = 'cname';

beforeEach(() => {
ghPages.clean();
});

describe('the --cname option', () => {
it('adds a CNAME file', (done) => {
const local = path.join(fixtures, fixtureName, 'local');
const expected = path.join(fixtures, fixtureName, 'expected');
const branch = 'gh-pages';

helper.setupRemote(fixtureName, {branch}).then((url) => {
const options = {
repo: url,
user: {
name: 'User Name',
email: '[email protected]',
},
cname: 'custom-domain.com',
};
ghPages.publish(local, options, (err) => {
if (err) {
return done(err);
}
helper
.assertContentsMatch(expected, url, branch)
.then(() => done())
.catch(done);
});
});
});
});
1 change: 1 addition & 0 deletions test/integration/fixtures/cname/expected/CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom-domain.com
1 change: 1 addition & 0 deletions test/integration/fixtures/cname/expected/hello-world.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
1 change: 1 addition & 0 deletions test/integration/fixtures/cname/local/hello-world.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
1 change: 1 addition & 0 deletions test/integration/fixtures/nojekyll/local/hello-world.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Empty file.
38 changes: 38 additions & 0 deletions test/integration/nojekyll.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const helper = require('../helper.js');
const ghPages = require('../../lib/index.js');
const path = require('path');

const fixtures = path.join(__dirname, 'fixtures');
const fixtureName = 'nojekyll';

beforeEach(() => {
ghPages.clean();
});

describe('the --nojekyll option', () => {
it('adds a .nojekyll file', (done) => {
const local = path.join(fixtures, fixtureName, 'local');
const expected = path.join(fixtures, fixtureName, 'expected');
const branch = 'gh-pages';

helper.setupRemote(fixtureName, {branch}).then((url) => {
const options = {
repo: url,
user: {
name: 'User Name',
email: '[email protected]',
},
nojekyll: true,
};
ghPages.publish(local, options, (err) => {
if (err) {
return done(err);
}
helper
.assertContentsMatch(expected, url, branch)
.then(() => done())
.catch(done);
});
});
});
});
38 changes: 38 additions & 0 deletions test/integration/nojekyllExists.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const helper = require('../helper.js');
const ghPages = require('../../lib/index.js');
const path = require('path');

const fixtures = path.join(__dirname, 'fixtures');
const fixtureName = 'nojekyll-exists';

beforeEach(() => {
ghPages.clean();
});

describe('the --nojekyll option', () => {
it('works even if the .nojekyll file already exists', (done) => {
const local = path.join(fixtures, fixtureName, 'local');
const expected = path.join(fixtures, fixtureName, 'expected');
const branch = 'gh-pages';

helper.setupRemote(fixtureName, {branch}).then((url) => {
const options = {
repo: url,
user: {
name: 'User Name',
email: '[email protected]',
},
nojekyll: true,
};
ghPages.publish(local, options, (err) => {
if (err) {
return done(err);
}
helper
.assertContentsMatch(expected, url, branch)
.then(() => done())
.catch(done);
});
});
});
});

0 comments on commit 55501ff

Please sign in to comment.