nvm.psm1

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#requires -version 3.0
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

$nvmwPath = Join-Path $PSScriptRoot 'vs'

function Set-NodeVersion {
    <#
    .Synopsis
       Set the node.js version for the current session
    .Description
       Set's the node.js version that was either provided with the -Version parameter or from using the .nvmrc file in the current working directory.
    .Parameter $Version
       A version string for the node.js version you wish to use. Use the format of v#.#.#. This also supports fuzzy matching, so v# will be the latest installed version starting with that major
    .Example
       Set based on the .nvmrc
       Set-NodeVersion
    .Example
       Set-NodeVersion v5
       Set using fuzzy matching
    .Example
       Set-NodeVersion v5.0.1
       Set using explicit version
    #>

    param(
        [string]
        [Parameter(Mandatory=$false)]
        [ValidatePattern('^v\d(\.\d{1,2}){0,2}$')]
        $Version
    )

    if ([string]::IsNullOrEmpty($Version)) {
        if (Test-Path .\.nvmrc) {
            $VersionToUse = Get-Content .\.nvmrc -Raw
        }
        else {
            "Version not given and no .nvmrc file found in folder"
            return
        }
    }
    else {
        $VersionToUse = $version
    }

    $VersionToUse = $VersionToUse.replace("`n","").replace("`r","")

    if (!($VersionToUse -match "v\d\.\d{1,2}\.\d{1,2}")) {
        "Version found is not a full version, using fuzzy matching"
        $VersionToUse = Get-NodeVersions -Filter $VersionToUse | Select-Object -First 1

        if (!$VersionToUse) {
            "No version found to fuzzy match against"
            return
        }
    }

    $requestedVersion = Join-Path $nvmwPath $VersionToUse

    if (!(Test-Path -Path $requestedVersion)) {
        "Could not find node version $VersionToUse"
        return
    }

    $env:Path = "$requestedVersion;$env:Path"
    $env:NODE_PATH = "$requestedVersion;"
    npm config set prefix $requestedVersion
    $env:NODE_PATH += npm root -g
    "Switched to node version $VersionToUse"
}

function Install-NodeVersion {
    <#
    .Synopsis
        Install a version of node.js
    .Description
        Download and install the specified version of node.js into the nvm directory. Once installed it can be used with Set-NodeVersion
    .Parameter $Version
        The version of node.js to install
    .Parameter $Force
        Reinstall an already installed version of node.js
    .Parameter $architecture
        The architecture of node.js to install, defaults to $env:PROCESSOR_ARCHITECTURE
    .Example
        Install-NodeVersion v5.0.0
        Install version 5.0.0 of node.js into the module directory
    .Example
        Install-NodeVersion v5.0.0 -architecture x86
        Installs the x86 version even if you're on an x64 machine
    #>

    param(
        [string]
        [Parameter(Mandatory=$true)]
        [ValidatePattern('^v\d\.\d{1,2}\.\d{1,2}$')]
        $Version,

        [switch]
        $Force,

        [string]
        $architecture = $env:PROCESSOR_ARCHITECTURE
    )

    $requestedVersion = Join-Path $nvmwPath $version

    if ((Test-Path -Path $requestedVersion) -And (-Not $force)) {
        "Version $version is already installed, use -Force to reinstall"
        return
    }

    if (-Not (Test-Path -Path $requestedVersion)) {
        New-Item $requestedVersion -ItemType 'Directory'
    }

    $msiFile = "node-$version-x86.msi"
    $nodeUrl = "http://nodejs.org/dist/$version/$msiFile"

    if ($architecture -eq 'AMD64') {
        $msiFile = "node-$version-x64.msi"

        if ($version -match '^v0\.\d{1,2}\.\d{1,2}$') {
            $nodeUrl = "http://nodejs.org/dist/$version/x64/$msiFile"
        } else {
            $nodeUrl = "http://nodejs.org/dist/$version/$msiFile"
        }
    }

    Invoke-WebRequest -Uri $nodeUrl -OutFile (Join-Path $requestedVersion $msiFile)

    if (-Not (Get-Command msiexec)) {
        "msiexec is not in your path"
        return
    }

    $unpackPath = Join-Path $requestedVersion '.u'
    if (Test-Path $unpackPath) {
        Remove-Item $unpackPath -Recurse -Force
    }

    New-Item $unpackPath -ItemType Directory

    $args = @("/a", (Join-Path $requestedVersion $msiFile), "/qb", "TARGETDIR=`"$unpackPath`"")

    Start-Process -FilePath "msiexec.exe" -Wait -PassThru -ArgumentList $args

    Move-Item (Join-Path (Join-Path $unpackPath 'nodejs') '*') -Destination $requestedVersion -Force
    Remove-Item $unpackPath -Recurse -Force
}

function Remove-NodeVersion {
    <#
    .Synopsis
        Removes an installed version of node.js
    .Description
        Removes an installed version of node.js along with any installed npm modules
    .Parameter $Version
        The full version string of the node.js package to remove
    .Example
        Remove-NodeVersion v5.0.0
        Removes the v5.0.0 version of node.js from the nvm store
    #>

    param(
        [string]
        [Parameter(Mandatory=$true)]
        [ValidatePattern('^v\d\.\d{1,2}\.\d{1,2}$')]
        $Version
    )

    $requestedVersion = Join-Path $nvmwPath $Version

    if (!(Test-Path -Path $requestedVersion)) {
        "Could not find node version $Version"
        return
    }

    Remove-Item $requestedVersion -Force -Recurse
}

function Get-NodeVersions {
    <#
    .Synopsis
        List local or remote node.js versions
    .Description
        Used to show all the node.js versions installed to nvm, using the -Remote option allows you to list versions of node.js available for install. Providing a -Filter parameter can reduce the versions using the pattern, either local or remote versions
    .Parameter $Remote
        Indicate whether or not to list local or remote versions
    .Parameter $Filter
        A version filter supporting fuzzy filters
    .Example
        Get-NodeVersions -Remote -Filter v4.2
        version
        -------
        v4.2.6
        v4.2.5
        v4.2.4
        v4.2.3
        v4.2.2
        v4.2.1
        v4.2.0
    #>

    param(
        [switch]
        $Remote,

        [string]
        [Parameter(Mandatory=$false)]
        [ValidatePattern('^v\d(\.\d{1,2}){0,2}$')]
        $Filter
    )

    if ($Remote) {
        $versions = Invoke-WebRequest -Uri https://nodejs.org/dist/index.json | ConvertFrom-Json

        if ($Filter) {
            $versions = $versions | Where-Object { $_.version.Contains($filter) }
        }

        $versions | Select-Object version | Sort-Object -Descending -Property version
    } else {
        if (!(Test-Path -Path $nvmwPath)) {
            "No Node.js versions have been installed"
        } else {
            $versions = Get-ChildItem $nvmwPath | %{ $_.Name }

            if ($Filter) {
                $versions = $versions | Where-Object { $_.Contains($filter) }
            }

            $versions | Sort-Object -Descending
        }
    }
}

# Start io.js handling

$iojsvmwPath = Join-Path $PSScriptRoot 'vs-iojs'

function Set-iojsVersion {
    param(
        [string]
        [Parameter(Mandatory=$true)]
        #[ValidatePattern('^v\d\.\d{1,2}\.\d{1,2}$')]
        $Version,

        [switch]
        $NoAlias
    )

    $requestedVersion = Join-Path $iojsvmwPath $version

    if (!(Test-Path -Path $requestedVersion)) {
        "Could not find io.js version $version"
        return
    }

    $env:Path = "$requestedVersion;$env:Path"

    if (!$NoAlias) {
        $env:NODE_PATH = "$requestedVersion;"
        $env:NODE_PATH += npm root -g
    }

    $env:IOJS_PATH = "$requestedVersion;"
    npm config set prefix $requestedVersion
    $env:IOJS_PATH += npm root -g

}

function Install-iojsVersion {
    param(
        [string]
        [Parameter(Mandatory=$true)]
        #[ValidatePattern('^v\d\.\d{1,2}\.\d{1,2}$')]
        $Version,

        [switch]
        $Force,

        [switch]
        $Nightly
    )


    $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")

    if (-Not $isAdmin) {
        $continue = Read-Host 'You are not running as an admin, it is likely that the installer will fail later trying to create the node symlink. Continue (y/N)?'

        if ($continue -ne 'y') {
            return
        }
    }

    $requestedVersion = Join-Path $iojsvmwPath $version

    if ($Nightly) {
        $type = 'download/nightly'
    } else {
        $type = 'dist'
    }

    if ((Test-Path -Path $requestedVersion) -And (-Not $force)) {
        "Version $version is already installed, use -Force to reinstall"
        return
    }

    if (-Not (Test-Path -Path $requestedVersion)) {
        New-Item $requestedVersion -ItemType 'Directory'
    }

    $msiFile = "iojs-$version-x86.msi"
    $iojsUrl = "https://iojs.org/$type/$version/$msiFile"

    if ($env:PROCESSOR_ARCHITECTURE -eq 'AMD64') {
        $msiFile = "iojs-$version-x64.msi"
        $iojsUrl = "https://iojs.org/$type/$version/$msiFile"
    }

    Invoke-WebRequest -Uri $iojsUrl -OutFile (Join-Path $requestedVersion $msiFile)

    if (-Not (Get-Command msiexec)) {
        "msiexec is not in your path"
        return
    }

    $unpackPath = Join-Path $requestedVersion '.u'
    if (Test-Path $unpackPath) {
        Remove-Item $unpackPath -Recurse -Force
    }

    New-Item $unpackPath -ItemType Directory

    $args = @("/a", (Join-Path $requestedVersion $msiFile), "/qb", "TARGETDIR=`"$unpackPath`"")

    Start-Process -FilePath "msiexec.exe" -Wait -PassThru -ArgumentList $args

    Move-Item (Join-Path (Join-Path $unpackPath 'iojs') '*') -Destination $requestedVersion -Force
    Remove-Item $unpackPath -Recurse -Force

    cmd /c mklink (Join-Path $requestedVersion 'node.exe') (Join-Path $requestedVersion 'iojs.exe')
}

function Remove-iojsVersion {
    param(
        [string]
        [Parameter(Mandatory=$true)]
        #[ValidatePattern('^v\d\.\d{1,2}\.\d{1,2}$')]
        $Version
    )

    $requestedVersion = Join-Path $iojsvmwPath $Version

    if (!(Test-Path -Path $requestedVersion)) {
        "Could not find io.js version $Version"
        return
    }

    Remove-Item $requestedVersion -Force -Recurse
}

function Get-iojsVersions {
    if (!(Test-Path -Path $iojsvmwPath)) {
        New-Item $iojsvmwPath -ItemType Directory
    }

    Get-ChildItem $iojsvmwPath | %{ $_.Name }
}