I have rewritten @rlkoshak’s library bash script to create helper javascript libraries.
It’s probably not perfect but I thought I’d make some improvements, namely:
- The script now asks for the name of the library you wish to create, instead of having to type it into the command line.
- The script better formats the outputted file (there were leading spaces which just annoyed the hell out me).
- The script creates the library with a file matching your input (instead of
index.js). - Replaced the first variable declaration to be the name of the Library (libName).
- Cleaned up the temp folder created inside the
tmpdirectory.
How to use:
- SSH into your terminal.
sudo nano createlib.sh(or whatever you want to call the utility).- Paste the following code into the file:
#!/bin/bash
# get library anme
read -p "Enter library name: " libname
# initialise directories
mkdir /tmp/$libname
cd /tmp/$libname
# build npm custom file
f='{"version":"1.0.0",'
f+='"private":true,'
f+='"main":"'
f+=$libname
f+='.js",'
f+='"scripts":{"test":"npx jest"}'
f+='}'
# override index.js
echo $f >> "./package.json"
# initialise npm
npm init
# build sample library
f="var libName = '${libname}'\n"
f+="function someFunction() {\n"
f+="\tconsole.log('Hello from your personal library!');\n"
f+="}\n\n"
f+="module.exports = {\n"
f+="\tlibName,\n"
f+="\tsomeFunction\n"
f+="};\n"
# output library to file
echo -e $f >> $libname.js
# pack the library
npm pack
# install the library
cd /$OPENHAB_CONF/automation/js
npm install /tmp/$libname/$libname-1.0.0.tgz
# clean up temp files
rm -rf /tmp/$libname
Ctrl-Xto save (you can change the filename here too, if you want).sudo chmod +x makelib.sh(this grants execution rights).
Usage:
- SSH into your terminal.
- Type
./makelib.sh - Enter the name of the library you wish to create.
- Follow the prompts for the
npmpackager (in most cases you can simply press enter repeatedly).
DO NOT CHANGE THEversion!! The script assumes 1.0.0! (not sure what I can do about this tbh).
I hope somebody finds this useful. Again, thanks to @rlkoshak for putting me onto this.
EDIT: Tweaked to put the generated script file as the entry point.