diff --git a/bin/gh-pages.js b/bin/gh-pages.js index 477e1708..f2057c7b 100755 --- a/bin/gh-pages.js +++ b/bin/gh-pages.js @@ -63,7 +63,7 @@ function main(args) { '-v, --remove ', 'Remove files that match the given pattern ' + '(ignored if used together with --add).', - ghpages.defaults.only + ghpages.defaults.remove ) .option('-n, --no-push', 'Commit only (with no push)') .option( @@ -96,7 +96,7 @@ function main(args) { depth: program.depth, dotfiles: !!program.dotfiles, add: !!program.add, - only: program.remove, + remove: program.remove, remote: program.remote, push: !!program.push, history: !!program.history, diff --git a/lib/git.js b/lib/git.js index d4dac880..aa2b3ddc 100644 --- a/lib/git.js +++ b/lib/git.js @@ -66,7 +66,7 @@ function Git(cwd, cmd) { * or rejected with an error. */ Git.prototype.exec = function() { - return spawn(this.cmd, [].slice.call(arguments), this.cwd).then(output => { + return spawn(this.cmd, [...arguments], this.cwd).then(output => { this.output = output; return this; }); @@ -136,20 +136,26 @@ Git.prototype.checkout = function(remote, branch) { /** * Remove all unversioned files. - * @param {string} files Files argument. + * @param {string|string[]} files Files argument. * @return {Promise} A promise. */ Git.prototype.rm = function(files) { - return this.exec('rm', '--ignore-unmatch', '-r', '-f', files); + if (!Array.isArray(files)) { + files = [files]; + } + return this.exec('rm', '--ignore-unmatch', '-r', '-f', ...files); }; /** * Add files. - * @param {string} files Files argument. + * @param {string|string[]} files Files argument. * @return {Promise} A promise. */ Git.prototype.add = function(files) { - return this.exec('add', files); + if (!Array.isArray(files)) { + files = [files]; + } + return this.exec('add', ...files); }; /** diff --git a/lib/index.js b/lib/index.js index 333e6903..87d5c1d8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -31,7 +31,7 @@ exports.defaults = { branch: 'gh-pages', remote: 'origin', src: '**/*', - only: '.', + remove: '.', push: true, history: true, message: 'Updates', @@ -52,6 +52,11 @@ exports.publish = function publish(basePath, config, callback) { const options = Object.assign({}, exports.defaults, config); + // For backward compatibility before fixing #334 + if (options.only) { + options.remove = options.only; + } + if (!callback) { callback = function(err) { if (err) { @@ -94,10 +99,6 @@ exports.publish = function publish(basePath, config, callback) { return; } - const only = globby.sync(options.only, {cwd: basePath}).map(file => { - return path.join(options.dest, file); - }); - let repoUrl; let userPromise; if (options.user) { @@ -151,9 +152,18 @@ exports.publish = function publish(basePath, config, callback) { } }) .then(git => { - if (!options.add) { - log('Removing files'); - return git.rm(only.join(' ')); + if (options.add) { + return git; + } + + log('Removing files'); + const files = globby + .sync(options.remove, { + cwd: path.join(git.cwd, options.dest) + }) + .map(file => path.join(options.dest, file)); + if (files.length > 0) { + return git.rm(files); } else { return git; } diff --git a/test/integration/fixtures/remove/expected/new.js b/test/integration/fixtures/remove/expected/new.js new file mode 100644 index 00000000..ae98a2e4 --- /dev/null +++ b/test/integration/fixtures/remove/expected/new.js @@ -0,0 +1 @@ +// to be copied diff --git a/test/integration/fixtures/remove/expected/stable.txt b/test/integration/fixtures/remove/expected/stable.txt new file mode 100644 index 00000000..f17a1a91 --- /dev/null +++ b/test/integration/fixtures/remove/expected/stable.txt @@ -0,0 +1 @@ +This file should not be removed. diff --git a/test/integration/fixtures/remove/local/new.js b/test/integration/fixtures/remove/local/new.js new file mode 100644 index 00000000..ae98a2e4 --- /dev/null +++ b/test/integration/fixtures/remove/local/new.js @@ -0,0 +1 @@ +// to be copied diff --git a/test/integration/fixtures/remove/remote/removed.css b/test/integration/fixtures/remove/remote/removed.css new file mode 100644 index 00000000..ba18ac65 --- /dev/null +++ b/test/integration/fixtures/remove/remote/removed.css @@ -0,0 +1 @@ +/* to be removed */ diff --git a/test/integration/fixtures/remove/remote/removed.js b/test/integration/fixtures/remove/remote/removed.js new file mode 100644 index 00000000..8a118b90 --- /dev/null +++ b/test/integration/fixtures/remove/remote/removed.js @@ -0,0 +1 @@ +// to be removed diff --git a/test/integration/fixtures/remove/remote/stable.txt b/test/integration/fixtures/remove/remote/stable.txt new file mode 100644 index 00000000..f17a1a91 --- /dev/null +++ b/test/integration/fixtures/remove/remote/stable.txt @@ -0,0 +1 @@ +This file should not be removed. diff --git a/test/integration/remove.spec.js b/test/integration/remove.spec.js new file mode 100644 index 00000000..5afd745e --- /dev/null +++ b/test/integration/remove.spec.js @@ -0,0 +1,67 @@ +const helper = require('../helper'); +const ghPages = require('../../lib/'); +const path = require('path'); + +const fixtures = path.join(__dirname, 'fixtures'); +const fixtureName = 'remove'; + +beforeEach(() => { + ghPages.clean(); +}); + +describe('the remove option', () => { + it('removes matched files in remote branch', done => { + const local = path.join(fixtures, fixtureName, 'local'); + const expected = path.join(fixtures, fixtureName, 'expected'); + const branch = 'gh-pages'; + const remove = '*.{js,css}'; + + helper.setupRemote(fixtureName, {branch}).then(url => { + const options = { + repo: url, + user: { + name: 'User Name', + email: 'user@email.com' + }, + remove: remove + }; + + ghPages.publish(local, options, err => { + if (err) { + return done(err); + } + helper + .assertContentsMatch(expected, url, branch) + .then(() => done()) + .catch(done); + }); + }); + }); + + it('skips removing files if there are no files to be removed', done => { + const local = path.join(fixtures, fixtureName, 'remote'); + const branch = 'gh-pages'; + const remove = 'non-exist-file'; + + helper.setupRemote(fixtureName, {branch}).then(url => { + const options = { + repo: url, + user: { + name: 'User Name', + email: 'user@email.com' + }, + remove: remove + }; + + ghPages.publish(local, options, err => { + if (err) { + return done(err); + } + helper + .assertContentsMatch(local, url, branch) + .then(() => done()) + .catch(done); + }); + }); + }); +});