indexzero
3/5/2014 - 9:27 PM

npmjs.ini

#!/bin/bash

RELEASE=v2.0.3
BASEDIR=`basename $0`
SCRIPTPATH=`cd $(dirname $0) ; pwd -P`

CREATED=201
CONFLICT=409
PRECONDITION_FAILED=412

function assert
{
    if [ "$?" != "0" ]; then
        fail "${1:-"An unknown error occurred."}";
    fi
}

function fail
{
    clean
    echo "$BASEDIR: $1" 1>&2
    exit 1
}

function clean
{
    npm set _npmjs.org:couch=
    # Only try to clean up files if we made it past cloning
    if [ "$SCRIPTPATH" != `pwd` ]; then
        cd ../;
        rm -rf npmjs.org;
    fi
}



# Read hostname
echo -n "Hostname [localhost:5984]: "
read hostname

if [ -z $hostname ]; then hostname="localhost:5984"; fi


# Read DB name
echo -n "Database Name [registry]: "
read database

if [ -z $database ]; then database="registry"; fi


# Read Username
echo -n "CouchDB Username [admin]: "
read username

if [ -z $username ]; then username="admin"; fi


# Read Password
echo -n "CouchDB Password: "
read -s password
echo
echo


# Per: https://github.com/npm/npmjs.org

# Get the npm app from github
git clone git@github.com:npm/npmjs.org.git
assert "$LINENO: Unable to clone npmjs.org."

cd npmjs.org

# Get the latest release (don't use master)
git checkout tags/$RELEASE
assert "$LINENO: Unable to get release $RELEASE."

npm install
assert "$LINENO: npm install failed."


# Set the registry for use by the upcoming scripts
registry=http://$username:$password@$hostname/$database
npm set _npmjs.org:couch=$registry

# Create the database
res=`curl -s -w "%{http_code}" -X PUT $registry -o /dev/null`
assert "$LINENO: Unable to connect to $host."
if [ "$res" != "$CREATED" ] && [ "$res" != "$PRECONDITION_FAILED" ]; then fail "$LINENO: Unable to create database \"$database\". Server responded with status code $res"; fi


# XXX
# Patch env per https://github.com/npm/npmjs.org/commit/2f9dbb9d2f75c96183f9b0cbdbafaa790d380d29
export DEPLOY_VERSION=`git describe --tags`
# XXX

# Sync the ddoc
npm start
assert "$LINENO: npm start failed."

# Load views
npm run load
assert "$LINENO: Unable to load views."

# Ensure _design/app exists
res=`curl -s -w "%{http_code}" -X PUT -H 'Content-Type: application/json' -d '{}' $registry/_design/app -o /dev/null`
assert "$LINENO: Unable to connect to $host."
if [ "$res" != "$CREATED" ] && [ "$res" != "$CONFLICT" ]; then fail "$LINENO: Unable to create _design/app document. Server responded with status code $res"; fi

# Install app
npm run copy
assert "$LINENO: Unable to install couch app."

res=`curl -s -w "%{http_code}" -X PUT -H 'Content-Type: application/json' $registry/error%3A%20forbidden -d '{ "_id": "error: forbidden", "forbidden":"must supply latest _rev to update existing package" }' -o /dev/null`
assert "$LINENO: Unable to connect to $host"
if [ "$res" != "$CREATED" ] && [ "$res" != "$CONFLICT" ]; then fail "$LINENO: Unable to create error document. Server responded with status code $res"; fi

clean
; CouchDB Config
; Drop in PREFIX/local.d/npmjs.ini

[couch_httpd_auth]
public_fields = appdotnet, avatar, avatarMedium, avatarLarge, date, email, fields, freenode, fullname, github, homepage, name, roles, twitter, type, _id, _rev
users_db_public = true

[httpd]
secure_rewrites = false

[couchdb]
delayed_commits = false