Skip to content

Commit

Permalink
feat: Linux & macOS binary installer
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Dec 18, 2019
1 parent c7b2412 commit f0f9698
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
9 changes: 9 additions & 0 deletions bin/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

'use strict';

if (
process.argv[2] === 'binary-postinstall' &&
process.argv.length === 3 &&
require('../lib/utils/isStandaloneExecutable')
) {
require('../scripts/postinstall');
return;
}

// global graceful-fs patch
// https://github.com/isaacs/node-graceful-fs#global-patching
const realFs = require('fs');
Expand Down
87 changes: 87 additions & 0 deletions scripts/pkg/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/sh

set -e

reset="\033[0m"
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
cyan="\033[36m"
white="\033[37m"

printf "\n$yellow Installing Serverless!$reset\n\n"

# Detect platform
if [[ $OSTYPE == "linux-gnu" ]]; then
PLATFORM="linux"
elif [[ $OSTYPE == "darwin"* ]]; then
PLATFORM="macos"
else
echo "$red Sorry, there's no serverless binary installer available for this platform. Please open request for it at https://github.com/serverless/serverless/issues.$reset"
exit 1
fi

# Detect architecture
MACHINE_TYPE=`uname -m`
if [[ $MACHINE_TYPE == "x86_64" ]]; then
ARCH='x64'
else
echo "$red Sorry, there's no serverless binary installer available for $MACHINE_TYPE architecture. Please open request for it at https://github.com/serverless/serverless/issues.$reset"
exit 1
fi

# Resolve latest tag
LATEST_TAG=`curl -L --silent https://api.github.com/repos/serverless/serverless/releases/latest 2>&1 | grep 'tag_name' | grep -oE "v[0-9]+\.[0-9]+\.[0-9]+"`

# Dowload binary
BINARIES_DIR_PATH=$HOME/.serverless/bin
BINARY_PATH=$BINARIES_DIR_PATH/serverless
mkdir -p $BINARIES_DIR_PATH
printf "$yellow Downloading binary...$reset\n"
curl -L -o $BINARY_PATH https://github.com/serverless/serverless/releases/download/$LATEST_TAG/serverless-$PLATFORM-$ARCH
chmod +x $BINARY_PATH

# Ensure aliases
ln -sf serverless $BINARIES_DIR_PATH/sls

# Add to $PATH
SOURCE_STR="# Added by serverless binary installer\nexport PATH=\"\$HOME/.serverless/bin:\$PATH\"\n"
add_to_path () {
command printf "\n$SOURCE_STR" >> "$1"
printf "\n$yellow Added the following to $1:\n\n$SOURCE_STR$reset"
}
SHELLTYPE="$(basename "/$SHELL")"
if [[ $SHELLTYPE = "fish" ]]; then
command fish -c 'set -U fish_user_paths $fish_user_paths ~/.serverless/bin'
printf "\n$yellow Added ~/.serverless/bin to fish_user_paths universal variable$reset."
elif [[ $SHELLTYPE = "zsh" ]]; then
SHELL_CONFIG=$HOME/.zshrc
if [ ! -r $SHELL_CONFIG ] || (! `grep -q '.serverless/bin' $SHELL_CONFIG`); then
add_to_path $SHELL_CONFIG
fi
else
SHELL_CONFIG=$HOME/.bashrc
if [ ! -r $SHELL_CONFIG ] || (! `grep -q '.serverless/bin' $SHELL_CONFIG`); then
add_to_path $SHELL_CONFIG
fi
SHELL_CONFIG=$HOME/.bash_profile
if [[ -r $SHELL_CONFIG ]]; then
if [[ ! $(grep -q '.serverless/bin' $SHELL_CONFIG) ]]; then
add_to_path $SHELL_CONFIG
fi
else
SHELL_CONFIG=$HOME/.bash_login
if [[ -r $SHELL_CONFIG ]]; then
if [[ ! $(grep -q '.serverless/bin' $SHELL_CONFIG) ]]; then
add_to_path $SHELL_CONFIG
fi
else
SHELL_CONFIG=$HOME/.profile
if [ ! -r $SHELL_CONFIG ] || (! `grep -q '.serverless/bin' $SHELL_CONFIG`); then
add_to_path $SHELL_CONFIG
fi
fi
fi
fi

$HOME/.serverless/bin/serverless binary-postinstall
6 changes: 5 additions & 1 deletion scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

const boxen = require('boxen');
const chalk = require('chalk');
const isPathDependent =
require('../lib/utils/isStandaloneExecutable') && process.platform !== 'win32';

const truthyStr = val => val && !['0', 'false', 'f', 'n', 'no'].includes(val.toLowerCase());
const { CI, ADBLOCK, SILENT } = process.env;
if (!truthyStr(CI) && !truthyStr(ADBLOCK) && !truthyStr(SILENT)) {
process.stdout.write(
`${boxen(
chalk.yellow(
'Serverless Framework successfully installed!\nTo start your first project, run “serverless”.'
`Serverless Framework successfully installed!\nTo start your first project, ${
isPathDependent ? 'please open another terminal and ' : ''
}run “serverless”.`
),
{ padding: 1, margin: 1, borderColor: 'yellow' }
)}\n`
Expand Down

0 comments on commit f0f9698

Please sign in to comment.