Skip to content

Instantly share code, notes, and snippets.

@bprobbins
Last active November 27, 2021 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bprobbins/e530a4916ba7e94aada8dfa46add0770 to your computer and use it in GitHub Desktop.
Save bprobbins/e530a4916ba7e94aada8dfa46add0770 to your computer and use it in GitHub Desktop.
sudo bash script for quickly installing the particle-cli onto Raspberry Pi
######################################################################
# Particle-cli setup script for Raspberry Pi
# by bprobbins
# Code leans heavily on audstanley's excellent nodejs installer:
# https://github.com/audstanley/NodeJs-Raspberry-Pi
#
# Run this installer in your home (usually pi) directory.
# In your home directory, enter the following in your terminal:
# sudo bash particle-cli-setup.sh
# Once running, please don't interrupt the script. It can take awhile
# on slower (earlier) pi models. Tested on pi models 3 and 4.
######################################################################
#!/bin/bash
#Try to ensure that script is being run as sudo
if [ ! `id -u` -eq 0 ]
then
echo "This script must be run with sudo!"
echo "i.e., sudo bash particle-cli-setup.sh"
exit 1
fi
# Ansi color code variables
red="\e[0;91m"
blue="\e[0;94m"
expand_bg="\e[K"
blue_bg="\e[0;104m${expand_bg}"
red_bg="\e[0;101m${expand_bg}"
green_bg="\e[0;102m${expand_bg}"
yellow_bg="\e[0;43m${expand_bg}"
green="\e[0;92m"
white="\e[0;97m"
yellow="\e[0;33m"
bold="\e[1m"
uline="\e[4m"
reset="\e[0m"
imsure=0
while [ $imsure == 0 ]; do
current_user=""
while [ -z ${current_user} ]; do
current_user=$(whiptail --inputbox "\nEnter your Raspberry Pi login username (must not be left empty):" 20 70 --title "NodeJs/Particle-Cli Setup" --nocancel 3>&1 1>&2 2>&3)
done
if (whiptail --title "Confirm username" --yesno "Is your username $current_user?" 20 70); then
imsure=1
else
imsure=0
fi
done
COLUMNS=$(tput cols)
astr0="Please wait"
astr1="Installing/Updating Nodejs"
astr2="Installing NPM"
astr3="Configuring Nodejs"
astr4="Installing Particle-Cli"
echo -e "${yellow_bg}${reset}"
echo -e "${green}"
printf "%*s\n" $(((${#astr1}+$COLUMNS)/2)) "$astr1"
printf "%*s\n" $(((${#astr0}+$COLUMNS)/2)) "$astr0"
echo -e "${reset}"
echo -e "${yellow_bg}${reset}"
sleep 3
sudo rm -rf package-lock.json
sudo rm -rf node_modules
#Use audstanley latest node install script
wget -O - https://raw.githubusercontent.com/audstanley/NodeJs-Raspberry-Pi/master/Install-Node.sh | sudo bash
echo -e "${yellow_bg}${reset}"
echo -e "${green}"
printf "%*s\n" $(((${#astr2}+$COLUMNS)/2)) "$astr2"
printf "%*s\n" $(((${#astr0}+$COLUMNS)/2)) "$astr0"
echo -e "${reset}"
echo -e "${yellow_bg}${reset}"
echo "Now installing npm............"
sudo npm install -g npm
#next we want to let npm install modules globally w/o needing sudo
#see: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
echo -e "${yellow_bg}${reset}"
echo -e "${green}"
printf "%*s\n" $(((${#astr3}+$COLUMNS)/2)) "$astr3"
printf "%*s\n" $(((${#astr0}+$COLUMNS)/2)) "$astr0"
echo -e "${reset}"
echo -e "${yellow_bg}${reset}"
echo "Now configring Nodejs............"
#create npm-global directory if doesn't exist
npmglobal_dir="/home/"$current_user"/.npm-global"
npmglobal_bindir="/home/"$current_user"/.npm-global/bin"
if [ -d "$npmglobal_dir" ]; then
echo " " #"$npmglobal_dir already exists"
else
echo " " #"$npmglobal_dir will be created"
mkdir "$npmglobal_dir" #"/home/"$current_user"/.npm-global"
fi
#ensure that npm uses the new path
npm config set prefix "$npmglobal_dir" #"/home/"$current_user"/.npm-global"
#check if npm config directory is in .profile; only add to .profile if not
if grep -w "$npmglobal_bindir" ".profile" >/dev/null; then
echo "'$npmglobal_bindir' is already in PATH. We can now install particle-cli." #don't EXPORT
else
touch "/home/"$current_user"/.profile"
echo "export PATH=/home/"$current_user"/.npm-global/bin:"$PATH >> "/home/"$current_user"/.profile"
#Next update the system
. "/home/"$current_user"/.profile" # same as source ~/.profile
echo "'$npmglobal_bindir' added to .profile. We can now install particle-cli."
fi
echo -e "${yellow_bg}${reset}"
echo -e "${green}"
printf "%*s\n" $(((${#astr4}+$COLUMNS)/2)) "$astr4"
printf "%*s\n" $(((${#astr0}+$COLUMNS)/2)) "$astr0"
echo -e "${reset}"
echo -e "${yellow_bg}${reset}"
echo "Now installing particle-cli ................"
npm install -g @mapbox/node-pre-gyp serialport particle-cli
if (whiptail --title "REBOOT ?" --yesno "After a REBOOT you can enter 'particle login'.\n\nWould you like to REBOOT now?" 12 78) then
echo -e "${red}************* REBOOTING... *****************${reset}"
sudo reboot
else
echo -e "${red}************* REBOOT REQUIRED before attempting to run particle! *************${reset}"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment