Snippets

Mitch Allen Node Starter Script

You are viewing an old version of this snippet. View the current version.
Revised by Mitch Allen 1acc0aa
#!/bin/bash

# chmod +x (this script)

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

# Setup package folder

packageName=$1

printf 'PACKAGE NAME: %s\n' "$packageName"

if [ -d $packageName ]; then
	echo "ERROR: folder ${packageName} already exists"
	exit 1
else
	echo "Folder ${packageName} does not exist"
	mkdir $packageName 
fi

# 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)"


# Change to the package folder

cd $packageName

# Create subfolders

mkdir test
mkdir examples

# Init: index.js

echo "//  Author: ${AUTHOR_NAME}" >> index.js
echo "// Package: @${NPM_SCOPE}/${packageName}" >> index.js

# Clone Kit Files

# must match name of cloned repo below 
cloneFolder="node-starter-kit"

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

# 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
rm -rf $cloneFolder

# Update fields in cloned files

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

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

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

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

rm *.bak
rm test/*.bak

cat package.json

# Initialize NPM

npm install

# Init git

git init
git add .
git commit -m 'init commit'

# 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 $BITBUCKET_USER  -H "Content-Type: application/json" \
  https://api.bitbucket.org/2.0/repositories/$BITBUCKET_USER/$packageName \
  -d "{\"scm\": \"git\", \"is_private\": \"$BITBUCKET_IS_PRIVATE\", \"language\": \"javascript\" }"

# 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 $GITHUB_USER https://api.github.com/user/repos -d "{\"name\":\"$packageName\" }"

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

# Add Bitbuucket remote
git remote add origin git@bitbucket.org:$BITBUCKET_USER/$packageName.git

# Append Github remote to origin
git remote set-url origin --add git@github.com:$GITHUB_USER/$packageName.git 

git remote -v show

git push -u origin --all # pushes up the repo and its refs for the first time
git push origin --tags # pushes up any tags

# Run grunt (to do a lint check)
grunt

# Verify testing is setup

npm test

# 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 "========================================================================"
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.