11import { getProjectDirectory } from "./getProjectDir" ;
22import * as vscode from "vscode" ;
3- import { readPackageJSON } from "pkg-types" ;
4- import { existsSync } from 'fs' ;
3+ import { readPackageJSONFiles } from "./readPackageJSONFiles" ;
4+ import { getAllPackageJSONFiles } from "./getAllPackageJSONFiles" ;
5+ import { existsSync } from "fs" ;
56
67type Dependency = {
78 name : string ;
89 version : string ;
910} ;
1011
1112export const getProjectDependencies = async ( ) : Promise < Dependency [ ] > => {
12- const dependencies : Dependency [ ] = [ ] ;
13+ let dependencies : Dependency [ ] = [ ] ;
14+
1315 const packageJsonPath = `${ getProjectDirectory ( ) } /package.json` ;
1416 const dir = vscode . workspace . workspaceFolders ?. [ 0 ] ?. uri ;
15- const dirContent = dir && await vscode . workspace . fs . readDirectory ( dir ) ;
16- if ( ! existsSync ( packageJsonPath ) && dirContent ?. length ) {
17- vscode . window . showErrorMessage ( `Cannot find package.json !` ) ;
17+
18+ if ( ! dir ) {
19+ vscode . window . showErrorMessage ( `No workspace folder found !` ) ;
1820 return [ ] ;
19- } else {
20- const packageJSON = await readPackageJSON ( packageJsonPath ) ;
21+ }
22+
23+ const packageJsonFiles = await getAllPackageJSONFiles ( dir ) ;
24+ let packageJSON = [ ] ;
25+
26+ if ( ! existsSync ( packageJsonPath ) && packageJsonFiles . length === 0 ) {
27+ vscode . window . showErrorMessage ( `Cannot find any package.json files!` ) ;
28+ return [ ] ;
29+ }
2130
22- const addDependencies = ( depObj : Record < string , string > | undefined ) => {
23- if ( depObj ) {
24- dependencies . push (
25- ...Object . keys ( depObj ) . map ( ( key ) => ( {
26- name : key ,
27- version : depObj [ key ] . replace ( / [ \^ ~ ] / g, '' ) , // Use 'g' flag to replace all occurrences
28- } ) )
29- ) ;
31+ // Read all package.json files
32+ if ( packageJsonFiles . length > 0 ) {
33+ for ( const pkg of packageJsonFiles ) {
34+ try {
35+ const response = await readPackageJSONFiles ( pkg ) ;
36+ if ( response ) {
37+ packageJSON . push ( response ) ;
38+ }
39+ } catch ( error ) {
40+ console . error ( `Error reading package.json at ${ pkg . path } :` , error ) ;
41+ vscode . window . showWarningMessage ( `Failed to read package.json at ${ pkg . path } ` ) ;
3042 }
31- } ;
43+ }
44+ }
3245
33- addDependencies ( packageJSON ?. dependencies ) ;
34- addDependencies ( packageJSON ?. devDependencies ) ;
46+ const addDependencies = ( depObj : Record < string , string > | undefined ) => {
47+ if ( depObj ) {
48+ const newDeps = Object . keys ( depObj ) . map ( ( key ) => ( {
49+ name : key ,
50+ version : depObj [ key ] . replace ( / [ \^ ~ ] / g, '' ) ,
51+ } ) ) ;
52+
53+ for ( const newDep of newDeps ) {
54+ if ( ! dependencies . some ( dep => dep . name === newDep . name ) ) {
55+ dependencies . push ( newDep ) ;
56+ }
57+ }
58+ }
59+ } ;
3560
36- return dependencies ;
61+ for ( const pkg of packageJSON ) {
62+ if ( pkg ) {
63+ addDependencies ( pkg . dependencies ) ;
64+ addDependencies ( pkg . devDependencies ) ;
65+ }
3766 }
67+
68+ return dependencies ;
3869} ;
0 commit comments