Snippets

Mitch Allen Node Starter Script

Created by Mitch Allen last modified
#!/bin/bash

# chmod +x (this script)

if [[ $# -eq 0 ]]; then
	echo "!!! Usage: node-starter.sh package-name"
	exit 1
fi

# Set package folder

packageName=$1

# Initialize Variables

BITBUCKET_USER=mitchallen
BITBUCKET_IS_PRIVATE=false
GITHUB_USER=mitchallen
GITHUB_IS_PRIVATE=false
NPM_SCOPE=mitchallen
AUTHOR_NAME='Mitch Allen'
# need to escape slashes in url for sed command
npmAuthor="$AUTHOR_NAME <npm@mitchallen.com> (http:\/\/mitchallen.com)"

# Create folder if it doesn't exist

function CreatePackageFolder {

	echo "*** $FUNCNAME $1"

	if [[ $# -ne 1 ]]; then
		echo "!!! Usage: $FUNCNAME folder-name "
		exit 1
	fi

	local __folder=$1

	printf '... PACKAGE NAME: %s\n' "$__folder"

	if [ -d $__folder ]; then
		echo "!!! ERROR: folder '$__folder' already exists"
		exit 1
	else
		echo "... Folder '$__folder' does not exist, creating ..."
		mkdir $__folder 
		echo "... Changing to subfolder '$__folder'"
		cd $__folder
	fi
}


function CreateSubfolders {

	echo "*** $FUNCNAME"

	# Create subfolders
	echo "... Creating subfolder 'test'"
	mkdir test

	echo "... Creating subfolder 'examples'"
	mkdir examples
}


function DemandSuccess {

	# Don't show argument 2 (error message)
	# Logging error message argument in call looks like a fail in the log
	echo "*** $FUNCNAME $1"

	if [[ $# -ne 2 ]]; then
		echo "!!! Usage: $FUNCNAME result message "
		exit 1
	fi

	__result=$1
	__message=$2

	if [ $__result -eq 0 ]; then
    	echo "... OK"
	else
    	echo "!!! ERROR: $__message"
    	exit 1
	fi

}

function CloneStarterKit {

	echo "*** $FUNCNAME"

	# Clone Kit Files

	git clone http://bitbucket.org/mitchallen/node-starter-kit.git

	DemandSuccess $? "Could not clone starter kit"
}

function CopyCloneFiles {

	echo "*** $FUNCNAME $1"

	if [[ $# -ne 1 ]]; then
		echo "!!! Usage: $FUNCNAME clone-folder "
		exit 1
	fi

	__cloneFolder=$1

	if [ -d $__cloneFolder ]; then
		echo "... VERIFYING: found clone folder '$__cloneFolder'"
	else
		echo "!!! ERROR: Clone folder '$__cloneFolder' does not exist!"
		exit 1
	fi

	echo "... Copying clone files"

	# copy files from clones kit folder
	cp $__cloneFolder/kit/Gruntfile.js .
	cp $__cloneFolder/kit/package.json .
	cp $__cloneFolder/kit/index.js .
	cp $__cloneFolder/kit/smoke-test.js test/smoke-test.js
	cp $__cloneFolder/kit/npmignore.txt .npmignore
	cp $__cloneFolder/kit/README.template README.md

	# remove clone folder
	echo "... Removing clone folder '$__cloneFolder'"
	rm -rf $__cloneFolder
}

function DemandFile {

	echo "*** $FUNCNAME $1"

	if [[ $# -ne 1 ]]; then
		echo "!!! Usage: $FUNCNAME file"
		exit 1
	fi

	__demandFile=$1

	if [ ! -f $__demandFile ]; then
    	echo "!!! ERROR: Can't find file: $__demandFile"
    	exit 1
	fi
}

function FillTemplates {

	echo "*** $FUNCNAME"

	# Update fields in cloned files


	#  File: index.js

	DemandFile "index.js"

	echo "... Updating index.js"

	sed -i .bak "s/NPM_SCOPE/$NPM_SCOPE/g" index.js
	sed -i .bak "s/PACKAGE_NAME/$packageName/g" index.js
	sed -i .bak "s/AUTHOR_NAME/$AUTHOR_NAME/g" index.js

	cat index.js


	#  File: test/smoke-test.js

	DemandFile "test/smoke-test.js"

	echo "... updating test/smoke-test.js"

	sed -i .bak "s/NPM_SCOPE/$NPM_SCOPE/g" test/smoke-test.js
	sed -i .bak "s/PACKAGE_NAME/$packageName/g" test/smoke-test.js
	sed -i .bak "s/AUTHOR_NAME/$AUTHOR_NAME/g" test/smoke-test.js

	cat test/smoke-test.js


	#  File: package.json

	DemandFile "package.json"

	echo "... Updating package.json"

	sed -i .bak "s/NPM_SCOPE/$NPM_SCOPE/g" package.json
	sed -i .bak "s/PACKAGE_NAME/$packageName/g" package.json
	sed -i .bak "s/BITBUCKET_USER/$BITBUCKET_USER/g" package.json
	sed -i .bak "s/GITHUB_USER/$GITHUB_USER/g" package.json
	sed -i .bak "s/NPM_AUTHOR/$npmAuthor/g" package.json

	cat package.json


	#  File: README.md

	DemandFile "README.md"

	echo "... Updating README.md"

	sed -i .bak "s/NPM_SCOPE/$NPM_SCOPE/g" README.md
	sed -i .bak "s/PACKAGE_NAME/$packageName/g" README.md
	sed -i .bak "s/BITBUCKET_USER/$BITBUCKET_USER/g" README.md
	sed -i .bak "s/GITHUB_USER/$GITHUB_USER/g" README.md
	sed -i .bak "s/NPM_AUTHOR/$npmAuthor/g" README.md

	cat README.md


	echo "... Removing sed backup files"

	rm *.bak
	rm test/*.bak
}

function InitNpm {

	echo "*** $FUNCNAME"

	# Initialize NPM

	echo "... $ npm install"

	npm install

	DemandSuccess $? "'npm install' failed"
}

function InitGit {

	echo "*** $FUNCNAME"

	# Init git

	echo "... $ git init"
	git init
	DemandSuccess $? "'git init' failed"

    echo "... $ git add ."
	git add .
	DemandSuccess $? "'git add' failed"

	echo "... $ git commit -m 'init commit'"
	git commit -m 'init commit'
	DemandSuccess $? "'git commit' failed"
}

function CreateBitbucketRepo {

	echo "*** $FUNCNAME"

	if [[ $# -ne 4 ]]; then
		echo "!!! Usage: CreateBitbucketRepo bb-user is_private package-name language"
		exit 1
	fi

	__bbUser=$1
	__bbIsPrivate=$2
	__packageName=$3
	__language=$4

	# Create Bitbucket Repo (THIS WILL BE PUBLIC BY DEFAULT)
	# Will ask user for password
	echo "========================================================================"
	echo "*"
	echo "*  Enter your BITBUCKET password at the prompt: "
	echo "*"

	# OLD Bitbucket 1.0 way
	# curl --user $BITBUCKET_USER https://api.bitbucket.org/1.0/repositories --data name=$packageName

	# Note that the language string must be all lowercase
	curl -X POST -v -u $__bbUser  -H "Content-Type: application/json" \
	  https://api.bitbucket.org/2.0/repositories/$__bbUser/$__packageName \
	  -d "{\"scm\": \"git\", \"is_private\": \"$__bbIsPrivate\", \"language\": \"$__language\" }"

	DemandSuccess $? "'curl command failed"

	# Add Bitbuucket remote
	echo "."
	echo "... $ git remote add origin git@bitbucket.org:$__bbUser/$__packageName.git"
	git remote add origin git@bitbucket.org:$__bbUser/$__packageName.git
	DemandSuccess $? "'git remote add origin' failed"

	echo "... $ git remote -v show"
	git remote -v show
	DemandSuccess $? "'git remote -v show' failed"
}

function CreateGitHubRepo {

	echo "*** $FUNCNAME"

	if [[ $# -ne 4 ]]; then
		echo "!!! Usage: CreateGitHubRepo gh-user is_private package-name language"
		exit 1
	fi

	__ghUser=$1
	__ghIsPrivate=$2
	__packageName=$3
	__language=$4

	# Create Github Repo (THIS WILL BE PUBLIC BY DEFAULT)
	# Will ask user for password
	echo "========================================================================"
	echo "*"
	echo "*  Enter your GITHUB password at the prompt: "
	echo "*"

	curl -u $__ghUser https://api.github.com/user/repos -d "{\"name\":\"$__packageName\" }"

	DemandSuccess $? "'curl command failed"

	# Reference: http://blog.kevinlee.io/2013/03/11/git-push-to-pull-from-both-github-and-bitbucket/

	# Append Github remote to origin
	echo "... $ git remote set-url origin --add git@github.com:$__ghUser/$__packageName.git"
	git remote set-url origin --add git@github.com:$__ghUser/$__packageName.git 
	DemandSuccess $? "'git remote set-url origin --add' failed"

	echo "... $ git remote -v show"
	git remote -v show
	DemandSuccess $? "'git remote -v show' failed"
}

function GitPushSetup {

	echo "*** $FUNCNAME"

	echo "... $git push -u origin --all"
	git push -u origin --all # pushes up the repo and its refs for the first time
	DemandSuccess $? "'push -u origin --all' failed"

	echo "... $ git push origin --tags"
	git push origin --tags # pushes up any tags
	DemandSuccess $? "'push origin --tags' failed"
}

function CallGrunt {

	echo "*** $FUNCNAME"

	# Run grunt (to do a lint check)
	echo "... $ grunt"
	grunt
	DemandSuccess $? "'grunt' failed"
}

function RunTests {

	echo "*** $FUNCNAME"

	echo "... $ npm test"
	npm test
	DemandSuccess $? "'npm test' failed"
}

function Main {

	echo "*** $FUNCNAME"

	CreatePackageFolder $packageName
	CreateSubfolders
	CloneStarterKit 
	# clone folder must match clone repo name
	CopyCloneFiles "node-starter-kit"
	FillTemplates
	InitNpm
	InitGit
	# Language must be lowercase ("javascript")
	CreateBitbucketRepo $BITBUCKET_USER $BITBUCKET_IS_PRIVATE $packageName "javascript"
	CreateGitHubRepo $GITHUB_USER $GITHUB_IS_PRIVATE $packageName "javascript"
	GitPushSetup
	CallGrunt
	RunTests

	# Inform User

	echo "========================================================================"
	echo "*"
	echo "*   New starter project created in $packageName"
	echo "*"
	echo "*   Created $packageName on Bitbucket and Github"
	echo "*"
	echo "*   WARNING: repos may be public" 
	echo "*"
	echo "========================================================================"
}

Main


Comments (0)