Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Dec 30, 2018
0 parents commit f3e1258
Show file tree
Hide file tree
Showing 5 changed files with 599 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
59 changes: 59 additions & 0 deletions README.md
@@ -0,0 +1,59 @@
# snoot.club/post-receive

a post receive deploy hook for snoots

## usage

put `npx @snootclub/post-receive` in your serve-side repo's `hooks/post-receive`

## snoot usage

for default snoot behaviour you can do something like this inside your snoot container:

```sh
git init --bare /repo
>/repo/hooks/post-receive cat <<.
#!/bin/sh
npx @snootclub/post-receive
.
chmod +x /repo/hooks/post-receive
cd /application
git remote add origin /repo
git add .
git push
```

now you'll be able to do this on your local machine:

```sh
git clone ssh://snoot.club:$MY_PORT/repo snoot
```

and when you push up any changes in `snoot` it will install them,
build them and make them live.

## advanced usage

this isn't really specific to snoots and there are some options avaiable

### --deploymentDirectory

the directory to use as the deployment target, where files are served from
by the webserver.

default `/application`

### --deploymentBranch

the only brank acceptable to deploy from. if changes are pushed to any branch
other than this, they won't be deployed ^_^

default `master`

### --repoDirectory

the source bare repo directory used when deploying. by default this uses the
directory the process is running from, which will be the current repo when
used in a git hook :)

default `process.cwd()`
83 changes: 83 additions & 0 deletions index.js
@@ -0,0 +1,83 @@
#!/usr/bin/env node
let getStream = require("get-stream")
let yargs = require("yargs")
let execa = require("execa")
let path = require("path")
let fs = require("fs-extra")

let {
argv
} = yargs.options({
deploymentDirectory: {
alias: ["d", "directory"],
default: "/application",
type: "string"
},
deploymentBranch: {
alias: ["b", "branch", "brank"],
default: "master",
type: "string"
},
repoDirectory: {
alias: ["r", "repo"],
// the post-receive runs in the repo
default: process.cwd(),
type: "string"
}
})

void async function postーreceive () {
let input = await getStream(process.stdin)

// post-receive gets called with `${previousRevisionHash} ${newRevisionHash} ${ref}`
let [, , ref] = input.split(/\s+/)
// ref takes the form `/refs/heads/${name}`
let [, brank] = ref.match(/refs\/heads\/(.*)/)


if (brank != argv.brank) {
return console.log(`not going to make ${brank} live. only ${argv.brank} is made live.`)
}

// cool, we have new code in the live branch !!
if (!await fs.pathExists(argv.deploymentDirectory)) {
console.warn(`warn: deploy directory "${argv.deploymentDirectory}" didn't exist. making it`)
await fs.mkdirp(argv.deploymentDirectory)
}

await execa("git", [
`--work-tree=${argv.deploymentDirectory}`,
`--git-dir=${argv.repo}`,
"checkout",
"-f",
brank
])

let packageJsonPath = path.resolve(argv.deploymentDirectory, "package.json")

let hasPackageJson = await fs.pathExists(packageJsonPath)

if (!hasPackageJson) {
return console.warn("no package.json so considering this snoot deployed")
}

await execa("npm", [
"install",
`--prefix=${argv.deploymentDirectory}`
])

let package = require(packageJsonPath)

if (!package.scripts || !package.scripts.build) {
return console.warn("no package.json#scripts#build so considering this snoot deployed!!")
}

await execa("npm", [
"run-script",
`--prefix=${argv.deploymentDirectory}`,
"build"
])

console.log("snoot deployed, installed && built !!")
}()

0 comments on commit f3e1258

Please sign in to comment.