-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-npm.ps1
117 lines (108 loc) · 4.12 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
114
115
116
117
<#
.SYNOPSIS
Compiles wj-merge.
.DESCRIPTION
Automates all of the necessary steps to compile and optionally publish the wj-merge 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 wj-merge.d.ts definition file.
5. Copies the package.json file.
6. Prepares the npmjs.org readme file by joining PublishNote.md and README.md.
7. 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("wj-merge", "Unit Testing")) {
Invoke-Call { npm run test }
}
}
[string] $path = Resolve-Path .\src\package.json
if ($VerUpgrade -ne '') {
if ($PSCmdlet.ShouldProcess($path, "Package version increment: $VerUpgrade")) {
Set-Location .\src
if ($PreId -ne '') {
npm version $VerUpgrade --preid $PreId --no-git-tag-version
}
else {
npm version $VerUpgrade --no-git-tag-version
}
Set-Location ..\
}
}
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\wj-merge.d.ts .\out
Copy-Item .\src\package.json .\out
# Copy-Item .\PublishNote.md .\out\README.md -Force
Get-Content .\README.md | Add-Content .\out\README.md -Encoding UTF8
if ($PSCmdlet.ShouldProcess($path, "Bunding")) {
Invoke-Call { npm run bundle }
}
if (!$Publish -and -not $WhatIfPreference) {
Write-Output "Running npm publish in dry run mode."
npm publish .\out --dry-run
}
elseif ($PSCmdlet.ShouldProcess($path, "Publish NPM package")) {
npm publish .\out
}
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
}
}
}