-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-npm.ps1
113 lines (104 loc) · 3.91 KB
/
build-npm.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
<#
.SYNOPSIS
Compiles vite-plugin-single-spa.
.DESCRIPTION
Automates all of the necessary steps to compile and optionally publish the vite-plugin-single-spa package:
1. Runs unit testing.
2. Increments the package version according to what is specified.
3. Compiles the TypeScript source code and outputs it to .\out.
4. Copies the vite-plugin-single-spa.d.ts definition file.
5. Copies the package.json file.
6. If the Publish switch is specified, performs actual publishing to the NPM public registry.
NOTE: If the Publish switch is not specified, then npm publish is run in dry-mode, just to show the potential result
of publishing.
Use the Verbose switch to turn on all messages.
.PARAMETER VerUpgrade
Specify a version change. See the documentation for the command 'npm version' for detailed information.
.PARAMETER PreId
Specify the pre-release ID to use. Common examples would be 'alpha', 'beta' or 'rc'. See the documentation for the command 'npm version' for detailed information.
.PARAMETER Publish
Publish the NPM package to npmjs.org.
.PARAMETER SkipTests
Skips unit testing. Useful to create interim builds for testing. If this switch is specified, then the Publish switch cannot be specified.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $false)]
[ValidateSet("major", "minor", "patch", "premajor", "preminor", "prepatch", "prerelease")]
[string] $VerUpgrade,
[Parameter(Mandatory = $false)]
[string] $PreId,
[Parameter(Mandatory = $false)]
[switch] $Publish,
[Parameter(Mandatory = $false)]
[switch] $SkipTests
)
begin {
function Invoke-Call {
param (
[scriptblock]$ScriptBlock,
[string]$ErrorAction = "Stop"
)
& @ScriptBlock
if (($LASTEXITCODE -ne 0) -and $ErrorAction -eq "Stop") {
exit $LASTEXITCODE
}
}
$ErrorActionPreference = 'Stop'
if ($SkipTests -and $Publish) {
Write-Error "Tests can only be skipped when not publishing."
}
elseif (-not $SkipTests) {
if ($PSCmdlet.ShouldProcess("vite-plugin-single-spa", "Unit Testing")) {
Invoke-Call { npm run test }
}
}
[string] $path = Resolve-Path .\package.json
if ($VerUpgrade -ne '') {
if ($PSCmdlet.ShouldProcess($path, "Package version increment: $VerUpgrade")) {
if ($PreId -ne '') {
npm version $VerUpgrade --preid $PreId --no-git-tag-version
}
else {
npm version $VerUpgrade --no-git-tag-version
}
}
}
else {
Write-Verbose "Version upgrade was not specified. The package's version will not be modified."
}
$path = Resolve-Path .\
if (Test-Path .\out\) {
Remove-Item -Path .\out -Recurse
}
New-Item .\out -ItemType Directory
if ($PSCmdlet.ShouldProcess($path, "TypeScript compilation")) {
Invoke-Call { npx tsc }
}
Copy-Item .\src\vite-plugin-single-spa.d.ts .\out
if (-not (Test-Path .\out\ex)) {
New-Item .\out\ex -ItemType Directory
}
Copy-Item .\src\ex.d.ts .\out\ex\index.d.ts
if (!$Publish -and -not $WhatIfPreference) {
Write-Output "Running npm publish in dry run mode."
npm publish --dry-run
}
elseif ($PSCmdlet.ShouldProcess($path, "Publish NPM package")) {
npm publish
}
elseif ($WhatIfPreference) {
Write-Verbose "NOTE: Running npm publish in dry run mode using sample data for illustration purposes only."
if (-not (Test-Path .\out)) {
New-Item -Path .\out -ItemType Directory -WhatIf:$false
}
if (-not (Test-Path .\out\*.js)) {
New-Item -Path .\out\test.js -ItemType File -WhatIf:$false
}
Copy-Item .\src\package.json .\out -WhatIf:$false
npm publish .\out\ --dry-run
if (Test-Path .\out\test.js) {
Remove-Item .\out\test.js -WhatIf:$false
}
}
}