Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions bin/cli.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node

const { spawn } = require("child_process");
const path = require("path");
const fs = require("fs");

const isWindows = process.platform === "win32";
const scriptDir = __dirname;

function runScript(script, args) {
const child = spawn(script, args, {
stdio: "inherit",
shell: true,
});

child.on("exit", (code) => {
process.exit(code);
});
}

if (isWindows) {
// Check if PowerShell is available
const powershellCheck = spawn("powershell", ["-Command", "$PSVersionTable.PSVersion"], {
stdio: "ignore",
shell: true,
});

powershellCheck.on("error", () => {
// PowerShell not found, fallback to .cmd
console.log("PowerShell not detected. Falling back to CMD script...");
runScript(path.join(scriptDir, "cli.cmd"), process.argv.slice(2));
});

powershellCheck.on("exit", (code) => {
if (code === 0) {
// PowerShell detected, run .ps1 script
console.log("PowerShell detected. Running PowerShell script...");
runScript(`powershell`, ["-File", path.join(scriptDir, "cli.ps1"), ...process.argv.slice(2)]);
} else {
// PowerShell not found, fallback to .cmd
console.log("PowerShell not detected. Falling back to CMD script...");
runScript(path.join(scriptDir, "cli.cmd"), process.argv.slice(2));
}
});
} else {
// Non-Windows platforms run the .sh script
runScript(path.join(scriptDir, "cli.sh"), process.argv.slice(2));
}
60 changes: 60 additions & 0 deletions bin/cli.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@echo off
setlocal enabledelayedexpansion

set "SRC_DIR=node_modules\@risemaxi\api-client\src"
set "DIST_DIR=node_modules\@risemaxi\api-client\dist"
set "OUTPUT_FILE=%SRC_DIR%\contract.ts"

:generate_file
set "INPUT_FILE=%~1"

if "%INPUT_FILE%"=="" (
echo Error: input swagger is required for the generate command.
exit /b 1
)

REM Create the source directory if it doesn't exist
if not exist "%SRC_DIR%" (
mkdir "%SRC_DIR%"
)

REM Run the typed-openapi command
npx typed-openapi "%INPUT_FILE%" -o "%OUTPUT_FILE%" -r typebox

echo Compiling TypeScript to JavaScript...
npx tsc --project "%SRC_DIR%\..\tsconfig.build.json"

REM Remove the source directory
rmdir /s /q "%SRC_DIR%"

echo File generated and compiled successfully.
exit /b 0

:display_help
echo Usage: rise-api [command] [options]
echo.
echo Commands:
echo generate [content] Generate a file with the specified content and compile to JavaScript
echo help Display this help message
exit /b 0

REM Main logic
if "%~1"=="" (
echo Error: Unknown command.
call :display_help
exit /b 1
)

if "%~1"=="generate" (
call :generate_file "%~2"
exit /b 0
)

if "%~1"=="help" (
call :display_help
exit /b 0
)

echo Error: Unknown command '%~1'
call :display_help
exit /b 1
61 changes: 61 additions & 0 deletions bin/cli.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Define directories and output file
$SRC_DIR = "node_modules\@risemaxi\api-client\src"
$DIST_DIR = "node_modules\@risemaxi\api-client\dist"
$OUTPUT_FILE = Join-Path $SRC_DIR "contract.ts"

function Generate-File {
param (
[string]$InputFile
)

if (-not $InputFile) {
Write-Host "Error: input swagger is required for the generate command." -ForegroundColor Red
exit 1
}

# Create the source directory if it doesn't exist
if (-not (Test-Path $SRC_DIR)) {
New-Item -ItemType Directory -Path $SRC_DIR | Out-Null
}

# Run the typed-openapi command
Write-Host "Generating TypeScript contract from OpenAPI..."
npx typed-openapi $InputFile -o $OUTPUT_FILE -r typebox

# Compile TypeScript to JavaScript
Write-Host "Compiling TypeScript to JavaScript..."
npx tsc --project (Join-Path $SRC_DIR "..\tsconfig.build.json")

# Remove the source directory
Remove-Item -Recurse -Force $SRC_DIR

Write-Host "File generated and compiled successfully." -ForegroundColor Green
}

function Display-Help {
Write-Host "Usage: rise-api [command] [options]"
Write-Host ""
Write-Host "Commands:"
Write-Host " generate [content] Generate a file with the specified content and compile to JavaScript"
Write-Host " help Display this help message"
}

# Main logic
param (
[string]$Command,
[string]$Option
)

switch ($Command) {
"generate" {
Generate-File -InputFile $Option
}
"help" {
Display-Help
}
default {
Write-Host "Error: Unknown command '$Command'" -ForegroundColor Red
Display-Help
exit 1
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/Balogunofafrica"
},
"bin": {
"rise-api": "./bin/cli.sh"
"rise-api": "./bin/cli.cjs"
},
"scripts": {
"prepare": "npm run build",
Expand Down