Private/Edit-PackageJsonFile.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
<#
.SYNOPSIS
Updates a 'package.json' files in a directory using the in the specified [Manifest] object.
 
.PARAMETER $Manifest
The [Manifest] object.
 
.PARAMETER Path
The project directory.
#>


function Edit-PackageJsonFile
{
    Param(
        [Parameter(Mandatory, ValueFromPipeline)]
        $Manifest,

        [string]$Path
    )

    $modifiedFiles = [System.Collections.ArrayList]::new();
    foreach ($file in (Get-ChildItem $Path -Recurse -Filter "package.json" | Select-Object -ExpandProperty FullName))
    {
        if ($PSCmdlet.ShouldProcess($file))
        {
            $package = Get-Content $file | Out-String | ConvertFrom-Json;
            foreach ($field in @{
                "name"=$Manifest.Name;
                "author"=$Manifest.Author;
                "homepage"=$Manifest.Website;
                "description"=$Manifest.Description;
                "keywords"=$Manifest.Tags.Split(' ');
                "repository"=$Manifest.RepositoryUrl;
                "version"=($Manifest | Convert-NcrementVersionNumberToString);
            }.GetEnumerator())
            {
                if (-not [string]::IsNullOrEmpty($field.Value))
                {
                    if ($package.PSObject.Properties.Match($field.Name).Count -eq 0)
                    {
                        if ($field.Name -eq "repository")
                        {
                            $field.Value = @{"type"="git";"url"=$field.Value;};
                        }

                        $package | Add-Member -MemberType NoteProperty -Name $field.Name -Value $field.Value;
                    }
                    elseif ($field.Name -eq "repository")
                    {
                        $package.repository.url = $field.Value;
                    }
                    else
                    {
                        $package."$($field.Name)" = $field.Value;
                    }
                }
            }

            $package | ConvertTo-Json | Out-File $file -Encoding utf8;
            $modifiedFiles.Add($file) | Out-Null;
        }
    }

    return $modifiedFiles;
}