-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ps1
187 lines (154 loc) Β· 4.52 KB
/
build.ps1
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#Requires -Version 7.0
# Script uses ternary operator.
# Also to avoid environment conflicts
Param(
[Parameter()]
[string] $Version,
[Parameter()]
[ValidateSet("ESModule", "CommonJS")]
[string] $Module,
[Parameter()]
[switch] $NoReset,
[Parameter()]
[switch] $Quiet,
[Parameter()]
[switch] $CleanInstall
)
If ((Get-Location).Path -notlike '*tpy') {
Write-Error "Not in project root"
Exit
}
$IsESM = $Module -eq "ESModule"
$ItemList = "src", "README.md", "LICENSE", "mod.ts"
$DeleteList = "lib", "node_modules", "mod.js", "mod.d.ts"
$Destination = "node"
$NodePackageLocation = "$Destination/package.json"
# Matches the file extention in ESM import/export statements.
# (Text in brackets represent [matched] text in this example.)
#
# import { responseBody, responseHTTP, } from "./error[.ts]";
# import { DeploymentStructures } from "./deployment[.d.ts]";
#
$ImportRegex = "(?<=(?<=\n|^)(im|ex)port(?:(?!(\.d)?\.ts).|\n)*)(\.d)?\.ts"
$NodeOnlyRegex = "\/\/ build:node-only "
$DiscordAPIRegex = "(?<=discord-api-types\/[a-z]+\/v8)\/guild"
# Matches ESM import/export statements without extensions, allowing for insertion.
#
# SHOULD MATCH
# import { TpyWs } from "./ws";
#
# SHOULD NOT MATCH
# export { Tpy as default } from "./src/tpy.js";
#
$NoExtensionRegex = '((ex|im)port {[\S\s]+?} from "\.\/[\S\s]+?(?<!\.js)(?="))(";)'
# if esm
# if .d.ts
# ext -> ""
# else (.ts)
# ext -> ".js"
# else (cjs)
# ext -> ""
# if esm
# if .d.ts
# ext -> ""
# else (.js)
# ext -> ".js"
Function Log {
Param(
[string] $str
)
If (-not $Quiet) {
Write-Host "Tpy Build: " -ForegroundColor DarkBlue -NoNewline
$str
}
}
Log "Searching for previous build artifacts"
[string[]]$PresentFiles = @()
$ItemList + $DeleteList | ForEach-Object {
$F = "$Destination/$_"
If (Test-Path $F) {
$PresentFiles += $F
}
}
$PresentFilesFormated = [String]::Join(", ", $PresentFiles)
If ($PresentFiles.Length -ne 0) {
$decision = $Host.UI.PromptForChoice(
"Item(s) in the specified destination location already exists. ($PresentFilesFormated)",
"Do you want to delete these items and continue?",
('&No', '&Yes'), 1)
If (($decision -eq 1) -or ($null -ne $env:CI)) {
Log "Deleting previous build artifacts"
$PresentFiles | Remove-Item -Recurse -Force
}
}
Log "Copying Deno project files to $Destination"
ForEach ($Item in $ItemList) {
If (Test-Path $Item -PathType Container) {
Copy-Item "$Item" "$Destination" -Recurse
}
Else {
Copy-Item "$Item" "$Destination"
}
}
Log "Setting up package.json for build"
$NodePackage = (Get-Content "$NodePackageLocation" -Raw | ConvertFrom-Json)
$NodePackage.type = $IsESM ? "module" : "commonjs"
If ($Version) {
$v = $Version
$NodePackage.version = $v
}
If ($IsESM) {
$NodePackage.version += "-esm"
}
$NodePackage | ConvertTo-Json > $NodePackageLocation
Log "Bumping node-fetch to ESModule version"
If ($IsESM) {
Set-Location node
npm install "node-fetch@^3.*"
npm uninstall "@types/node"
Set-Location ..
}
Log "Correcting import syntax"
# Node Import Syntax Fix
Get-ChildItem $Destination -Recurse -Filter *.ts | ForEach-Object {
$Regexes = $IsESM -and $_.FullName.EndsWith("d.ts") ? "($NodeOnlyRegex|$DiscordAPIRegex)" : "($ImportRegex|$NodeOnlyRegex|$DiscordAPIRegex)"
$Content = Get-Content $_.FullName -Raw
$Content = $Content -replace $Regexes, ""
If (-not $_.FullName.EndsWith(".d.ts")) {
$Content = $Content -replace "(\.d)?\.ts", ".js"
}
$Content > $_.FullName
}
# Verification
Set-Location "$Destination"
If ($IsESM) {
Log "Fixing ex/import extensions"
Get-ChildItem "src/types" -Recurse | ForEach-Object {
$Content = Get-Content $_.FullName -Raw
$Content = $Content -replace $ImportRegex, ""
$Content > $_.FullName
}
}
$ShorthandModule = $IsESM ? "esm" : "cjs"
Log "Running NPM scripts"
npm ($CleanInstall ? "ci" : "install") -Wait
npm run "build:$ShorthandModule" -Wait
Log "Copying .d.ts files and verifying integrity"
Copy-Item -Recurse "src/types" "lib/src"
npm run "build:${ShorthandModule}_noemit" -Wait
If ($IsESM) {
Log "Fixing ex/import extensions (TS emission)"
Get-ChildItem "lib" -Recurse -Filter *.js | ForEach-Object {
$Content = Get-Content $_.FullName -Raw
$Content = $Content -replace $NoExtensionRegex, '$1.js$3'
$Content > $_.FullName
}
}
Remove-Item "./src" -Recurse
Set-Location ".."
# Reset
If ($IsESM -and (-not $NoReset)) {
Log "Reseting package.json for future use"
Log "Use the `-NoReset` flag to disable this behavior"
& "$PSScriptRoot/reset.ps1"
}