Skip to content

Commit cc3d423

Browse files
committed
✨ Add Read-PackageJson & Get-PackageScripts
1 parent 352b536 commit cc3d423

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/utils.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,46 @@ function AreEqual($a, $b) {
99
return $a -eq $b
1010
}
1111
}
12+
13+
function Read-PackageJson {
14+
begin {
15+
$ErrorActionPreference = 'SilentlyContinue'
16+
}
17+
18+
process {
19+
$cwd = Get-Location
20+
$packagePath = Join-Path $cwd 'package.json'
21+
22+
if (-not (Test-Path $packagePath -PathType Leaf)) {
23+
return $null
24+
}
25+
26+
$packagePath = Resolve-Path $packagePath
27+
28+
return Get-Content $packagePath | ConvertFrom-Json
29+
}
30+
}
31+
32+
function Get-PackageScripts {
33+
begin {
34+
$ErrorActionPreference = 'SilentlyContinue'
35+
}
36+
37+
process {
38+
$package = Read-PackageJson
39+
40+
if ((-not $package) -or (-not $package.scripts)) {
41+
return $()
42+
}
43+
44+
$scripts = $package.scripts | Get-Member -MemberType NoteProperty | ForEach-Object {
45+
$_.Name
46+
}
47+
48+
if (-not $scripts) {
49+
return $()
50+
}
51+
52+
return $scripts
53+
}
54+
}

0 commit comments

Comments
 (0)