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

feat(beforeAdd): allow custom script before git add #355

Merged
merged 1 commit into from
May 29, 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
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 protected]'}}
},
{
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: '[email protected]'
}
};
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!