Skip to content

Instantly share code, notes, and snippets.

@msrose
Last active December 13, 2016 09:46
Show Gist options
  • Save msrose/d85140de7050752c9171c3c666039e4a to your computer and use it in GitHub Desktop.
Save msrose/d85140de7050752c9171c3c666039e4a to your computer and use it in GitHub Desktop.
Creates an NPM package project
set -e
PACKAGE_NAME=$1
GIT_REMOTE=$2
DESCRIPTION=$3
TEST_DIR=__tests__
if [[ -z $PACKAGE_NAME ]]; then
echo "No package name provided"
exit 1
fi
if [[ -z $GIT_REMOTE ]]; then
echo "No git remote provided"
exit 1
fi
if [[ -z $DESCRIPTION ]]; then
echo "No description provided"
exit 1
fi
echo "Node version"
NODE_VERSION=$(node -v)
echo $NODE_VERSION
NODE_MAJOR_VERSION=$(echo $NODE_VERSION | tr -d 'v' | cut -d '.' -f 1)
echo "NPM version"
NPM_VERSION=$(npm -v)
echo $NPM_VERSION
echo "Making package directory"
mkdir $PACKAGE_NAME
cd $PACKAGE_NAME
mkdir $TEST_DIR
echo "Initializing git repository"
git init .
git remote add origin $GIT_REMOTE
echo "Initializing package.json and dependencies"
npm init -y \
--init-version "0.0.1" \
--init-license "MIT" \
&> /dev/null
npm install --save-dev jest eslint eslint-config-msrose
echo "Updating package.json"
DESCRIPTION=$DESCRIPTION NODE_MAJOR_VERSION=$NODE_MAJOR_VERSION node --eval \
"var config = require('./package.json'); \
config.description = process.env.DESCRIPTION; \
config.scripts.test = 'jest'; \
config.scripts.lint = 'eslint .'; \
config.jest = { testPathIgnorePatterns: ['/node_modules/', '.eslintrc'] }; \
config = JSON.stringify(config, null, 2); \
console.log(config); \
require('fs').writeFileSync('./package.json', config);" \
echo "Writing index.js"
cat > index.js <<EOF
module.exports = '$PACKAGE_NAME';
EOF
echo "Writing $TEST_DIR/index.js"
cat > $TEST_DIR/index.js <<EOF
describe('$PACKAGE_NAME', () => {
it('runs a test', () => {
expect(1 + 1).toBe(2);
});
});
EOF
echo "Writing eslint files"
cat > .eslintrc.js <<EOF
module.exports = {
env: {
node: true,
es6: true
},
parserOptions: {
ecmaVersion: 6
},
extends: 'msrose'
};
EOF
cat > $TEST_DIR/.eslintrc.js <<EOF
module.exports = {
env: {
jest: true
}
};
EOF
echo "Writing gitignore"
cat > .gitignore <<EOF
node_modules
npm-debug.log
EOF
echo "Writing travis config"
cat > .travis.yml <<EOF
language: node_js
node_js:
- "$NODE_MAJOR_VERSION"
script:
- npm run lint
- npm test
EOF
echo "Writing npmignore"
cat > .npmignore <<EOF
__tests__
.eslintrc.js
.gitignore
.travis.yml
EOF
echo "Writing README"
cat > README.md <<EOF
# $PACKAGE_NAME
$DESCRIPTION
EOF
echo "Writing LICENSE"
cat > LICENSE <<EOF
MIT License
Copyright (c) $(date +"%Y") $(npm config get init-author-name)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
EOF
git add .
git commit -m "Set up package"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment