-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgenerate-swagger-client.ps1
More file actions
92 lines (75 loc) · 4.16 KB
/
generate-swagger-client.ps1
File metadata and controls
92 lines (75 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
$generatorUrl = "https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.68/swagger-codegen-cli-3.0.68.jar"
$swaggerUrl = "http://localhost:5000/swagger/v1/swagger.json"
$generatorFolder = Join-Path $PSScriptRoot "swagger-codegen"
$generatorFile = Join-Path $PSScriptRoot "swagger-codegen\swagger-codegen-cli-3.0.68.jar"
$outputFolder = Join-Path $PSScriptRoot "swagger-codegen\archive"
$apiDestination = Join-Path $PSScriptRoot "hwproj.front\src\api"
if (-not (Test-Path -Path $generatorFolder)) {
Write-Host "Создаем папку для генератора..."
New-Item -ItemType Directory -Path $generatorFolder | Out-Null
}
Set-Location -Path $generatorFolder
if (-Not (Test-Path -Path $generatorFile)) {
Write-Host "JAR-файл генератора отсутствует. Скачиваем его..."
try {
Invoke-WebRequest -Uri $generatorUrl -OutFile $generatorFile
Write-Host "Генератор успешно загружен."
} catch {
Write-Error "Не удалось скачать файл: $($_.Exception.Message)"
Exit 1
}
} else {
Write-Host "JAR-файл генератора уже существует"
}
# Убедимся, что папка archive существует
if (-not (Test-Path -Path $outputFolder)) {
Write-Host "Создаем папку для архива..."
New-Item -ItemType Directory -Path $outputFolder | Out-Null
}
# Генерация клиента TypeScript с использованием swagger-codegen
Write-Host "Генерация API клиента..."
java -jar $generatorFile `
generate -i $swaggerUrl -l typescript-fetch -o $outputFolder
# Проверяем, что архив был успешно создан и существует
if (-not (Test-Path -Path "$outputFolder")) {
Write-Error "Не удалось найти папку после генерации. Проверьте путь."
Exit 1
}
# Разархивируем сгенерированный клиент и достаем api.ts
Write-Host "Разархивируем файлы..."
Get-ChildItem -Path $outputFolder -Recurse | ForEach-Object {
if ($_.Extension -eq ".zip") {
Expand-Archive -Path $_.FullName -DestinationPath $outputFolder -Force
}
}
# Ищем файл `api.ts`
$apiFile = Get-ChildItem -Path $outputFolder -Recurse | Where-Object { $_.Name -eq "api.ts" }
if ($null -eq $apiFile) {
Write-Error "Файл api.ts не найден среди разархивированных файлов."
Exit 1
}
# Проверяем путь назначения для `api.ts` и создаем, если нужно
if (-not (Test-Path -Path $apiDestination)) {
Write-Host "Папка назначения не существует. Создаем..."
New-Item -ItemType Directory -Path $apiDestination -Force | Out-Null
}
# Перемещаем api.ts в папку назначения
Write-Host "Перемещаем api.ts в $apiDestination..."
Move-Item -Path $apiFile.FullName -Destination $apiDestination -Force
Write-Host "Выполняем текстовые замены в api.ts..."
$apiFilePath = Join-Path $apiDestination "api.ts"
if (Test-Path -Path $apiFilePath) {
$apiContent = Get-Content -Path $apiFilePath
# 1. Замена импорта isomorphic-fetch на ESM-совместимый
$apiContent = $apiContent -replace 'import\s+\*\s+as\s+isomorphicFetch\s+from\s+"isomorphic-fetch";', 'import isomorphicFetch from "isomorphic-fetch";'
# 2. Делаем конфигурацию опциональной
$apiContent = $apiContent -replace 'configuration:\s*Configuration;', 'configuration: Configuration | undefined;'
# 3. Форматируем пробелы
$apiContent = $apiContent | ForEach {$_.TrimEnd()}
$apiContent = $apiContent -replace '\t', (' ' * 4)
# Сохраняем изменения
$apiContent | Set-Content -Path $apiFilePath -Encoding UTF8 -Force
} else {
Write-Warning "Файл api.ts не найден по пути $apiFilePath для выполнения замен."
}
Write-Host "Готово! Файл api.ts подготовлен и обновлён."