Skip to content

Commit

Permalink
Fix yarn package name parsing (#10)
Browse files Browse the repository at this point in the history
- Fix yarn package name parsing to account for beginning '@'
- Add tests to test package name parsing function
- Add tests to CI and Deploy workflows
  • Loading branch information
kelleyma49 authored Aug 23, 2022
1 parent 3ec7c8e commit 6bf6231
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/scripts/Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Invoke-Pester -Path (Join-Path $PSScriptRoot "..\..\tests") -Verbose
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ jobs:
- name: Build module
run: ./build.ps1
shell: pwsh
- name: Run tests
run: ./.github/scripts/Tests.ps1
shell: pwsh
3 changes: 3 additions & 0 deletions .github/workflows/Deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: Build Crescendo module
run: ./build.ps1
shell: pwsh
- name: Run tests
run: ./.github/scripts/Tests.ps1
shell: pwsh
- name: Publish PowerShell Module
run: ./.github/scripts/Deploy.ps1
shell: pwsh
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
obj/*
output/*
yarn.crescendo/*
sample/node_modules/*
**/node_modules/*
4 changes: 1 addition & 3 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,4 @@ $ManifestInfo = @{
ProjectUri = 'https://github.com/kelleyma49/yarn.crescendo'
}

Update-ModuleManifest -Path (Join-Path $Output "yarn.crescendo.psd1") @ManifestInfo -Verbose

# Invoke-Pester -Path "$PSScriptRoot\tests"
Update-ModuleManifest -Path (Join-Path $Output "yarn.crescendo.psd1") @ManifestInfo -Verbose
8 changes: 8 additions & 0 deletions src/Helpers/Find-PackageNameWithoutVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

function Find-PackageNameWithoutVersion($name) {
$idx = $name.LastIndexOf('@')
if ($idx -gt 0) {
$name = $name.Substring(0, $idx)
}
$name
}
7 changes: 4 additions & 3 deletions src/Helpers/Get-Packages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ param ( $CommandName,
$CommandAst,
$FakeBoundParameters )

. (Join-Path $PSScriptRoot "Find-PackageNameWithoutVersion.ps1")

if ([string]::IsNullOrEmpty($WordToComplete)) {
$json = & yarn list --json | ConvertFrom-Json
}
else {
$json = & yarn list --pattern $WordToComplete --json | ConvertFrom-Json
}
$names = $json.data.trees | ForEach-Object {
$nameWithoutVersion = $_.name.Split('@')[0]
$nameWithoutVersion
Find-PackageNameWithoutVersion $_.name
}
return $names
return $names
38 changes: 38 additions & 0 deletions tests/Helpers.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#Import-Module "$PSScriptRoot\..\yarn-crescendo\yarn.crescendo.psd1" -Force
BeforeAll {
. "$PSScriptRoot\..\src\Helpers\Find-PackageNameWithoutVersion.ps1"
}

Describe "Find-PackageNameWithoutVersion" {
It "should return same string with -" {
Find-PackageNameWithoutVersion "some-package" | Should -Be "some-package"
}

It "should return same string with ." {
Find-PackageNameWithoutVersion "example.com" | Should -Be "example.com"
}

It "should return same string with _" {
Find-PackageNameWithoutVersion "under_score" | Should -Be "under_score"
}

It "should return same string starting with numbers" {
Find-PackageNameWithoutVersion "123numeric" | Should -Be "123numeric"
}

It "should return same string starting @" {
Find-PackageNameWithoutVersion "@npm/thingy" | Should -Be "@npm/thingy"
}

It "should strip number" {
Find-PackageNameWithoutVersion "[email protected]" | Should -Be "thingy"
}

It "should strip number with ^" {
Find-PackageNameWithoutVersion "thingy@^1.2.3" | Should -Be "thingy"
}

It "should strip number" {
Find-PackageNameWithoutVersion "@npm/[email protected]" | Should -Be "@npm/thingy"
}
}

0 comments on commit 6bf6231

Please sign in to comment.