Skip to content

Instantly share code, notes, and snippets.

@vipyne
Created July 20, 2017 19:37
Show Gist options
  • Save vipyne/f6de3184c3a940f88ede1d4d6f1f9b00 to your computer and use it in GitHub Desktop.
Save vipyne/f6de3184c3a940f88ede1d4d6f1f9b00 to your computer and use it in GitHub Desktop.
Bash script to generate a Node Module template.
#!/usr/bin/env bash
##
# author: @vipyne
# date: 2017-07-20
#
# usage:
# $ ./node_module_template <module name> <github username> <"Module description."> [create repo and push to github - leave blank if NO]
#
# TODO: add more robust error handling
# TODO: add function to search and replace all "xxxxxx" with module name
# TODO: handle adding dependencies ?
##
if [[ "${#@}" -lt 3 ]]; then
echo " ##"
echo " # Please add at least 3 args"
echo " # "
echo " # usage:"
echo " # $ ./node_module_template <module name> <github username> <Module description> [create repo and push to github - leave blank if NO]"
echo " ##"
exit
fi
MODULE_NAME="$1"
DESCRIPTION="$3"
PUSH_TO_GITHUB="$4"
if [[ "$PUSH_TO_GITHUB" ]]; then
USERNAME="$2"
DATA_OPTS="{ \"name\":\"$MODULE_NAME\", \"gitignore_template\": \"Node\", \"license_template\": \"mit\" }"
curl -i -X POST -u "$USERNAME" -d "$DATA_OPTS" \
https://api.github.com/user/repos > /dev/null
fi
mkdir "$MODULE_NAME" || exit
cd "$MODULE_NAME" || exit
touch README.md
touch index.js
touch "$MODULE_NAME.js"
mkdir test
touch test/index.js
touch test/"$MODULE_NAME.js"
mkdir example
touch example/index.js
touch example/index.html
## "$MODULE_NAME.js"
cat <<"CODE" > "$MODULE_NAME.js"
'use strict'
module.exports = XXXXXX
function XXXXXX(v = 0) {
console.log("your lovely code here.")
return true
}"
CODE
## index.js
cat <<"MAININDEX" > index.js
'use strict'
module.exports = {
XXXXXX: require('./xxxxxx'),
}
MAININDEX
## test/"$MODULE_NAME.js"
cat <<"TEST" > test/"$MODULE_NAME.js"
'use strict'
const test = require('tape')
test('xxxxxx', ({
assert,
pass,
plan,
end,
}) => {
plan(2)
assert(true)
if (true) {
pass('is true.')
}
end()
})
TEST
## test/index.js
cat <<"TESTINDEX" > test/index.js
'use strict'
require('./xxxxxx')
TESTINDEX
## example/index.html
cat <<"EXAMPLEHTML" > ./example/index.html
<!doctype html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>
EXAMPLEHTML
printf "%s" "$MODULE_NAME" >> ./example/index.html # `printf` to avoid newline
cat <<"EX2" >> ./example/index.html
example</title>
<body>
<script type="text/javascript" src="/index.js"></script>
</body>
</html>
EX2
## example/index.js
cat <<"EXAMPLEJS" > example/index.js
'use strict'
xxxxxx // add example code here
EXAMPLEJS
## README.md
echo "# $MODULE_NAME" > README.md
echo "" >> README.md
echo "$DESCRIPTION" >> README.md # also becomes default module description in package.json
cat <<"README" >> README.md
## Usage
#### `xxxxxx`
## Installation
Download or clone this repo and run:
```
npm install
npm run example
```
## Example
```javascript
'use strict'
xxxxxx // add example code here
```
## API
### FunctionName
#### Parameters:
`x` - x, defaults to 0<br>
`y` - y, defaults to '[a,b,c]'<br>
#### Returns:
```
returnFunction() || {...} || whatevs
```
README
git init
git add .
git commit -m 'initial node module template commit'
wait
git remote add origin git@github.com:littlstar/"$MODULE_NAME".git
npm init
# npm install tape --save-dev # uncomment if desired
git add .
git commit --ame --no-edit
if [[ "$PUSH_TO_GITHUB" ]]; then
wait
git pull origin master
git push -u origin master
fi
echo ""
echo ""
echo ""
echo ""
echo " success"
echo ""
echo ""
echo " *high five*"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment