From 1f2435ddc936d0b58230ac6a2b0000521e543b91 Mon Sep 17 00:00:00 2001 From: Alban Mouton Date: Sun, 22 Jul 2018 23:28:30 +0200 Subject: [PATCH] add --no-history option --- bin/gh-pages-multi.js | 3 ++- docs/index.html | 2 +- lib/index.js | 43 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/bin/gh-pages-multi.js b/bin/gh-pages-multi.js index 3c82003..de87d7e 100755 --- a/bin/gh-pages-multi.js +++ b/bin/gh-pages-multi.js @@ -16,8 +16,9 @@ program .option('-t, --target [target]', 'The target directory that will be created or overwritten in the remote branch.', 'latest') .option('-r, --remote [remote]', 'The remote git repository to push to.', remote) .option('-b, --branch [branch]', 'The branch to push to. WARNING: this branch history will be overwritten.', 'gh-pages') - .option('-t, --template [template]', 'The pug template to use to generate the index.hml file.', path.join(__dirname, '../lib/index.pug')) + .option('--template [template]', 'The pug template to use to generate the index.hml file.', path.join(__dirname, '../lib/index.pug')) .option('--title [title]', 'The title of the generated index.html.', remote ? remote.split('/').pop().replace('.git', '') : 'gh-pages-multi') + .option('--no-history', 'Erase the history of the modified directory. Useful when re-creating multiple times a quite large directory with built files for example.') .option('--dry-run', 'Keep the cloned repository instead of cleaning it and do not push result to remote.') .option('-v, --verbose') .action(function (options) { diff --git a/docs/index.html b/docs/index.html index fb4a0cb..8fa3d93 100644 --- a/docs/index.html +++ b/docs/index.html @@ -6,6 +6,6 @@

gh-pages-multi demo

-

This page was published using gh-pages-multi Demo

+

This page was published using gh-pages-multi Demo.

diff --git a/lib/index.js b/lib/index.js index f57e7c5..bcef13c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,14 +3,16 @@ const util = require('util') const exec = util.promisify(require('child_process').exec) const ncp = util.promisify(require('ncp')) const rimraf = util.promisify(require('rimraf')) -const readdir = util.promisify(require('fs').readdir) -const readFile = util.promisify(require('fs').readFile) -const writeFile = util.promisify(require('fs').writeFile) +const fs = require('fs') +const readdir = util.promisify(fs.readdir) +const readFile = util.promisify(fs.readFile) +const writeFile = util.promisify(fs.writeFile) +const access = util.promisify(fs.access) const pug = require('pug') const tmp = require('tmp-promise') const debug = require('debug')('gh-pages-multi') -exports.deploy = async function ({src, target, branch, remote, template, title, dryRun}) { +exports.deploy = async function ({src, target, branch, remote, template, title, dryRun, history}) { debug(`deploy ${src} to ${remote}:${branch}/${target}`) const tmpDir = (await tmp.dir({keep: dryRun})).path @@ -27,8 +29,26 @@ exports.deploy = async function ({src, target, branch, remote, template, title, await exec(`git checkout --orphan ${branch}`, {cwd: tmpDir}) await exec(`git rm -rf .`, {cwd: tmpDir}) } + + let targetExists + try { + await access(path.resolve(tmpDir, target), fs.constants.F_OK) + targetExists = true + } catch (err) { + debug(`${target} does not exist yet`) + targetExists = false + } + if (targetExists) { + if (!history) { + debug(`remove all references to ${target} in git history`) + await exec(`git filter-branch --tree-filter 'rm -rf ${target}' --prune-empty HEAD`, {cwd: tmpDir}) + } + + debug(`remove previous directory ${target}`) + await rimraf(path.resolve(tmpDir, target)) + } + debug(`replace the directory ${target} with new content from ${src}`) - await rimraf(path.resolve(tmpDir, target)) await ncp(path.resolve(process.cwd(), src), path.resolve(tmpDir, target)) debug(`create index.html file that lists the directories in branch ${branch} from template ${template}`) @@ -40,8 +60,17 @@ exports.deploy = async function ({src, target, branch, remote, template, title, // Push everything if (!dryRun) { + if (history) { + res = await exec(`git diff --name-only`, {cwd: tmpDir}) + if (res.stdout.length === 0) return debug(`no modification to validate`) + } await exec(`git add -A`, {cwd: tmpDir}) - await exec(`git commit -m "Pushed by gh-pages-multi"`, {cwd: tmpDir}) - await exec(`git push -u origin ${branch}`, {cwd: tmpDir}) + await exec(`git commit -m "Pushed ${target} by gh-pages-multi"`, {cwd: tmpDir}) + if (history) { + await exec(`git push -u origin ${branch}`, {cwd: tmpDir}) + } else { + await exec(`git push --force -u origin ${branch}`, {cwd: tmpDir}) + } + debug(`pushed modifications to ${remote}:${branch}`) } }