Bash script for helper libraries

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:

  1. The script now asks for the name of the library you wish to create, instead of having to type it into the command line.
  2. The script better formats the outputted file (there were leading spaces which just annoyed the hell out me).
  3. The script creates the library with a file matching your input (instead of index.js).
  4. Replaced the first variable declaration to be the name of the Library (libName).
  5. Cleaned up the temp folder created inside the tmp directory.

How to use:

  1. SSH into your terminal.
  2. sudo nano createlib.sh (or whatever you want to call the utility).
  3. 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

  1. Ctrl-X to save (you can change the filename here too, if you want).
  2. sudo chmod +x makelib.sh (this grants execution rights).

Usage:

  1. SSH into your terminal.
  2. Type ./makelib.sh
  3. Enter the name of the library you wish to create.
  4. Follow the prompts for the npm packager (in most cases you can simply press enter repeatedly).
    DO NOT CHANGE THE version!! 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.

The index.js is not arbitrary. It actually serves a purpose. It acts as the entrypoint for your library. It prevents you from needing to require each file individually every time.

The most concise explanations for it can be found at https://stackoverflow.com/questions/21063587/what-is-index-js-used-for-in-node-js-projects. If you have more than just one or two .js files, you may want index.js to provide a way to require a whole directory in one line instead of needing to require each file separately.

That’s why the docs show an example in index.js.

So, for example, I have a personal library rlk_personal with two library files: alert.js and util.js. I have index.js with the following contents.

module.exports = {
  get alerting() { return require('./alerting.js') },
  get utils() { return require('./utils.js') }
}

This way I can:

const personal = require('rlk_personal'); // import the whole library
const {alerting, utils} = require('rlk_personal'); // import the whole library but separate them into separate variables
const {alerting} == require('rlk_personal'); // just import alerting

// Calling the isNight() function
personal.alerting.isNight();
// or
alerting.isNight()
// depending on how you imported

Without index.js I can only:

const alerting = require('rlk_personal/alerting.js');
const utils = require('rlk_personal/utils.js');
const lighting_constants = require('rlk_personal/constants/lighting.js');

Let’s say my personal library was large and complicated enough to have subfolders. Let say I have a bunch of constants in the “constants” folder. By adding get constants() { return require('./constants') } to the index.js file in the root folder and adding an index.js file to the constants folder too which exports all the files there in my rule I can use const {constants} = require('rlk_personal'); to import all the constants from the constants folder.

1 Like