Skip to content

Commit

Permalink
feat(version): allow packages not matching folder names (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
sparten11740 committed Jul 8, 2024
1 parent 18ca1c8 commit f7bb33d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
12 changes: 8 additions & 4 deletions dist/version/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/version/index.js.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/version/normalize-packages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ describe('normalizePackages', () => {
name: '@exodus/batmans-identity',
private: true,
}),
batcave: JSON.stringify({
name: '@exodus/batcave',
}),
}

const lernaConfig = JSON.stringify({
Expand Down Expand Up @@ -55,6 +58,7 @@ describe('normalizePackages', () => {
'groups/wayne/bruce/package.json': packageContents.bruce,
'groups/wayne/batman/package.json': packageContents.batman,
'private/batmans-identity/package.json': packageContents.batmansIdentity,
'libraries/wayne-manor/package.json': packageContents.batcave,
})
})

Expand Down Expand Up @@ -90,6 +94,14 @@ describe('normalizePackages', () => {
expect(result).toEqual(['libraries/formatting', 'modules/config'])
})

it('should normalize package names not matching folder names', async () => {
const result = await normalizePackages({
packagesCsv: '@exodus/batcave',
filesystem: fs as never,
})
expect(result).toEqual(['libraries/wayne-manor'])
})

it('should normalize deeply nested package name', async () => {
const result = await normalizePackages({
packagesCsv: '@exodus/the-ultimate-package',
Expand Down
21 changes: 14 additions & 7 deletions src/version/normalize-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ type Params = {
packagesCsv: string
}

type PackageDetails = { path: string; private?: boolean }

export default async function normalizePackages({ packagesCsv, filesystem = fs }: Params) {
const pkgs = await parsePackageFiles<PackageJson>('package.json', { filesystem })
const byFolder = Object.fromEntries(
pkgs.map((pkg) => {
const folder = path.dirname(pkg.path)
return [path.basename(folder), { path: folder, private: pkg.content.private }]
})
)

const byFolder = new Map<string, PackageDetails>()
const byName = new Map<string, PackageDetails>()

for (const pkg of pkgs) {
const folder = path.dirname(pkg.path)
const entry = { path: folder, private: pkg.content.private }

byFolder.set(path.basename(folder), entry)
byName.set(pkg.content.name, entry)
}

const normalized = []
const invalid = []
Expand All @@ -26,7 +33,7 @@ export default async function normalizePackages({ packagesCsv, filesystem = fs }
if (trimmed === '') continue

const folderName = path.basename(trimmed)
const pkg = byFolder[folderName]
const pkg = byName.get(trimmed) ?? byFolder.get(folderName)

if (!pkg) {
invalid.push(trimmed)
Expand Down

0 comments on commit f7bb33d

Please sign in to comment.