Skip to content

Commit

Permalink
Merge pull request #355 from Xiphe/before-add
Browse files Browse the repository at this point in the history
feat(beforeAdd): allow custom script before git add
  • Loading branch information
tschaub authored May 29, 2020
2 parents 6f5ac52 + 1ef29dc commit 27af5eb
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 1 deletion.
24 changes: 23 additions & 1 deletion bin/gh-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ function main(args) {
'-f, --no-history',
'Push force new commit without parent history'
)
.option(
'--before-add <file>',
'Execute the function exported by <file> before "git add"'
)
.parse(args);

let user;
Expand All @@ -83,6 +87,23 @@ function main(args) {
}
user = {name: parts.name, email: parts.address};
}
let beforeAdd;
if (program.beforeAdd) {
const m = require(require.resolve(program.beforeAdd, {
paths: [process.cwd()]
}));

if (typeof m === 'function') {
beforeAdd = m;
} else if (typeof m === 'object' && typeof m.default === 'function') {
beforeAdd = m.default;
} else {
throw new Error(
`Could not find function to execute before adding files in ` +
`"${program.beforeAdd}".\n `
);
}
}

const config = {
repo: program.repo,
Expand All @@ -100,7 +121,8 @@ function main(args) {
remote: program.remote,
push: !!program.push,
history: !!program.history,
user: user
user: user,
beforeAdd: beforeAdd
};

return publish(config);
Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ exports.publish = function publish(basePath, config, callback) {
}
);
})
.then(git => {
return Promise.resolve(
options.beforeAdd && options.beforeAdd(git)
).then(() => git);
})
.then(git => {
log('Adding all');
return git.add('.');
Expand Down
29 changes: 29 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,35 @@ ghpages.publish('dist', {
```


#### <a id="optionsbeforeadd">options.beforeAdd</a>
* type: `function`
* default: `null`

Custom callback that is executed right before `git add`.

The CLI expects a file exporting the beforeAdd function

```bash
gh-pages --before-add ./cleanup.js
```

Example use of the `beforeAdd` option:

```js
/**
* beforeAdd makes most sense when `add` option is active
* Assuming we want to keep everything on the gh-pages branch
* but remove just `some-outdated-file.txt`
*/
ghpages.publish('dist', {
add: true,
async beforeAdd(git) {
return git.rm('./some-outdated-file.txt');
}
}, callback);
```


#### <a id="optionsgit">options.git</a>
* type: `string`
* default: `'git'`
Expand Down
3 changes: 3 additions & 0 deletions test/bin/fixtures/beforeAdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function myBeforeAdd() {
/* noop */
};
11 changes: 11 additions & 0 deletions test/bin/gh-pages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const ghpages = require('../../lib/index');
const sinon = require('sinon');
const cli = require('../../bin/gh-pages');
const assert = require('../helper').assert;
const beforeAdd = require('./fixtures/beforeAdd');

describe('gh-pages', () => {
describe('main', () => {
Expand Down Expand Up @@ -71,6 +72,16 @@ describe('gh-pages', () => {
dist: 'lib',
config: {user: {name: 'Full Name', email: 'email@example.com'}}
},
{
args: [
'--dist',
'lib',
'--before-add',
require.resolve('./fixtures/beforeAdd')
],
dist: 'lib',
config: {beforeAdd}
},
{
args: ['--dist', 'lib', '-u', 'junk email'],
dist: 'lib',
Expand Down
43 changes: 43 additions & 0 deletions test/integration/beforeAdd.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const helper = require('../helper');
const ghPages = require('../../lib/');
const path = require('path');

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

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

describe('the beforeAdd option', () => {
it('runs a provided async function before adding files', 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,
add: true,
beforeAdd(git) {
return Promise.resolve().then(() => {
return git.rm('hello-outdated-world.txt');
});
},
user: {
name: 'User Name',
email: 'user@email.com'
}
};
ghPages.publish(local, options, err => {
if (err) {
return done(err);
}
helper
.assertContentsMatch(expected, url, branch)
.then(() => done())
.catch(done);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Old World!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
1 change: 1 addition & 0 deletions test/integration/fixtures/beforeAdd/local/hello-world.txt
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 Old World!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Outdated World!

0 comments on commit 27af5eb

Please sign in to comment.