Skip to content

Commit

Permalink
update hpx CLI (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Aug 9, 2023
1 parent 6f9993a commit 6e9a196
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/happyx.nim
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,12 @@
##
## **⚠ !Warning! It works only with `-d:enableUi` flag! ⚠**
##
## - [palette](happyx/spa/ui/palette.html) provides built-in color palette
## - [card](happyx/spa/ui/card.html) provides built-in `Card` component
## - [button](happyx/spa/ui/button.html) provides built-in `Button` component
## - [input](happyx/spa/ui/input.html) provides built-in `Input` component
## - [progress](happyx/spa/ui/progress.html) provides built-in `ProgressBar` component
## - [enums](happyx/spa/ui/enums.html) provides built-in UI enums.
## - [palette](happyx/spa/ui/palette.html) provides built-in color palette.
## - [card](happyx/spa/ui/card.html) provides built-in `Card` component.
## - [button](happyx/spa/ui/button.html) provides built-in `Button` component.
## - [input](happyx/spa/ui/input.html) provides built-in `Input` component.
## - [progress](happyx/spa/ui/progress.html) provides built-in `ProgressBar` component.
##
## ### Template Engine 🎴
##
Expand Down
85 changes: 67 additions & 18 deletions src/happyx/hpx.nim
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ proc updateHappyx(version: string) =
process.close()

sleep(1000)
styledEcho fgMagenta, "HappyX ", fgGreen, "successfully updated to ", fgMagenta, version
styledEcho fgMagenta, "HappyX ", fgGreen, "successfully updated to ", fgMagenta, version


# ---=== Commands ===--- #
Expand All @@ -253,15 +253,15 @@ proc mainHelpMessage() =
let subcommands = [
"build", "dev", "serve", "create", "html2tag", "update", "help"
]
styledEcho fgBlue, center("# ---=== HappyX CLI ===--- #", 28)
styledEcho fgBlue, center("# ---=== 🔥 HappyX CLI 🔥 ===--- #", 28)
styledEcho fgGreen, align("v" & HpxVersion, 28)
styledEcho(
"\nCLI for ", fgGreen, "creating", fgWhite, ", ",
fgGreen, "serving", fgWhite, " and ", fgGreen, "building",
fgWhite, " HappyX projects\n"
)
styledEcho "Usage:"
styledEcho fgMagenta, " hpx ", fgBlue, subcommands.join("|"), fgYellow, " [subcommand-args]"
styledEcho fgMagenta, " hpx ", fgBlue, subcommands.join("|"), fgYellow, " [subcommand-args]"


proc updateCommand(args: seq[string]): int =
Expand All @@ -287,7 +287,7 @@ proc updateCommand(args: seq[string]): int =

proc buildCommand(optSize: bool = false): int =
## Builds Single page application project into one HTML and JS files
styledEcho "Welcome to ", styleBright, fgMagenta, "HappyX ", resetStyle, fgWhite, "builder"
styledEcho "🔥 Welcome to ", styleBright, fgMagenta, "HappyX ", resetStyle, fgWhite, "builder"
let
project = compileProject()
assetsDir = project.assetsDir.replace("\\", "/").replace('/', DirSep)
Expand Down Expand Up @@ -384,13 +384,14 @@ proc buildCommand(optSize: bool = false): int =
f = open(project.buildDir / fmt"{SPA_MAIN_FILE}.js", fmWrite)
f.write(data)
f.close()
styledEcho fgGreen, "Build completed"
styledEcho fgGreen, "Build completed"
shutdownCli()
QuitSuccess


proc html2tagCommand(output: string = "", args: seq[string]): int =
## Converts HTML into `buildHtml` macro
styledEcho fgGreen, "🔨 Convert HTML into happyx file"
var o = output
# Check args
if args.len != 1:
Expand Down Expand Up @@ -427,6 +428,7 @@ proc html2tagCommand(output: string = "", args: seq[string]): int =
file = open(o, fmWrite)
file.write(outputData)
file.close()
styledEcho fgGreen, "✅ Successfully!"
shutdownCli()
QuitSuccess

Expand All @@ -439,11 +441,11 @@ proc createCommand(name: string = "", kind: string = "", templates: bool = false
selected: int = 0
imports = @["happyx"]
let projectTypes = ["SSR", "SSG", "SPA"]
styledEcho "New ", fgBlue, styleBright, "HappyX", fgWhite, " project ..."
styledEcho "🔥 New ", fgBlue, styleBright, "HappyX", fgWhite, " project"
if name == "":
try:
# Get project name
styledWrite stdout, fgYellow, align("Project name: ", 14)
styledWrite stdout, fgYellow, align("🔖 Project name: ", 14)
projectName = readLine(stdin)
except EOFError:
styledEcho fgRed, "EOF error was occurred!"
Expand All @@ -463,7 +465,7 @@ proc createCommand(name: string = "", kind: string = "", templates: bool = false
projectName = name

if kind == "":
styledEcho "Choose project type ", fgYellow, "(via arrow keys)"
styledEcho "👨‍🔬 Choose project type ", fgYellow, "(via arrow keys)"
var
choosen = false
needRefresh = true
Expand Down Expand Up @@ -500,7 +502,7 @@ proc createCommand(name: string = "", kind: string = "", templates: bool = false
shutdownCli()
return QuitFailure

styledEcho "Initializing project ..."
styledEcho "Initializing project"
createDir(projectName)
createDir(projectName / "src")
# Create .gitignore
Expand Down Expand Up @@ -626,7 +628,12 @@ proc createCommand(name: string = "", kind: string = "", templates: bool = false
f.close()
else:
discard
styledEcho fgGreen, "Successfully created ", fgMagenta, projectName, fgGreen, " project!"
# Tell user about choosen
if useTailwind:
styledEcho fgYellow, "🐥 You choose tailwind css on project creation. Read docs: ", styleUnderscore, fgGreen, "https://tailwindcss.com/docs/"
if templates:
styledEcho fgYellow, "🐥 You enabled templates on project creation. Read more: ", styleUnderscore, fgGreen, "https://github.com/enthus1ast/nimja"
styledEcho fgGreen, "⚡ Successfully created ", fgMagenta, projectName, fgGreen, " project!"
shutdownCli()
QuitSuccess

Expand All @@ -651,14 +658,14 @@ proc devCommand(host: string = "127.0.0.1", port: int = 5000,
createThread(godEyeThread, godEye, addr godEyeData)

# Launch SSR app
if project.projectType == ptSSR:
styledEcho fgRed, "SSR projects not available in the dev mode."
styledEcho fgMagenta, "Make SSR for dev mode and send Pull Request if you want!"
if project.projectType in [ptSSG, ptSSR]:
styledEcho fgRed, "SSR/SSG projects not available in the dev mode."
styledEcho fgMagenta, "💡 Make SSR/SSG dev mode and send Pull Request if you want!"
shutdownCli()
return QuitSuccess

# Start server for SPA
styledEcho "Server launched at ", fgGreen, styleUnderscore, "http://", host, ":", $port, fgWhite
styledEcho "Server launched at ", fgGreen, styleUnderscore, "http://", host, ":", $port, fgWhite
openDefaultBrowser("http://" & host & ":" & $port & "/#/")

serve host, port:
Expand Down Expand Up @@ -697,6 +704,43 @@ proc devCommand(host: string = "127.0.0.1", port: int = 5000,
shutdownCli()
QuitSuccess


proc serveCommand(host: string = "0.0.0.0", port: int = 80): int =
## Serve SPA for production
var
project = compileProject()

if project.error.len > 0:
return QuitFailure

# Explain SSG Error
if project.projectType in [ptSSG, ptSSR]:
styledEcho fgYellow, "❌ SSG projects not required to be supported in serve mode."
styledEcho fgMagenta, "💡 Compile and run your SSG server!"
shutdownCli()
return QuitSuccess

# Start SPA server
styledEcho "🔥 Server launched at ", fgGreen, styleUnderscore, "http://", host, ":", $port, fgWhite

serve host, port:
get "/":
let f = open(getCurrentDir() / "build" / "index.html")
var data = f.readAll()
f.close()
req.answerHtml(data)

get "/{file:path}":
var result = ""
let path = getCurrentDir() / "build" / file.replace('\\', '/').replace('/', DirSep)
echo "File: ", file
echo "Path: ", path
if fileExists(path):
await req.answerFile(path)
shutdownCli()
QuitSuccess


proc mainCommand(version = false): int =
if version:
styledEcho "HappyX ", fgGreen, HpxVersion
Expand All @@ -710,10 +754,8 @@ when isMainModule:
illwillInit(false)
dispatchMultiGen(
[buildCommand, cmdName = "build"],
[
devCommand,
cmdName = "dev"
],
[devCommand, cmdName = "dev"],
[serveCommand, cmdName = "serve"],
[createCommand, cmdName = "create"],
[html2tagCommand, cmdName = "html2tag"],
[updateCommand, cmdName = "update"],
Expand Down Expand Up @@ -765,6 +807,13 @@ when isMainModule:
styledEcho align("host", 8), "|h - change address (default is 127.0.0.1) (ex. --host:127.0.0.1)"
styledEcho align("port", 8), "|p - change port (default is 5000) (ex. --port:5000)"
styledEcho align("reload", 8), "|r - enable autoreloading (ex. --reload)"
of "serve":
styledEcho fgBlue, "HappyX", fgMagenta, " serve ", fgWhite, "command starting product server for SPA project."
styledEcho "\nUsage:"
styledEcho fgMagenta, "hpx dev\n"
styledEcho "Optional arguments:"
styledEcho align("host", 8), "|h - change address (default is 0.0.0.0) (ex. --host:0.0.0.0)"
styledEcho align("port", 8), "|p - change port (default is 80) (ex. --port:80)"
of "create":
styledEcho fgBlue, "HappyX", fgMagenta, " create ", fgWhite, "command creates a new HappyX project."
styledEcho "\nUsage:"
Expand Down

0 comments on commit 6e9a196

Please sign in to comment.