Snippets
Revised by
Mitch Allen
1acc0aa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | #!/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 "========================================================================"
|
You can clone a snippet to your computer for local editing. Learn more.