Skip to content

Commit

Permalink
add --no-history option
Browse files Browse the repository at this point in the history
  • Loading branch information
albanm committed Jul 22, 2018
1 parent a9deaed commit 1f2435d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
3 changes: 2 additions & 1 deletion bin/gh-pages-multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</head>
<body>
<h1>gh-pages-multi demo</h1>
<p>This page was published using <a href="https://github.com/albanm/gh-pages-multi">gh-pages-multi Demo</a></p>
<p>This page was published using <a href="https://github.com/albanm/gh-pages-multi">gh-pages-multi Demo.</a></p>
</body>
</html>
43 changes: 36 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`)
Expand All @@ -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}`)
}
}

0 comments on commit 1f2435d

Please sign in to comment.