From 38d126a2bc32178728d0a5f530f496133d217af1 Mon Sep 17 00:00:00 2001 From: kosiken Date: Wed, 18 Dec 2024 10:11:52 +0100 Subject: [PATCH] feat: add windows scripts --- bin/cli.cjs | 48 +++++++++++++++++++++++++++++++++++++++++ bin/cli.cmd | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ bin/cli.ps1 | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 170 insertions(+), 1 deletion(-) create mode 100755 bin/cli.cjs create mode 100644 bin/cli.cmd create mode 100644 bin/cli.ps1 diff --git a/bin/cli.cjs b/bin/cli.cjs new file mode 100755 index 0000000..c615d92 --- /dev/null +++ b/bin/cli.cjs @@ -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)); +} diff --git a/bin/cli.cmd b/bin/cli.cmd new file mode 100644 index 0000000..45b55c9 --- /dev/null +++ b/bin/cli.cmd @@ -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 diff --git a/bin/cli.ps1 b/bin/cli.ps1 new file mode 100644 index 0000000..b96570f --- /dev/null +++ b/bin/cli.ps1 @@ -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 + } +} diff --git a/package.json b/package.json index b86bf40..efb5790 100644 --- a/package.json +++ b/package.json @@ -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",