11import { existsSync , readFileSync } from 'node:fs' ;
22import { join , relative } from 'node:path' ;
33import { PackageJson } from 'type-fest' ;
4+ import { load } from 'js-yaml' ;
45
56// Class to represent all of the information we need to know about a package in a workspace.
67export class Package {
@@ -9,14 +10,27 @@ export class Package {
910 pathPackageJson : string ; // Absolute path to package.json.
1011 packageJson : PackageJson ;
1112 packageJsonEndsInNewline : boolean ;
13+ pnpmWorkspacePackages ?: string [ ] ;
1214
1315 constructor ( path : string , pathWorkspace : string ) {
1416 this . path = path ;
1517 this . pathWorkspace = pathWorkspace ;
18+
19+ // package.json
1620 this . pathPackageJson = join ( path , 'package.json' ) ;
1721 const packageJsonContents = readFileSync ( this . pathPackageJson , 'utf8' ) ;
1822 this . packageJsonEndsInNewline = packageJsonContents . endsWith ( '\n' ) ;
1923 this . packageJson = JSON . parse ( packageJsonContents ) ;
24+
25+ // pnpm-workspace.yaml
26+ const pnpmWorkspacePath = join ( path , 'pnpm-workspace.yaml' ) ;
27+ if ( existsSync ( pnpmWorkspacePath ) ) {
28+ const pnpmWorkspaceContents = readFileSync ( pnpmWorkspacePath , 'utf8' ) ;
29+ const pnpmWorkspaceYaml = load ( pnpmWorkspaceContents ) as {
30+ packages ?: string [ ] ;
31+ } ;
32+ this . pnpmWorkspacePackages = pnpmWorkspaceYaml . packages ;
33+ }
2034 }
2135
2236 get name ( ) {
@@ -49,6 +63,16 @@ export class Package {
4963 throw new TypeError ( 'package.json `workspaces` is not a string array.' ) ;
5064 }
5165 }
66+
67+ if ( this . pnpmWorkspacePackages ) {
68+ if ( ! Array . isArray ( this . pnpmWorkspacePackages ) ) {
69+ throw new TypeError (
70+ 'pnpm-workspace.yaml `packages` is not a string array.'
71+ ) ;
72+ }
73+ return this . pnpmWorkspacePackages ;
74+ }
75+
5276 return [ ] ;
5377 }
5478
0 commit comments