Private/Angular/Setup/Install-NodeModule.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
<############################################################################
 # Run npm install if package not already present in package.json,
 # and import statement add to app.module.ts
 ############################################################################>

Function Install-NodeModule([string]$packageName, [string]$className = "", [string]$importFromFile = "", [string]$classToInsert = "") {

    [string]$packageJsonFile = "$($webCsprojInfo.angularDir)\package.json"
    [string]$packageJsonContents = Get-Content -raw $packageJsonFile
    if($packageJsonContents -like "*`"$($packageName)`"*") {
        # Already installed, do nothing
    } else {
        Write-Host "### Add $packageName library"
        [string] $curDir = Get-Location
        try {
            Set-Location $webCsprojInfo.angularDir
            &{npm install --save $($packageName)}

            # Special case, sometimes this fails, try again, don't know why
            if($LastExitCode -ne 0) {
                &{npm install}
            }
        } finally {
            Set-Location $curDir
        }
        Confirm-LastExitCode

        if($className -ne "") {
            Write-Host "### Add angular include for $packageName "
            Edit-NgModuleAddService $webCsprojInfo $className $importFromFile $classToInsert
        }
    }

}