From 0d52b352c11870720b10f87f51270eb72680e703 Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Tue, 25 Feb 2020 20:09:00 +0900 Subject: [PATCH 01/10] Skip removing files if no files need to be removed. --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 333e6903..4338bce2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -151,7 +151,7 @@ exports.publish = function publish(basePath, config, callback) { } }) .then(git => { - if (!options.add) { + if (!options.add && only.length > 0) { log('Removing files'); return git.rm(only.join(' ')); } else { From 2e6e71238b511b14a2afa8fb5ff0e9036c2aea3c Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 26 Feb 2020 11:27:55 +0900 Subject: [PATCH 02/10] Move declaration of removing files. --- lib/index.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4338bce2..6736ec42 100644 --- a/lib/index.js +++ b/lib/index.js @@ -94,10 +94,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 +147,18 @@ exports.publish = function publish(basePath, config, callback) { } }) .then(git => { - if (!options.add && only.length > 0) { + if (!options.add) { log('Removing files'); - return git.rm(only.join(' ')); + const only = globby + .sync(options.only, { cwd: basePath }) + .map(file => { + return path.join(options.dest, file); + }); + if (only.length > 0) { + return git.rm(only.join(' ')); + } else { + return git; + } } else { return git; } From fda1862ce76e8d37745b9893a5dfceaf691b4030 Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 26 Feb 2020 11:31:27 +0900 Subject: [PATCH 03/10] Change order. --- lib/index.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6736ec42..724841bb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -147,18 +147,16 @@ exports.publish = function publish(basePath, config, callback) { } }) .then(git => { - if (!options.add) { - log('Removing files'); - const only = globby - .sync(options.only, { cwd: basePath }) - .map(file => { - return path.join(options.dest, file); - }); - if (only.length > 0) { - return git.rm(only.join(' ')); - } else { - return git; - } + if (options.add) { + return git; + } + + log('Removing files'); + const only = globby.sync(options.only, { cwd: basePath }).map(file => { + return path.join(options.dest, file); + }); + if (only.length > 0) { + return git.rm(only.join(' ')); } else { return git; } From dcdbf74fd27ddff9c6c404cceb173b428d7e7d8b Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 26 Feb 2020 11:47:28 +0900 Subject: [PATCH 04/10] Remove files in remote branch. --- lib/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 724841bb..7fcde49c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -152,11 +152,11 @@ exports.publish = function publish(basePath, config, callback) { } log('Removing files'); - const only = globby.sync(options.only, { cwd: basePath }).map(file => { - return path.join(options.dest, file); + const files = globby.sync(options.only, { + cwd: path.join(git.cwd, options.dest) }); - if (only.length > 0) { - return git.rm(only.join(' ')); + if (files.length > 0) { + return git.rm(files.join(' ')); } else { return git; } From be7bb477c69fdff4adf6a2b0e5c379641ff66898 Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 26 Feb 2020 12:00:00 +0900 Subject: [PATCH 05/10] Rename options.only to options.remove --- bin/gh-pages.js | 6 +++--- lib/index.js | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bin/gh-pages.js b/bin/gh-pages.js index 477e1708..6171f66f 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( @@ -81,7 +81,7 @@ function main(args) { '(format should be "Your Name ")' ); } - user = {name: parts.name, email: parts.address}; + user = { name: parts.name, email: parts.address }; } const config = { @@ -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/index.js b/lib/index.js index 7fcde49c..e7f2fb14 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) { @@ -152,7 +157,7 @@ exports.publish = function publish(basePath, config, callback) { } log('Removing files'); - const files = globby.sync(options.only, { + const files = globby.sync(options.remove, { cwd: path.join(git.cwd, options.dest) }); if (files.length > 0) { From 0c5f446a0e8ba5a603c62fb15561bc36feafef80 Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 26 Feb 2020 12:23:47 +0900 Subject: [PATCH 06/10] Create .prettierrc. --- .prettierrc | 5 +++++ bin/gh-pages.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..c7c53446 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "bracketSpacing": false, + "tabWidth": 2, + "singleQuote": true +} diff --git a/bin/gh-pages.js b/bin/gh-pages.js index 6171f66f..f2057c7b 100755 --- a/bin/gh-pages.js +++ b/bin/gh-pages.js @@ -81,7 +81,7 @@ function main(args) { '(format should be "Your Name ")' ); } - user = { name: parts.name, email: parts.address }; + user = {name: parts.name, email: parts.address}; } const config = { From 5443b3d8ed07b5469d6bf292d94887e4cd859302 Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 26 Feb 2020 12:54:37 +0900 Subject: [PATCH 07/10] Append dest in removing files. --- lib/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index e7f2fb14..b155d007 100644 --- a/lib/index.js +++ b/lib/index.js @@ -157,9 +157,11 @@ exports.publish = function publish(basePath, config, callback) { } log('Removing files'); - const files = globby.sync(options.remove, { - cwd: path.join(git.cwd, options.dest) - }); + 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.join(' ')); } else { From 66f3ddbc2d820a74208d5f29ac3dd5678a80e92f Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 26 Feb 2020 13:32:24 +0900 Subject: [PATCH 08/10] Fix argument passing. --- lib/git.js | 16 +++++++++++----- lib/index.js | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) 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 b155d007..87d5c1d8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -163,7 +163,7 @@ exports.publish = function publish(basePath, config, callback) { }) .map(file => path.join(options.dest, file)); if (files.length > 0) { - return git.rm(files.join(' ')); + return git.rm(files); } else { return git; } From 878fe5c18ed64740d92c61820fae2c924652121a Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Wed, 26 Feb 2020 14:02:51 +0900 Subject: [PATCH 09/10] Add options.remove tests. --- .../fixtures/remove/expected/new.js | 1 + .../fixtures/remove/expected/stable.txt | 1 + test/integration/fixtures/remove/local/new.js | 1 + .../fixtures/remove/remote/removed.css | 1 + .../fixtures/remove/remote/removed.js | 1 + .../fixtures/remove/remote/stable.txt | 1 + test/integration/remove.spec.js | 67 +++++++++++++++++++ 7 files changed, 73 insertions(+) create mode 100644 test/integration/fixtures/remove/expected/new.js create mode 100644 test/integration/fixtures/remove/expected/stable.txt create mode 100644 test/integration/fixtures/remove/local/new.js create mode 100644 test/integration/fixtures/remove/remote/removed.css create mode 100644 test/integration/fixtures/remove/remote/removed.js create mode 100644 test/integration/fixtures/remove/remote/stable.txt create mode 100644 test/integration/remove.spec.js 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); + }); + }); + }); +}); From 0522cafaf2275c8d7c70b748f24afda7460730ab Mon Sep 17 00:00:00 2001 From: Sunghwan Bang Date: Tue, 3 Mar 2020 02:09:12 +0900 Subject: [PATCH 10/10] Delete .prettierrc --- .prettierrc | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index c7c53446..00000000 --- a/.prettierrc +++ /dev/null @@ -1,5 +0,0 @@ -{ - "bracketSpacing": false, - "tabWidth": 2, - "singleQuote": true -}