From 024a371590f24568a5d7e91bc043cee97aa20722 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sun, 21 Mar 2021 14:06:04 -0500 Subject: [PATCH] Add command line tools --- ex | 138 +++++++++++++++++++++++++++++++++++++++++++ example.env | 4 ++ package.json | 4 +- pnpm-lock.yaml | 2 + src/Units.extollo.ts | 6 +- src/cli.ts | 23 ++++++++ 6 files changed, 174 insertions(+), 3 deletions(-) create mode 100755 ex create mode 100644 example.env create mode 100644 src/cli.ts diff --git a/ex b/ex new file mode 100755 index 0000000..758648d --- /dev/null +++ b/ex @@ -0,0 +1,138 @@ +#!/bin/bash -e + +ENV_NODE="$(which node)" +ENV_PNPM="$(which pnpm)" + +LOGO=" ______ _ _ _ + | ____| | | | | | + | |__ __ _| |_ ___ | | | ___ + | __| \\ \\/ / __/ _ \\| | |/ _ \\ + | |____ > <| || (_) | | | (_) | + |______/_/\\_\\\\__\\___/|_|_|\\___/" + +# Author: Tasos Latsas + +# spinner.sh +# +# Display an awesome 'spinner' while running your long shell commands +# +# Do *NOT* call _spinner function directly. +# Use {start,stop}_spinner wrapper functions + +# usage: +# 1. source this script in your's +# 2. start the spinner: +# start_spinner [display-message-here] +# 3. run your command +# 4. stop the spinner: +# stop_spinner [your command's exit status] +# +# Also see: test.sh + + +function _spinner() { + # $1 start/stop + # + # on start: $2 display message + # on stop : $2 process exit status + # $3 spinner function pid (supplied from stop_spinner) + + local on_success="DONE" + local on_fail="FAIL" + local white="\e[1;37m" + local green="\e[1;32m" + local red="\e[1;31m" + local nc="\e[0m" + + case $1 in + start) + # calculate the column where spinner and status msg will be displayed + let column=$(tput cols)-${#2}-8 + # display message and position the cursor in $column column + echo -ne ${2} + printf "%${column}s" + + # start spinner + i=1 + sp='\|/-' + delay=${SPINNER_DELAY:-0.15} + + while : + do + printf "\b${sp:i++%${#sp}:1}" + sleep $delay + done + ;; + stop) + if [[ -z ${3} ]]; then + echo "spinner is not running.." + exit 1 + fi + + kill $3 > /dev/null 2>&1 + + # inform the user uppon success or failure + echo -en "\b[" + if [[ $2 -eq 0 ]]; then + echo -en "${green}${on_success}${nc}" + else + echo -en "${red}${on_fail}${nc}" + fi + echo -e "]" + ;; + *) + echo "invalid argument, try {start/stop}" + exit 1 + ;; + esac +} + +function start_spinner { + # $1 : msg to display + _spinner "start" "${1}" & + # set global spinner pid + _sp_pid=$! + disown +} + +function stop_spinner { + # $1 : command exit status + _spinner "stop" $1 $_sp_pid + unset _sp_pid +} + +function echoRun() { + echo "" + echo "+ $@" + echo "" + "$@" + echo "" +} + +if [ ! -d "./node_modules" ]; then + echo "$LOGO" + echo "+----------------------------------------+" + echo "| Docs: https://extollo.garrettmills.dev |" + echo "+----------------------------------------+" + echo "" + echo "Welcome to Extollo! Let's set things up for the first time..." + + if [ ! -x "$ENV_PNPM" ]; then + echo "Please install PNPM to use Extollo." + fi + + echoRun "$ENV_PNPM" i + + if [ ! -f "./.env" ]; then + echoRun cp example.env .env + fi + + echo "" + echo "" + printf "\033[32m✓\033[39m Looks like you're all set up! Run this command again to access the Extollo CLI.\n" +else + start_spinner "Building your app..." + "$ENV_PNPM" run build > /dev/null + stop_spinner 0 + "$ENV_NODE" --experimental-repl-await ./lib/cli.js $@ +fi diff --git a/example.env b/example.env new file mode 100644 index 0000000..1b6e92a --- /dev/null +++ b/example.env @@ -0,0 +1,4 @@ +DEBUG_MODE=true +EXTOLLO_LOGGING_LEVEL=6 +DATABASE_PASSWORD=extollo +DATABASE_HOST=db01.platform.local diff --git a/package.json b/package.json index 0c98bd2..4a53e41 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "@extollo/di": "file:../di", "@extollo/lib": "file:../lib", "@extollo/orm": "file:../orm", + "@extollo/cli": "file:../cli", "copyfiles": "^2.4.1", "typescript": "^4.1.3" }, @@ -18,7 +19,8 @@ "test": "echo \"Error: no test specified\" && exit 1", "build": "rm -rf lib && mkdir -p lib && cp .env lib/.env && tsc", "postbuild": "copyfiles -u 1 src/app/resources/**/* lib", - "app": "pnpm run build && node lib/index.js" + "app": "pnpm run build && node lib/index.js", + "cli": "pnpm run build && node lib/cli.js" }, "files": [ "lib/**/*" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6ed142c..548e381 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,4 +1,5 @@ dependencies: + '@extollo/cli': link:../cli '@extollo/di': link:../di '@extollo/lib': link:../lib '@extollo/orm': link:../orm @@ -294,6 +295,7 @@ packages: resolution: integrity: sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw== specifiers: + '@extollo/cli': file:../cli '@extollo/di': file:../di '@extollo/lib': file:../lib '@extollo/orm': file:../orm diff --git a/src/Units.extollo.ts b/src/Units.extollo.ts index a983c50..12faf66 100644 --- a/src/Units.extollo.ts +++ b/src/Units.extollo.ts @@ -1,8 +1,10 @@ -import {Config, Controllers, HTTPServer, Middlewares, Routing} from '@extollo/lib' +import {Config, Controllers, HTTPServer, Middlewares, Routing, Unit} from '@extollo/lib' import {Database, Models} from "@extollo/orm"; +import {CommandLine} from "@extollo/cli"; export const Units = [ Config, + CommandLine, Controllers, Middlewares, Database, @@ -10,4 +12,4 @@ export const Units = [ Routing, HTTPServer, -] +] as (typeof Unit)[] diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..46759f2 --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,23 @@ +import {Application} from "@extollo/lib"; +import {Units} from './Units.extollo'; +import {CommandLineApplication} from "@extollo/cli"; + +(async () => { + /* + * The Application + * ----------------------------------------------------- + * The application instance is a global inversion of control container that + * ties your entire application together. The app container manages services + * and lifecycle. + */ + const app = Application.getApplication() + app.forceStartupMessage = false + + Units.reverse() + CommandLineApplication.setReplacement(Units[0]) + Units[0] = CommandLineApplication + Units.reverse() + + app.scaffold(__dirname, Units) + await app.run() +})()