Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
albanm committed Jul 20, 2018
1 parent 6016bbc commit a9deaed
Show file tree
Hide file tree
Showing 8 changed files with 2,242 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
root: true,
extends: [
// https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style
'standard'
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
48 changes: 48 additions & 0 deletions bin/gh-pages-multi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node

const path = require('path')
const program = require('commander')

const packageInfo = require('../package')
const remote = require('remote-origin-url').sync()

program
.version(packageInfo.version)

program
.command('deploy')
.description('Push a directory from your fs into a directory of a remote git branch. Also build index.html listing the directories in the branch.')
.option('-s, --src [src]', 'The source directory to copy into the remote branch.', 'docs')
.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('--title [title]', 'The title of the generated index.html.', remote ? remote.split('/').pop().replace('.git', '') : 'gh-pages-multi')
.option('--dry-run', 'Keep the cloned repository instead of cleaning it and do not push result to remote.')
.option('-v, --verbose')
.action(function (options) {
if (options.dryRun) options.verbose = true
if (options.verbose) process.env.DEBUG = process.env.DEBUG ? process.env.DEBUG + ',gh-pages-multi' : 'gh-pages-multi'
require('../lib').deploy(options).then(cb, cbError)
})

if (!process.argv.slice(2).length) program.help()

program.parse(process.argv)

function cb () {
process.exit()
}

function cbError (err) {
console.error(err)
process.exit(1)
}

/*
ghPagesMulti({src: process.argv[2], target: process.argv[3], branch: process.argv[4], template: process.argv[5], remote: process.argv[6]})
.then(() => process.exit(), (err) => {
console.error(err)
process.exit(1)
})
*/
11 changes: 11 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>gh-pages-multi demo</title>
</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>
</body>
</html>
47 changes: 47 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require('path')
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 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}) {
debug(`deploy ${src} to ${remote}:${branch}/${target}`)

const tmpDir = (await tmp.dir({keep: dryRun})).path
// Is the target branch new or already created ?
let res = await exec(`git ls-remote --heads ${remote} ${branch}`)
const branchExists = res.stdout.indexOf(`refs/heads/${branch}`) !== -1
if (branchExists) {
debug(`branch ${branch} exists, clone it.`)
await exec(`git clone --single-branch -b ${branch} ${remote} ${tmpDir}`)
} else {
debug(`branch ${branch} doesn't exist yet, create it.`)
// Create empty new branch
await exec(`git clone ${remote} ${tmpDir}`)
await exec(`git checkout --orphan ${branch}`, {cwd: tmpDir})
await exec(`git rm -rf .`, {cwd: tmpDir})
}
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}`)
const dirs = (await readdir(tmpDir)).filter(dir => dir.indexOf('.') !== 0 && dir !== 'index.html').sort()
const compiledTemplate = pug.compile(await readFile(template, 'utf8'))
const fullTemplatePath = path.resolve(tmpDir, 'index.html')
await writeFile(fullTemplatePath, compiledTemplate({dirs, title}))
debug(`written ${fullTemplatePath}`)

// Push everything
if (!dryRun) {
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})
}
}
26 changes: 26 additions & 0 deletions lib/index.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
doctype html
html(lang='en')
head
title= title
link(href='https://fonts.googleapis.com/css?family=Nunito', rel='stylesheet')
style(type='text/css').
body {
font-family: 'Nunito', sans-serif;
color: rgba(0, 0, 0, 0.87);
background-color: #FAFAFA;
}
h1 {
font-size: 45px;
font-weight: 500;
text-align: center;
}
li {
list-style-type: none;
text-align: center;
}
body
h1= title
ul
each dir in dirs
li
a(href='./' + dir)= dir
Loading

0 comments on commit a9deaed

Please sign in to comment.