diff --git a/bin/control.sh.ps1 b/bin/control.sh.ps1 index e1bb6aa..5736c2d 100644 --- a/bin/control.sh.ps1 +++ b/bin/control.sh.ps1 @@ -13,12 +13,14 @@ param( # list collection items [Int32]$Limit = 50, [Int32]$Offset = 0, - [switch]$Full + [switch]$Full, + + [switch]$Help ) $ErrorActionPreference = "stop" -if (!$Command) { +if (!$Command -OR $Help) { Write-Host "Usage: .\control.ps1 start [-Manager] [-Echo] [-Force] [-Color] [-WinMon] [-Hide] diff --git a/bin/manager.bat b/bin/manager.bat index d7e0fba..1cc849a 100644 --- a/bin/manager.bat +++ b/bin/manager.bat @@ -1,6 +1,49 @@ @echo off -cd /D "%~dp0" + +set SCRIPT_LOC=%~dp0 + +:parseArgs + +if "%1"=="" goto endArgs + +if /I "%1"=="--port" ( + if "%2"=="" ( + echo Specify http port for cronicle + exit + ) + set CRONICLE_WebServer__http_port=%2 + echo Custom port set: %2 + shift + shift +) else if /I "%1"=="--storage" ( + if "%2"=="" ( + echo Specify path to storage.json config file + exit + ) + set CRONICLE_storage_config=%~f2 + echo Custom storage set: %~f2 + shift + shift +) else if /I "%1"=="--key" ( + if "%2"=="" ( + echo Secret key not specified + exit + ) + set CRONICLE_secret_key=%2 + echo Using custom secret key: ***** + shift + shift +) else if /I "%1"=="--help" ( + echo Usage: .\manager [--port port] [ --storage /path/to/storage.json] + shift +) else (exit) + +goto parseArgs + +:endArgs + +cd /D %SCRIPT_LOC% REM check for custom node version IF EXIST "%~dp0..\nodejs\node.exe" ( @@ -14,4 +57,4 @@ if not "%~1"=="" ( echo CRONICLE_http_port is set to %1 ) -node .\cronicle.js --manager --echo --foreground --color \ No newline at end of file +node .\cronicle.js --manager --echo --foreground --color diff --git a/bundle.ps1 b/bundle.ps1 index 26a46d9..f090982 100644 --- a/bundle.ps1 +++ b/bundle.ps1 @@ -1,6 +1,5 @@ -# $Path = "dist" -# if($args[0]) { $Path = $args[0] } + param( [Parameter(Position = 0)][String]$Path = "dist", # install directory [switch]$S3, # bundle s3 engine @@ -14,7 +13,6 @@ param( [switch]$Level, # bundle level db engine [switch]$Lmdb, # bundle lmdb engine * [switch]$Sftp, # bundle sftp engine - [string][ValidateSet("s3","lmdb","level","sqlite","oracle","mysql","mssql","pgsql","sftp","redis")]$Engine, # copy storage.engine.json from sample_conf to dist [switch]$Force, # force reinstall if something is broken in node_modules [switch]$Dev, # prevent minificaiton and add verbosity [Switch]$Restart, # for dev purposes only: it will force kill cronicle if running, and start it over again once bundling is complete @@ -24,9 +22,42 @@ param( [switch]$Test, # run unit test at the end [ValidateSet("warning", "debug", "info", "warning", "error","silent")][string]$ESBuildLogLevel = "warning", [switch]$Help + # dummy parameters to capture "--" args + ,[Parameter(Position = 1)][String]$arg1 + ,[Parameter(Position = 2)][String]$arg2 + ,[Parameter(Position = 3)][String]$arg3 + ,[Parameter(Position = 4)][String]$arg4 + ,[Parameter(Position = 5)][String]$arg5 ) +foreach ($arg in $PSBoundParameters.Values) { + if ($arg -eq "--s3") { $S3 = $true } + if ($arg -eq "--sql") { $SQL = $true } + if ($arg -eq "--oracle") { $Oracle = $true } + if ($arg -eq "--mssql") { $MSSQL = $true } + if ($arg -eq "--mysql") { $Mysql = $true } + if ($arg -eq "--pgsql") { $Pgsql = $true } + if ($arg -eq "--sqlite") { $Sqlite = $true } + if ($arg -eq "--redis") { $Redis = $true } + if ($arg -eq "--level") { $Level = $true } + if ($arg -eq "--lmdb") { $Lmdb = $true } + if ($arg -eq "--sftp") { $Sftp = $true } + if ($arg -eq "--force") { $Force = $true } + if ($arg -eq "--dev") { $Dev = $true } + if ($arg -eq "--restart") { $Restart = $true } + if ($arg -eq "--tools") { $Tools = $true } + if ($arg -eq "--all") { $All = $true } + if ($arg -eq "--test") { $Test = $true } + if ($arg -eq "--help") { $Help = $true } + if ($arg -eq "--verbose") { $V = $true } +} + +if($Path -like "--*") { + # perhaps user meant some flag, fallback Path to default + $Path = "dist" +} + if($Help) { Write-Host "Usage: ./bundle.ps1 [path\to\dist] # default bundle location is dist" Write-Host " [ -S3 | -Redis | -Lmdb | -Level | -Redis | -Sftp ] # 1 or more storage engines (FS always added)" @@ -44,9 +75,17 @@ if($Help) { $ErrorActionPreference = 'Stop' -Write-Host "-----------------------------------------" -Write-Host " Installing cronicle bundle into $Path" -Write-Host "-----------------------------------------" +function Write-Bold { param($Text, [switch]$U) + $b = "[1m"; if($U) {$b = "[1;4m" }; Write-Output "$( [char]27 )$b$Text$( [char]27 )[0m" +} + +$FullPath = mkdir -Force $Path + +Write-Bold "`nInstalling cronicle bundle into $FullPath`n" -U | Write-Host -ForegroundColor Green + +# Write-Host "-----------------------------------------" +# Write-Host " Installing cronicle bundle into $($Path)" +# Write-Host "-----------------------------------------" $proc = $null $pidFile = "$Path\logs\cronicled.pid" @@ -56,7 +95,7 @@ if(Test-Path $pidFile ) { } if($proc) { - if($Restart.IsPresent) { + if($Restart) { Write-Host "Shutting down cronicle..." if(!$proc.CloseMainWindow()) {Stop-Process -id $proc.Id } } @@ -71,20 +110,20 @@ $minify = "--minify=true" $ESBuildLogLevel = "warning" $npmLogLevel = "warn" -if($Restart.IsPresent) { $minify = "--minify=false" } +if($Restart) { $minify = "--minify=false" } -if ($Dev.IsPresent) { +if ($Dev) { $minify = "--minify=false" $ESBuildLogLevel = "info" $npmLogLevel = "info" } -if($V.IsPresent) { +if($V) { $ESBuildLogLevel = "info" $npmLogLevel = "info" } -if($All.IsPresent) { +if($All) { $S3 = $true $Sftp = $true $Lmdb = $true @@ -95,15 +134,13 @@ if($All.IsPresent) { # ------------------------- -# if (!(Get-Command esbuild -ErrorAction SilentlyContinue)) { npm i esbuild -g --loglevel warn } - if(Test-Path $PSScriptRoot\nodejs\node.exe) { $env:Path = "$PSScriptRoot\nodejs\;$env:Path;" Write-Warning "Using custom node version $(node -v)" # exit 0 } -if (!(Test-Path .\node_modules) -or $Force.IsPresent) { +if (!(Test-Path .\node_modules) -or $Force) { Write-Host "`n---- Installing npm packages`n" npm install --loglevel $npmLogLevel Write-Host "`n-----------------------------------------" @@ -115,7 +152,7 @@ if (!(Get-Command esbuild -ErrorAction SilentlyContinue)) { } # ---- set up bin and htdocs folders on dist (will overwrite) -mkdir -Force $Path +# mkdir -Force $Path | Out-Null Copy-Item -Force -r htdocs $Path/ mkdir -EA SilentlyContinue $Path/htdocs/js/external, $Path/htdocs/css, $Path/htdocs/fonts | Out-Null @@ -198,7 +235,7 @@ Get-Content ` , node_modules/codemirror/addon/display/placeholder.js ` , node_modules/codemirror/mode/xml/xml.js ` , node_modules/codemirror/mode/sql/sql.js ` - , node_modules/js-yaml/dist/js-yaml.js ` + , node_modules/js-yaml/dist/js-yaml.js ` , node_modules/codemirror/addon/lint/lint.js ` , node_modules/codemirror/addon/lint/json-lint.js ` , node_modules/codemirror/addon/lint/yaml-lint.js ` @@ -249,16 +286,16 @@ Copy-Item -Force htdocs/index-bundle.html $Path/htdocs/index.html # -------- Bundle storage-cli and event plugins ----------------------------------------------- # -Write-Host "`n ---- Building storage-cli and plugins`n" +Write-Bold "Building storage-cli and plugins" esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/ ` --external:../conf/config.json --external:../conf/storage.json --external:../conf/setup.json ` bin/storage-cli.js -if($Tools.IsPresent) { - Write-Host " - storage-repair.js`n" +if($Tools) { + Write-Host " - storage-repair.js" esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/ --external:../conf/config.json bin/storage-repair.js - Write-Host " - storage-migrate.js`n" + Write-Host " - storage-migrate.js" esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/ --external:../conf/config.json bin/storage-migrate.js } @@ -275,35 +312,35 @@ esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$ # ------------ Bundle Storage Engines ------------------------------ # -Write-Host "`n ---- Building Storage Engines:`n" +Write-Bold "Building Storage Engines:" $engines = "FS" -Write-Host " - bundling FS Engine`n" +Write-Host " - bundling FS Engine" esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/engines engines/Filesystem.js -if ($S3.IsPresent ) { - Write-Host " - bundling S3 Engine`n" +if ($S3 ) { + Write-Host " - bundling S3 Engine" $engines += ", S3" npm i @aws-sdk/client-s3 @aws-sdk/lib-storage --no-save --loglevel silent esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/engines engines/S3.js } -if ($Redis.IsPresent) { - Write-Host " - bundling Redis Engine`n" +if ($Redis) { + Write-Host " - bundling Redis Engine" $engines += ", Redis" npm i redis@3.1.2 --no-save --loglevel silent esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/engines engines/Redis.js } -if ($Sftp.IsPresent) { - Write-Host " - bundling Sftp Engine`n" +if ($Sftp) { + Write-Host " - bundling Sftp Engine" $engines += ", Sftp" npm i ssh2-sftp-client --no-save --loglevel silent esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/engines --loader:.node=file engines/Sftp.js } -if ($Level.IsPresent) { - Write-Host " - bundling Level Engine`n" +if ($Level) { + Write-Host " - bundling Level Engine" $engines += ", Level" npm i level --no-save --loglevel silent esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/engines engines/Level.js @@ -335,14 +372,14 @@ if($sqlDrivers.Count -gt 0) { Write-Host " - bundling SQL Engine [$($sqlDrivers -join ",")]" & npm $sqlInstall & esbuild $sqlArgs - if($Sqlite.IsPresent) { + if($Sqlite) { Copy-Item -Recurse -Force node_modules/sqlite3/build $Path/bin/ } } # Lmdb, need to install lmdb separetly (npm i lmdb) -if($Lmdb.IsPresent) { - Write-Host " - bundling Lmdb Engine*`n" +if($Lmdb) { + Write-Host " - bundling Lmdb Engine*" $engines += ", Lmdb" esbuild --bundle --log-level=$ESBuildLogLevel $minify --platform=node --outdir=$Path/bin/engines --external:lmdb engines/Lmdb.js } @@ -350,7 +387,7 @@ if($Lmdb.IsPresent) { #### ------- SET UP CONFIGS ----- only do it if config folder doesnt exist # if (!(Test-Path $Path/conf)) { - Write-Host "`n ---- Setting up initial config files`n" + Write-Bold "Setting up initial config files" Copy-Item -Force -r sample_conf/ $Path/conf @@ -365,27 +402,13 @@ if(Test-Path "sample_conf\examples\storage.$Engine.json") { Copy-Item -Force "sample_conf\examples\storage.$Engine.json" $Path\conf\storage.json } -# --- CRONICLE.JS -Write-Host "`n ---- Bundling cronicle.js`n" -esbuild --bundle $minify --keep-names --platform=node --outfile=$Path/bin/cronicle.js lib/main.js - # --- fix formidable -Write-Host "`n ---- Applyig some patches" +Write-Bold "Applyig some patches" esbuild --bundle --log-level=$ESBuildLogLevel $minify --keep-names --platform=node --outdir=$Path/bin/plugins node_modules/formidable/src/plugins/*.js -# --------------- Final Clean up --------------------------- - -# Remove-Item -Recurse -Force ` -# $Path/bin/jars ` -# , $Path/bin/cms ` -# , $Path/bin/cronicled.init ` -# , $Path/bin/importkey.sh ` -# , $Path/bin/debug.sh ` -# , $Path/bin/java-plugin.js ` -# , $Path/bin/install.js ` -# , $Path/bin/build.js ` -# , $Path/bin/build-tools.js ` - +# --- CRONICLE.JS +Write-Bold "Bundling cronicle.js" +esbuild --bundle $minify --keep-names --platform=node --outfile=$Path/bin/cronicle.js lib/main.js # --- if needed set up npm package in dist folder to install some deps that cannot be bundled Push-Location $Path @@ -397,70 +420,59 @@ if(!(Test-Path "package.json")) { npm pkg set main="bin/cronicle.js" npm pkg set scripts.start="node bin/cronicle.js --foreground --echo --manager --color" } -if($Lmdb.IsPresent) { npm i "lmdb@2.9.4" --loglevel silent} +if($Lmdb) { npm i "lmdb@2.9.4" --loglevel silent} Pop-Location - -if($Restart.IsPresent) { - Write-Host "`n---- Restarting cronicle`n" +if($Restart) { + Write-Bold "`Restarting cronicle" # --- $env:CRONICLE_dev_version="1.x.dev-$([datetime]::Now.ToString("yyyy-MM-dd HH:mm:ss"))" - Start-Process node -WindowStyle Minimized -ArgumentList @("$Path\bin\cronicle.js", "--foreground", "--echo", "--manager", "--color") + Start-Process "$Path\bin\manager" -WindowStyle Minimized + #Start-Process node -WindowStyle Minimized -ArgumentList @("$Path\bin\cronicle.js", "--foreground", "--echo", "--manager", "--color") } # --- Print setup info / stats -Write-Host "`n-------------------------------------------------------------------------------------------------------------------------------------------`n" -Write-Host "Bundle is ready: $FullPath `n" -ForegroundColor Green -Write-Host "Minified: $($minify -like '*true*' )" -Write-Host "Engines bundled: $engines" -if($SQL.IsPresent) { - Write-Host " * SQL bundle includes mysql and postgres drivers. You can additionally install sqlite3, oracledb, tedious (for mssql)" -} -if($Lmdb.IsPresent) { - Write-Host " * Lmdb cannot be fully bundled. lmdb package is installed in the dist folder using npm" -} +Write-Host "`n [ Bundle is ready: $FullPath ] `n" -ForegroundColor Green +Write-Bold "Info:" +Write-Host " - minified: $($minify -like '*true*' )" +Write-Host " - engines bundled: $engines" -if($Restart.IsPresent) { - Write-Host "Running in dev mode. Version: $env:CRONICLE_dev_version `n" +if($Restart) { + Write-Bold "Running in dev mode. Version: $env:CRONICLE_dev_version `n" -U exit 0 } if($env:Path.indexOf("$FullPath\bin") -lt 0) { $env:Path = $env:Path + ";$FullPath\bin"; Write-Host "$Path\bin is added to path variable"} -Write-Host " -Before you begin: - - Configure you storage engine setting in conf/config.json || conf/storage.json || CRONICLE_storage_config=/path/to/custom/storage.json - - Set you Secret key in conf/congig.json || conf/secret_key file || CRONICLE_secret_key_file || CRONICLE_secret_key env variables - -To setup cronicle storage (on the first run): - node .\$Path\bin\storage-cli.js setup +if(!$Dev -and !$Restart) { # do not print below info during dev or debug -Start as manager in foreground: - node .\$Path\bin\cronicle.js --echo --foreground --manager --color +Write-Bold "`nBefore you begin:" +Write-Host " - Configure you storage engine setting in conf/config.json OR conf/storage.json OR CRONICLE_storage_config=/path/to/custom/storage.json" +Write-Host " - Set you Secret key in conf/congig.json OR via conf/secret_key file OR via CRONICLE_secret_key_file / CRONICLE_secret_key env variables" +Write-Host " - Init cronicle storage (on the first run): node .\$Path\bin\storage-cli.js setup" +Write-Host " - Start cronicle as manage: node .\$Path\bin\cronicle.js --echo --foreground --manager --color`n" -Or both together: .\$Path\bin\manager +Write-Bold "You can also setup/start cronicle using [manager] entrypoint:" +Write-Host ".\$Path\bin\manager [ --port 3012 ] [ --storage Path\to\storage.json ] [ --key someSecretKey ]`n" -------------------------------------------------------------------------------------------------------------------------------------------- +Write-Bold "To Reinstall/upgrade run (please back up $FullPath first):" +Write-Host ".\bundle.ps1 $Path -Force`n" -to reinstall/upgrade run (please back up $FullPath first): - .\bundle.ps1 $Path -Force - -to install as windows service: - cd $Path +Write-Bold "To install as Windows Service:" -U +Write-Host " cd $Path npm i node-windows -g npm link node-windows node bin\install.js - -test: Get-Service cronicle - -to remove service: - node bin\uninstall.js - + ### Make sure it's running: Get-Service cronicle + ### Remove service: node bin\uninstall.js " -if($Test.IsPresent) { +} + +# perform unit test if needed +if($Test) { - Write-Host "Running unit test" - Write-Host "build test script" + Write-Bold "Running unit test" + Write-Host " - building test script" esbuild --bundle --outdir=$Path/bin/ --platform=node lib/test.js node .\node_modules\pixl-unit\unit.js $Path\bin\test.js Remove-Item $Path\bin\test.js