Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add globbing support for package roots #3

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@
"tsconfig.json",
"README.md"
],
"packageManager": "yarn@3.2.3"
"packageManager": "yarn@3.2.3",
"dependencies": {
"glob": "^10.3.3"
}
}
41 changes: 36 additions & 5 deletions src/package.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,60 @@ import {
getPathsByPackageNames,
parsePackageFiles,
} from './package'
import { createFsFromJSON } from './utils/testing'

describe('package', () => {
let fs: Volume

describe('getPackageRoots', () => {
it('should return normalized package roots', async () => {
fs = Volume.fromJSON({
fs = createFsFromJSON({
'lerna.json': JSON.stringify({
packages: ['libraries/*', 'modules/*', 'single/package'],
packages: ['libraries', 'modules/*', 'single/package', 'nested/**/package'],
}),
'libraries/wayne-manor/package.json': 'some content',
'modules/wayne-tower/package.json': 'some content',
'single/package/package.json': 'some content',
'nested/bruce-wayne/package/package.json': 'some content',
'nested/batman/package/package.json': 'some content',
})

await expect(getPackageRoots({ filesystem: fs as never })).resolves.toEqual([
'libraries',
'modules',
'single/package',
'nested/bruce-wayne/package',
'nested/batman/package',
])
})

it('should return normalized paths for grouped package roots', async () => {
fs = createFsFromJSON({
'lerna.json': JSON.stringify({
packages: [
'libraries',
'modules/exchange/{exchange,exchange-monitors}',
'single/package',
],
}),
'libraries/wayne-manor/package.json': 'some content',
'modules/exchange/exchange/package.json': 'some content',
'modules/exchange/exchange-monitors/package.json': 'some content',
'single/package/package.json': 'some content',
})

await expect(getPackageRoots({ filesystem: fs as never })).resolves.toEqual([
'libraries',
'single/package',
'modules/exchange/exchange',
'modules/exchange/exchange-monitors',
])
})
})

describe('getPackageNameByPath', () => {
beforeEach(() => {
fs = Volume.fromJSON({
fs = createFsFromJSON({
'lerna.json': JSON.stringify({
packages: ['packages/*'],
}),
Expand All @@ -53,7 +84,7 @@ describe('package', () => {
})

const setup = (additionalContents: { [path: string]: string } = {}) => {
fs = Volume.fromJSON({
fs = createFsFromJSON({
'lerna.json': JSON.stringify({
packages: ['modules/*', 'libraries/*', 'empty/*'],
}),
Expand Down Expand Up @@ -112,7 +143,7 @@ describe('package', () => {

describe('parsePackageFiles', () => {
it('should parse files as JSON and return contents ', async () => {
fs = Volume.fromJSON({
fs = createFsFromJSON({
'lerna.json': JSON.stringify({
packages: ['modules/*', 'libraries/*', 'empty/*'],
}),
Expand Down
12 changes: 8 additions & 4 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs'
import { readJson } from './utils/fs'
import { LernaConfig, PackageJson } from './utils/types'
import { filterAsync } from './utils/arrays'
import { glob } from 'glob'

type DefaultParams = {
filesystem?: typeof fs
Expand All @@ -12,11 +13,14 @@ export async function getPackageRoots({ filesystem = fs }: DefaultParams = {}):
const lernaConfig = await readJson<LernaConfig>('lerna.json', { filesystem })
const packageRoots = lernaConfig?.packages ?? ['packages/*']

return packageRoots.map((root: string) => {
if (root.endsWith('*')) return path.dirname(root)
return glob(
packageRoots.map((root: string) => {
if (root.endsWith('*')) return path.dirname(root)

return root
})
return root
}),
{ fs: filesystem }
)
}

export async function getPackagePaths({ filesystem = fs }: DefaultParams = {}): Promise<string[]> {
Expand Down
3 changes: 2 additions & 1 deletion src/user-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Volume } from 'memfs/lib/volume'
import { normalizePackages } from './user-input'
import { createFsFromJSON } from './utils/testing'

describe('normalizePackages', () => {
let fs: Volume
Expand Down Expand Up @@ -27,7 +28,7 @@ describe('normalizePackages', () => {
})

beforeEach(() => {
fs = Volume.fromJSON({
fs = createFsFromJSON({
'lerna.json': lernaConfig,
'modules/storage-mobile/package.json': packageContents.storageMobile,
'deeply/nested/package/root/the-ultimate-package/package.json':
Expand Down
6 changes: 6 additions & 0 deletions src/utils/testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createFsFromVolume } from 'memfs'
import { DirectoryJSON, Volume } from 'memfs/lib/volume'

export function createFsFromJSON(json: DirectoryJSON) {
return createFsFromVolume(Volume.fromJSON(json))
}
Loading