1- const { findWorkspacePackages} = require ( '@pnpm/find-workspace-packages' ) ;
1+ const path = require ( 'path' ) ;
2+ const readYamlFile = require ( 'read-yaml-file' ) ;
3+ const fg = require ( 'fast-glob' ) ;
4+ const { readExactProjectManifest} = require ( '@pnpm/read-project-manifest' ) ;
25
36module . exports = {
47 utils : { getProjects} ,
@@ -8,19 +11,68 @@ module.exports = {
811 } ,
912} ;
1013
14+ function requirePackagesManifest ( dir ) {
15+ return readYamlFile ( path . join ( dir , 'pnpm-workspace.yaml' ) ) . catch ( ( err ) => {
16+ if ( err . code === 'ENOENT' ) {
17+ return null ;
18+ }
19+
20+ throw err ;
21+ } ) ;
22+ }
23+
24+ function normalizePatterns ( patterns ) {
25+ const normalizedPatterns = [ ] ;
26+ for ( const pattern of patterns ) {
27+ normalizedPatterns . push ( pattern . replace ( / \/ ? $ / , '/package.json' ) ) ;
28+ normalizedPatterns . push ( pattern . replace ( / \/ ? $ / , '/package.json5' ) ) ;
29+ normalizedPatterns . push ( pattern . replace ( / \/ ? $ / , '/package.yaml' ) ) ;
30+ }
31+ return normalizedPatterns ;
32+ }
33+
34+ function findWorkspacePackages ( cwd ) {
35+ return requirePackagesManifest ( cwd )
36+ . then ( ( manifest ) => {
37+ const patterns = normalizePatterns (
38+ ( manifest && manifest . packages ) || [ '**' ]
39+ ) ;
40+ const opts = {
41+ cwd,
42+ ignore : [ '**/node_modules/**' , '**/bower_components/**' ] ,
43+ } ;
44+
45+ return fg ( patterns , opts ) ;
46+ } )
47+ . then ( ( entries ) => {
48+ const paths = Array . from (
49+ new Set ( entries . map ( ( entry ) => path . join ( cwd , entry ) ) )
50+ ) ;
51+
52+ return Promise . all (
53+ paths . map ( ( manifestPath ) => readExactProjectManifest ( manifestPath ) )
54+ ) ;
55+ } )
56+ . then ( ( manifests ) => {
57+ return manifests . map ( ( manifest ) => manifest . manifest ) ;
58+ } ) ;
59+ }
60+
1161function getProjects ( context ) {
1262 const ctx = context || { } ;
1363 const cwd = ctx . cwd || process . cwd ( ) ;
1464
1565 return findWorkspacePackages ( cwd ) . then ( ( projects ) => {
16- return projects . reduce ( ( projects , project ) => {
17- const name = project . manifest . name ;
66+ return projects
67+ . reduce ( ( projects , project ) => {
68+ const name = project . name ;
1869
19- if ( name && project . dir !== cwd ) {
20- projects . push ( name . charAt ( 0 ) === '@' ? name . split ( '/' ) [ 1 ] : name ) ;
21- }
70+ if ( name ) {
71+ projects . push ( name . charAt ( 0 ) === '@' ? name . split ( '/' ) [ 1 ] : name ) ;
72+ }
2273
23- return projects ;
24- } , [ ] ) ;
74+ return projects ;
75+ } , [ ] )
76+ . sort ( ) ;
2577 } ) ;
2678}
0 commit comments